随笔分类 - C语言程序设计(第2版)
练习题
练习2-2 在不使用运算符&&或者||的条件下编写一个与上面的for循环语句等价的循环语句。
摘要:1 //for (i = 0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i) 2 // s[i] = c; 3 4 enum loop {NO, YES}; 5 enum loop okloop = YES; 6 i = 0; 7 whil
阅读全文
练习2-1 编写一个程序一确定分别由signed及unsigned限定的char,short,int及long类型变量的取值范围。采用打印标准头文件中的相应值以及直接计算两种方式实现。通过直接计算来确定浮点类型的取值范围是一项难度很大的任务。
摘要:1.打印标准头文件中的相应值 1 #include <stdio.h> 2 #include <limits.h> 3 main() 4 { 5 /* signed types */ 6 7 printf("signed char min = %d\n", SCHAR_MIN); 8 printf(
阅读全文
练习1-24 编写一个程序,查找C语言程序中的基本语法错误,如圆括号,方括号以及花括号不配对等。要正确的处理引号(包括单引号,双引号)、转移字符序列与注释(如果读者想把该程序编写成完全通用的程序,难度会比较大。)
摘要:1 #include <stdio.h> 2 3 int brace, brack, paren; 4 5 void in_quote(int c); 6 void in_comment(viod); 7 void search(int c); 8 9 /* rudimentary syntax c
阅读全文
练习1-22 编写一个程序,把较长的输入行折成短一些的两行或者多行,折行的位置在输入行的第N列之前的最后一个非空格之后。要保持程序能够智能地处理输入行很长以及在制定的列前没有空格或者制表符时的情况。
摘要:1 #include <stdio.h> 2 #define MAXCOL 3 /* maximum colum of input*/ 3 #define TABINC 8 /* tab increment size*/ 4 5 char line[MAXCOL]; /* input line*/
阅读全文
练习1-21 编写程序entab,将空格串替换成最少数量的制表符和空格,但要保持单词之间的间隔不变。假设制表符终止位的位置与练习1-20的detab程序的情况相同。当使用一个制表符或者一个空格都可以到达下一个制表符终止位时,选用哪种替换字符比较好?
摘要:1 #include <stdio.h> 2 #define TABINC 8 3 4 main() 5 { 6 int c, nb, nt, pos; 7 nb = 0; 8 nt = 0; 9 for(pos = 1; (c = getchar()) != EOF; ++pos) 10 if(c
阅读全文
练习1-20 编写程序detab,将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方。假设制表符终止位的位置是固定的,比如每隔n列就会出现一个制表符终止位。n应该作为变量还是符号常量呢?
摘要:1 main.c 2 #include <stdio.h> 3 4 #define TABINC 8 //定义每隔TABINC(8)个位置会出现一个制表位 5 6 int main() 7 { 8 int c, nb, pos; 9 10 nb = 0; //到遇到制表符时,到达下个制表位需要的空格
阅读全文
练习1-19 编写函数reverse(s),将字符串s中的字符顺序颠倒过来。使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序。
摘要:1 #include<stdio.h> 2 3 #define MAXLINE 1000 4 5 int get_Line(char s[],int lim); 6 void reversestring(char s[]); 7 8 main() 9 { 10 char line[MAXLINE];
阅读全文
练习1-18 编写一个程序,删除每个输入行末尾的空格以及制表符,并删除完全是空格的行。
摘要:1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define MAXLINE 1000 //允许输入行的最大长度 6 7 int get_Line(char line[], int maxline); 8 9
阅读全文
练习1-17 编写一个程序,打印长度大于80个字符的所有输入行。
摘要:1 #include <stdio.h> 2 #define MAXLINE 1000 //允许输入行的最大长度 3 #define longerLine 80 4 int get_Line(char line[], int maxline); 5 6 7 main() 8 { 9 int len;
阅读全文
练习1-16 修改打印最长文本行的程序的主程序main,使之可以打印任意长度的输入行的长度,并尽可能多地打印文本。
摘要:1 #include <stdio.h> 2 #define MAXLINE 1000 //允许输入行的最大长度 3 4 int get_Line(char line[], int maxline); 5 void copy(char to[], char from[]); 6 7 main() 8
阅读全文
练习1-15 重新编写1.2节中温度转换程序,使用函数实现温度转换计算。
摘要:1 #include <stdio.h> // 包含标准库的信息。 2 3 float fahrToCelsius(float fahr); 4 float celsiusToFahr(float celsius); 5 6 int main() // 定义名为main的函数,它不接受参数值。 7
阅读全文
练习1-14 编写一个程序, 打印输入中各个字符出现频度的直方图。
摘要:#include <stdio.h> int main() { printf(" 打印输入字符频度的直方图 \n"); unsigned int ws[128]; // 字符频度数组。 int i, j, c; i = j = c = 0; // 默认每个字符出现0次。 for (i = 0; i
阅读全文
练习1-13 编写一个程序,打印输入中单词长度的直方图。
摘要:/*水平方向的直方图*/ 1 #include <stdio.h> 2 #define MAXHIST 15 3 #define MAXWORD 11 4 #define IN 1 5 #define OUT 0 6 7 int main() 8 { 9 10 int c, i, nc, state
阅读全文
练习1-12 编写一个程序,以每行一个单词的形式打印其输入。
摘要:1 #include <stdio.h> 2 #define IN 1 3 #define OUT 0 4 5 int main() 6 { 7 int c, state; 8 state = OUT; 9 10 while( (c = getchar()) != EOF) 11 12 { 13 i
阅读全文
练习1-11 如何测试单词计数程序?如果程序中存在某种错误,那么什么样的输入最可能发现这类错误呢?
摘要:1 #include <stdio.h> 2 3 #define IN 1 4 #define OUT 0 5 6 /*统计各个数字、空白符及其他字符出现的次数*/ 7 8 int main() 9 { 10 int c, n1, nw, nc, state; 11 12 state = OUT;
阅读全文
练习1-10 编写一个将输入复制到输出的程序,并将其中的制表符替换为\t,把回退符替换为\b,把反斜杠替换为\\。这样可以将制表符和回退符以可见的方式显示出来。
摘要:1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int c; 6 while ((c=getchar())!=EOF){ /*判断输入字符是否为文件结束符*/ 7 if (c=='\t') /*如果输入字符为制表符*/ 8
阅读全文
练习1-8 编写一个统计空格、制表符与换行符个数的程序。
摘要:1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int space=0, tab=0, line=0, c; 6 while ((c=getchar())!=EOF){ 7 if (c==' ') 8 ++space; 9
阅读全文
练习1-7 编写一个打印EOF值的程序
摘要:1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 printf("%d\n", EOF); 6 system("pause"); 7 return 0; 8 }
阅读全文
练习1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替。
摘要:1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int c, space=0; 6 while ((c=getchar())!=EOF){ 7 if (c==' ') 8 ++space; 9 else 10 space=0
阅读全文
浙公网安备 33010602011771号