## page was renamed from C语言练习 <> = 找出以下程序中的错误并作出解释 = 1. 输出hello, world并换行 {{{ #!cplusplus include main { printf(hello, world/n) } }}} 1. 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数 {{{ #!cplusplus #include "stdio.h" main() { lower = 0; upper = 300; fahr = lower; while( fahr <= upper ) { celsius = 5 * (fahr - 32)/9; printf("%3.0f %6.1f\n", fahr, celsius); fahr += step; } } }}} 1. 单词计数 {{{ #!cplusplus #include enum STATUS{IN, OUT}; main() { int c, state; int nl = nw = nc = 0; while( c = getchar() != EOF) { ++nc; if(c == "\n") ; ++nl; if(c == ' ' && c == '\n' && c == '\t') state = OUT; else if(state = OUT) state = IN; ++nw; } printf("%d %d %d\n", nl, nw, nc); } }}} 1. 单词计数新版{{{#!cplusplus #include /* 所有的输入分两类字符: 一是分界符字符,用OUT表示; 二是非分界符字符,用IN表示 表示当前字符类别的state, 由OUT 变成 IN 时, 就有一个单词读入; */ enum STATUS{IN, OUT}; main() { int c, state; int nl,nw,nc; nl = nw = nc = 0; state = OUT; while( (c = getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') state = OUT; else if(state == OUT){ state = IN; ++nw; } } printf("%d %d %d\n", nl, nw, nc); } }}} 1. 求指数 {{{#!cplusplus #include main() { int i; for(i = 0, i < 10, i++) printf("%d %f %f\n", i, power(2.0f, i), power(-3.0f, i)); return 0; } float power(base, n) { int i; float p; for(i = 1; i < n; ++i) p *= base; return p; } }}} 1. 读取字符串并求字符串长度 {{{ #!cplusplus #include int strlen(char s) { int i = 0; while(s[++i] != '0') ; return i; } int getline(char s, int lim) { int c, i; for (i = 0; i < lim-1; ++i) { if( (c = getchar()) != EOF && c!='\n') s[i] = c; else brake; if (c == '\n') s[++i] = c; s[i] = '\0'; return i; } main() { char s[]; int len; getline(s, 1000); len = strlen(s); printf("%d:%s", len, s); } }}} 1. 栈的数据定义和操作 {{{ #!cplusplus #define MAXVAL 100; extern int sp = 0; double val[MAXVAL]; void push(double f) { if(sp < MAXVAL) val[++sp] = f; else printf("error: stack full"); } double pop(void) { if(sp >= 0) return val[--sp]; else { printf("error: stack empty"); return 0.0; } } main() { int i; for(i = 0; i < 5; i++) push(i); for(i = 0; i < 5; i++) printf("%f", pop(i)); } }}} 1. 递归函数打印一个整数 {{{ #!cplusplus #include void printd(int n) { printd(n / 10); putchar(n % 10 + '0'); } main() { int i = 1234; printd(i); } }}} 1. 读取整数的函数 {{{ #!cplusplus #include #include char buf[BUFSIZE]; int bufp = 0; int getch(void); /*从输入流读取一个字符*/ { return (bufp>0)?buf[--bufp]:getchar(); } void ungetch(int c); /*将一个字符放回输入流*/ { if(bufp >= BUFSIZE) printf("ungetch: too many char\n"); else buf[bufp++] = c; } int getint(int *pn) { int c, sign; while(isspace( getch())); if(!isdigit(c) && c != EOF && c!='+' && c!='-') { ungetch(c); return 0; } sign = c == '-' ? -1 : 1; if( c == '+' || c == '-') c = getch(); for( pn = 0; isdigit(c); c = getch()) pn = 10 * pn + c – '0'; pn *= sign; if(c != EOF) ungetch(c); return c; } }}} = 读程序,写出程序运行的结果 = 1. {{{ #!cplusplus #include int dec(int *a) { return (*a)--; } int inc(int a) { return a++; } main() { int x = 1, y; y = dec( &x ); y = inc(x); printf("%d\n%d\n", x, y); } }}} 1. {{{ #!cplusplus #include #define min(x,y) (((x)<(y))?(x):(y)) #ifdef EOF #define square(x) x*x #else #define square(x) ((x)*(x)) #endif int main() { int a = 1, b = 2; printf("%d\n", square(a+b)); min(a++, b++); printf("%d\n ", min(a, b)); } }}} 1. {{{ #!cplusplus #include void selectsort(int v[], int n) { int i, j, min, temp; for( i = 0; i < n-1; i++) { min = i; for( j = i + 1; j < n; j++) if(v[j] < v[min]) min = j; temp = v[min]; v[min] = v[i]; v[i] = temp; } } int main() { int i; int s[] = { 5, 4, 3, 2, 1}; selectsort(s, 5); for(i = 0; i < 5; i++) printf("%3d", s[i]); } }}} 1. 假定int, unsigned是32位的。 {{{ #!cplusplus #include int bitcount(unsigned x) { int b; x = ~x; for(b = 0; x != 0; x >>= 1) if(x & 1) b++; return b; } main() { printf("%d\n%d\n%d\n", bitcount( ~ 0xA), bitcount(017), bitcount(255) ); } }}} 1. {{{ #!cplusplus #include #define MAXVAL 10 int queue[MAXVAL]; int front = 0; int back = 0; int empty() { return front == back; } int full() { return (back + 1)%MAXVAL == front; } int size() { return (back - front + MAXVAL) % MAXVAL; } void enqueue(int v) { if(full()) return; queue[back++] = v; back = back % MAXVAL; } int dequeue() { int v; if( empty() ) return 0; v = queue[front++]; front = front % MAXVAL; return v; } main() { int i; for(i = 1; !full(); i++) enqueue(i); print("%d\n", size()); while(!empty()) printf("%d\n", dequeue()); } }}} 1. {{{ #!cplusplus #include #include int stricmp(char s[], char t[]) { int i; for( i = 0; s[i] != '\0' && t[i] != '\0'; i++) if(tolower(s[i]) != tolower(t[i])) break; return tolower(s[i]) - tolower(t[i]); } main() { char s1[] = { 'a', 'b', 'c', 'd', '\0'}; char s2[] = "ABCD"; char s3[] = "abcde"; int s1s2 = stricmp(s1, s2); int s2s3 = stricmp(s2, s3); printf("%s is %s %s\n", s1, s1s2==0 ? "equal to" : s1s2 > 0 ? "great than" : "less than" , s2); printf("%s is %s %s\n", s2, s2s3==0 ? "equal to" : s2s3 > 0 ? "great than" : "less than" , s3); } }}} 1. {{{ #!cplusplus #include #define INITVAL 5 int count() { static int c = INITVAL; return c--; } main() { int i; while(i = count() ) printf("%d\n", i); } }}} 1. {{{ #!cplusplus #include double mod(double x, double y) { return x - (int)( x / y ) * y; } main() { printf("%6.1f\n%6.1f\n", mod(23, 3), mod(-36.5, 7.5) ); } }}} = 编程题,按要求编写程序 = 1. 分别编写递归方式和非递归方式的reverse(s)函数,用于将字符串s倒过来 1. 分别编写递归方式和非递归方式的fact(n)函数,用于计算n的阶乘n! 1. 分别编写递归方式和非递归方式的combination(m, n),用于计算组合数 1. 写一个程序统计输入的空格数,tab数,和换行数。 1. 编写一个程序,把输入拷贝到输出,把其中连续的多个空格替换成一个空格。 1. 编写一个程序,把输入中长度超过80个字符的行输出。 1. 编写一个程序,把输入中每一行末尾的空格或者tab删除。 1. 编写一个程序,将输入中的水平制表符替换成适当数目的空格,使空格充满到下一个制表符终止位。假设制表符终止位的位置是固定的,每隔10列就会出现一个制表符终止位。 1. 编写一个程序,把超过50个字符的输入行折成短一些的两行或多行,折行的位置在输入行的第50、100、150……列的位置。 1. 编写一个程序,把输入拷贝到输出,并替换退格符为\b,替换其中的tab为\t,替换反斜杠为\\,使得原来不可见的字符变得可见。 = 读程序,写出程序运行的结果 = 1. {{{ #!cplusplus #include int min(int a, int b) { return a < b ? a : b; } main() { int x = -1, y = -2; printf("%d", min(x, y)); } }}} 1. {{{ #!cplusplus #include int inc(int a) { return ++a; } main() { int x = -1, y; y = inc(x); printf("%d\n%d", x, y); } }}} 1. {{{ #!cplusplus #include #include #define FALSE 0 #define TRUE 1 #define BOOL int BOOL prime(int a) { int i; for( i = 2; i <= sqrt((double)a); i++) if( a % i == 0) return FALSE; return TRUE; } main() { int x = 37, y = 91; printf("%d\n%d", prime(x), prime(y)); } }}} 1. {{{ #!cplusplus #include int fibonacci(int a) { int i, curr = 1, next = 1, temp; for( i = 1; i < a; i++) { temp = next + curr; curr = next; next = temp; } return curr; } main() { int i; for(i = 1; i < 10; i++) printf("%d\t", fibonacci(i)); } }}} 1. {{{ #!cplusplus #include main() { int x = 2, y = 1; /*assume int is 32-bit*/ printf("%d", (~x | ~y) + (x ^ y) ); } }}} 1. {{{ #!cplusplus #include main() { int flag = 0; int count = 0; int c; while( (c = getchar()) != EOF ) { if(flag == 0) { if(c =='c') flag ++; } else if( flag == 1) { if( c == 'z') flag ++; else flag = 0; } else if( flag == 2) { if( c == 'k') count ++; flag = 0; } } printf("%d", count); } }}} 程序运行后输入{{{czk loves c programming language! czk programs every day!}}} 1. {{{ #!cplusplus #include main() { int x, y; x = 0!=4<=1+2*3/4<<1; y = (1 << 2 | 0xF0 ) ^ 071 & 8; printf("%d\n%d", x, y); } }}} 1. {{{ #!cplusplus #include #define PI 3.14 main() { double r; for( r = 1.0; r <= 4.5; r += 1.0) { printf("%6.0f%6.2f%6.2f\n", r, PI*r*2, PI*r*r); } } }}} 1. {{{ #!cplusplus #include enum months{JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; main() { int days[12]; int i; days[JAN] = 31; days[FEB] = 28; days[MAR] = 31; days[APR] = 30; days[MAY] = 31; days[JUN] = 30; days[JUL] = 31; days[AUG] = 31; days[SEP] = 30; days[OCT] = 31; days[NOV] = 30; days[DEC] = 31; for( i = JAN; i <= DEC; ++i) { printf("month %6d has %6d days\n", i, days[i]); } } }}} 1. {{{ #!cplusplus #include enum BOOL {FALSE, TRUE}; int isdigit(int c) { return c >='0' && c<='9'; } int islower(int c) { return c >='a' && c<='z'; } int isupper(int c) { return c >='A' && c<='Z'; } int isalpha(int c) { return isupper(c) || islower(c); } int isspace(int c) { return c=='\n' || c=='\t' || c==' ' || c=='\r' || c=='\v' || c=='\f'; } int isxdigit(int c) { return isdigit(c) || (c>='a' && c <='f') || (c>='A' && c<='F'); } int isalnum(int c) { return isalpha(c) || isdigit(c); } main() { int c; while( (c = getchar())!=EOF) { printf("%2d%2d%2d%2d%2d%2d%2d\n", isdigit(c), islower(c), isupper(c), isalpha(c), isspace(c), isxdigit(c), isalnum(c)); } } }}} 输入(共计7个字符):{{{b =0XF;}}} = 按要求编写程序 = 1. 写一个函数reverse(s),将字符串s反转。比如 {{{ char s[]="hello"; reverse(s); }}} 这时s变成"olleh" 1. 写一个函数invert(x,p,n),这个函数将整数x从右数第p位开始的n位反转(0变1,1变0),并将这个值返回。比如:invert(0x72, 4, 3)将返回0x6E. 1. 请写一个程序分别统计输入当中空格、水平制表符(tab)和换行符(newline)的个数,即程序运行后输入任意一些字符,以Ctrl+Z结束输入,程序输出这些字符中空格的个数、水平制表符的个数以及换行符的个数。 = 找出以下程序中的错误,并作出解释 = 1. 输出hello, world并换行 {{{ #!cplusplus include main() { printf("hello, world/n") } }}} 第一行漏了#,应该是#include /n应该改成\n printf语句后面漏了; 1. 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数 {{{ #!cplusplus #include main() { int fahr; int lower, upper, step; lower = 0; upper = 300; fahr = lower; while( fahr <= upper ) { double celsius = 5 * (fahr – 32)/9; printf("%3.0f %6.1f\n", fahr, celsius); fahr += step; } } }}} step没有赋初值 fahr是整数,格式化串应该对应的%3d而不是%3.0f 5*(fahr - 32)/9为整数,没有保留小数部分,应改为5.0*(fahr - 32.0) / 9.0 1. 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数 {{{ #!cplusplus #include #define LOWER 0; #define UPPER 300; #define STEP 20; main () { int fahr; for(fahr = LOWER, fahr <= UPPER, fahr += STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)(fahr-32)); } }}} #define宏后面不要有分号 for的三个字句应该用;分开,而不能用,隔开 (5.0/9.0)和(fahr-32)之间应该有* 1. 把输入字符复制到输出 {{{ #!cplusplus #include main { while(c = getchar() != EOF) putchar(c); } }}} main函数后面应该有() while条件应该为(c=getchar())!=EOF 变量c没有定义,应该定义为int c; 1. 单词计数 {{{ #!cplusplus #include #define OUT 1 #define IN 0 main() { int c, state = OUT; int nl = nw = nc = 0; while((c=getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' && c == '\n' && c == '\t') state = OUT; else if(state = OUT) { state = IN; ++nw; } } printf("%d %d %d\n", nl, nw, nc); } }}} int nl = nw = nc = 0;是错误的,应该写为int nl = 0, nw = 0, nc = 0; c == ' ' && c == '\n' && c == '\t'应该改为c == ' ' || c == '\n' || c == '\t' state = OUT应该改为state == OUT 1. 求指数 {{{ #!cplusplus #include main() { int i; for(i = 0; i < 10; i++) printf("%d %f %f\n",i, power(float 2.0f, int i), power(float -3.0f,int i)); return 0; } float power(base, n) { int i; float p = 1.0f; for(i = 1; i <= n; ++i) p *= base; return p; } }}} 函数power缺少声明 power函数调用的时候参数错误 power函数定义base和n缺少参数 1. 求字符串长度的函数 {{{ #!cplusplus int strlen(char s[]) { int i = 0; while(s[i] != '0') ++i; return i; } }}} '0'应该改为'\0' 1. 栈的数据定义和操作 {{{ #!cplusplus #define MAXVAL 100 extern int sp = 0; double val[MAXVAL]; void push(double f) { if(sp < MAXVAL) val[++sp] = f; else printf("error: stack full"); } double pop(void) { if(sp >= 0) return val[--sp]; else { printf("error: stack empty"); return 0.0; } } }}} extern声明变量sp的时候不能赋初值 push函数当中val[sp++] = f; 1. 递归函数打印一个整数 {{{ #!cplusplus #include void printd(int n) { if(n < 0) { putchar('-') n = -n; } printd(n / 10); putchar(n % 10 + '0'); } }}} printd(n/10);语句前应该加上if(n/10) 1. 读取整数的函数(getch函数和ungetch函数的定义省略) {{{ #!cplusplus #include int getch(void); /*从输入流读取一个字符*/ void ungetch(int); /*将一个字符放回输入流*/ int getint(int *pn) { int c, sign; while(isspace(c = getch())); if(!isdigit(c) && c != EOF && c!='+' && c!='-') { ungetch(c); return 0; } sign = c == '-' ? -1 : 1; if( c == '+' || c == '-') c = getch(); for( *pn = 0; isdigit(c); c = getch()) pn = 10 * pn + c – '0'; *pn *= sign; if(c != EOF) ungetch(c); return c; } }}} pn = 10 * pn + c – '0';应该改为*pn = 10 * *pn + c - '0'; = 读程序,写出程序运行的结果 = 1. 程序 {{{ #!cplusplus #include int inc(int *a) { return (*a)++; } main() { int x = 1, y; y = inc( &x ); printf("%d\n%d\n", x, y); } }}} 结果: {{{ 2 1 }}} 1. 程序 {{{ #!cplusplus #include double fmod(double x, double y) { return x - (int)( x / y ) * y; } main() { printf("%6.1f\n%6.1f\n", fmod(5.0, 3.0), fmod(-21.0, 5.0) ); } }}} 结果 {{{ 2.0 -1.0 }}} 1. 程序 {{{ #!cplusplus #include int leapyear(int y) { return y % 400 == 0 || y % 100 != 0 && y % 4 == 0; } main() { int low = 1995; int high = 2000; int i; for(i = low; i <= high; ++i) printf("Year %d is %s year\n", i, leapyear(i)?"leap":"nonleap"); } }}} 结果: {{{ Year 1995 is nonleap year Year 1996 is leap year Year 1997 is nonleap year Year 1998 is nonleap year Year 1999 is nonleap year Year 2000 is leap year }}} 1. 程序 {{{ #!cplusplus #include int count() { static int c = 0; return ++c; } main() { int i; for(i = 5; i > 0; i--) printf("count called for %d times\n", count()); } }}} 结果: {{{ count called for 1 times count called for 2 times count called for 3 times count called for 4 times count called for 5 times }}} 1. 程序 {{{ #!cplusplus #include #include int stricmp(char s[], char t[]) { int i; for( i = 0; s[i] != '\0' && t[i] != '\0'; i++) if(tolower(s[i]) != tolower(t[i])) break; return tolower(s[i]) - tolower(t[i]); } main() { char s1[] = { 'a', 'b', 'c', 'd', '\0'}; char s2[] = "ABCD"; char s3[] = "abc"; printf("%s and %s are %s equal\n", s1, s2, (stricmp(s1, s2)==0) ? "" : "not"); printf("%s and %s are %s equal\n", s2, s3, (stricmp(s2, s3)==0) ? "" : "not"); } }}} 结果: {{{ abcd and ABCD are equal ABCD and abc are not equal }}} 1. 程序 {{{ #!cplusplus #include #define MAXVAL 10 int queue[MAXVAL]; int front = 0; int back = 0; void enqueue(int v) { queue[back++] = v; back = back % MAXVAL; } int dequeue() { int v = queue[front++]; front = front % MAXVAL; return v; } main() { int i; for(i = 1; i <= 3; i++) enqueue(i); for(i = 0; i < 3; i++) printf("%d\n", dequeue()); } }}} 结果: {{{ 1 2 3 }}} 1. 程序(此题中假定int和unsigned为32位) {{{ #!cplusplus #include int bitcount(unsigned x) { int b; x = ~x; for(b = 0; x != 0; x >>= 1) if(x & 1) b++; return b; } main() { printf("%d\n%d\n%d\n", bitcount( ~ 0xA), bitcount(017), bitcount(255) ); } }}} 结果 {{{ 2 28 24 }}} 1. 程序 {{{ #!cplusplus #include int weekday(int year, int month, int day) { int y, c; if(month == 1 || month == 2) { month += 12; year--; } y = year % 100; c = year / 100; return (y + y/4 + c / 4 - 2 * c + 26*(month+1) / 10 + day -1) % 7; } int main() { int year = 2005; int month = 12; int day = 25; printf("%d\n", weekday(year, month, day)); switch(weekday(year, month, day)) { case 1: case 2: case 3: case 4: case 5: printf("this is a work day\n"); break; case 6: case 0: printf("this is not a work day\n"); break; default: printf("this day does not exist\n"); } } }}} 结果: {{{ 0 this is not a work day }}} 1. 程序 {{{ #!cplusplus #include void bubblesort(int v[], int n) { int i, j; for( i = n-1; i >= 0; i--) for(j = 0; j < i; j++) if(v[j] > v[j+1]) { int temp = v[j]; v[j] = v[j+1]; v[j+1] = temp; } } int main() { int i; int s[] = { 5, 4, 3, 2, 1}; bubblesort(s, 5); for(i = 0; i < 5; i++) printf("%3d", s[i]); } }}} 结果 {{{ 1 2 3 4 5 }}} 1. 程序 {{{ #!cplusplus #include #define max(x,y) (((x)>(y))?(x):(y)) int main() { int a = 5, b = 6; printf("%d\n", max(5, 6)); max(a++, b++); printf("%d\n ", max(a, b)); } }}} 结果 {{{ 6 8 }}} = 程序填空题,按要求填写程序 = 1. 打印输入的单词长度的直方图 {{{ #!cplusplus #include #define MAXLENGTH 20 main() { int c, i, len, max, nlength[MAXLENGTH+1]; for(i = 0; i <= MAXLENGTH; i++) ___________________ max = 0; len = 0; while (__(c=getchar()) != EOF__) { if( c != ' ' && c!= '\n' && c != '\t') { ++len; } else if(len != 0) { if(len <= MAXLENGTH) { __________________ if(len > max) max = len; } len = 0; } } for(i = 1; ___________; i++) { int j = 0; printf("length:%4d count:%4d ", i, nlength[i]); for(j = 0; ________________; j++) putchar('='); putchar('\n'); } } }}} 程序输入: {{{ Write a program to print a histogram of the lengths of words in its input }}} 程序输出结果: {{{ length: 1 count: 2 == length: 2 count: 4 ==== length: 3 count: 2 == length: 4 count: 0 length: 5 count: 4 ==== length: 6 count: 0 length: 7 count: 2 == length: 8 count: 0 length: 9 count: 1 = }}} 1. 快速排序 {{{ #!cplusplus #include void swap(int v[], int i, int j); void qsort(int v[], int left, int right); int main() { int i; int v[] = {15, 12, 31, 21 ,45}; qsort(_____________); for(i = 0; i < 5; i++) printf("%4d", v[i]); } void qsort(int v[], int left, int right) { int i, last; if(_______________) return; swap(v, left, (left+right)/2); last = left; for(i = left + 1; i <= right; i++) if(v[i] < v[left]) swap(v, ++last, i); swap(v, left, last); qsort(_____________________); qsort(_____________________); } void swap(int v[], int i, int j) { ___________; v[i] = v[j]; v[j] = temp; } }}} 程序输出结果 {{{ 12 15 21 31 45 }}} = 编程题,按要求编写程序 = 1. 编写一个ackerman函数,这个函数用于计算如下数学公式。编写一个main函数计算并输出A(3, 4)的值 {{{ #!cplusplus #include #include #define TABSIZE 10 main() { int c, i; int count = 0; while((c=getchar())!=EOF) { if(c=='\t') { int space = TABSIZE - count % TABSIZE; for(i = 0; i < space; i++) { putchar(' '); count ++; } } else if (c == '\n'){ putchar(c); count = 0; } else { putchar(c); count ++; } } } }}}