|
大小: 24075
备注:
|
大小: 24867
备注: converted to 1.6 markup
|
| 删除的内容标记成这样。 | 加入的内容标记成这样。 |
| 行号 1: | 行号 1: |
| [[TableOfContents]] | ## page was renamed from C语言练习 <<TableOfContents>> |
| 行号 5: | 行号 7: |
| #!cplusplus | #!cplusplus |
| 行号 13: | 行号 15: |
| #!cplusplus | #!cplusplus |
| 行号 29: | 行号 31: |
| #!cplusplus #include <stdio.h> enum STATUS{IN, OUT}; |
#!cplusplus #include <stdio.h> enum STATUS{IN, OUT}; |
| 行号 48: | 行号 50: |
| 1. 单词计数新版 {{{ #!cplusplus #include <stdio.h> /* 所有的输入分两类字符: 一是分界符字符,用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); } }}} |
|
| 行号 50: | 行号 80: |
| #!cplusplus | #!cplusplus |
| 行号 58: | 行号 88: |
| float power(base, n) | float power(base, n) |
| 行号 69: | 行号 99: |
| #!cplusplus | #!cplusplus |
| 行号 81: | 行号 111: |
| if( (c = getchar()) != EOF && c!='\n') | if( (c = getchar()) != EOF && c!='\n') |
| 行号 90: | 行号 120: |
| main() | main() |
| 行号 101: | 行号 131: |
| #!cplusplus | #!cplusplus |
| 行号 130: | 行号 160: |
| #!cplusplus | #!cplusplus |
| 行号 145: | 行号 175: |
| #!cplusplus | #!cplusplus |
| 行号 161: | 行号 191: |
| int getint(int *pn) | int getint(int *pn) |
| 行号 180: | 行号 210: |
| 行号 183: | 行号 212: |
| #!cplusplus | #!cplusplus |
| 行号 186: | 行号 215: |
| return (*a)--; | return (*a)--; |
| 行号 200: | 行号 229: |
| #!cplusplus | #!cplusplus |
| 行号 216: | 行号 245: |
| #!cplusplus | #!cplusplus |
| 行号 240: | 行号 269: |
| #!cplusplus | #!cplusplus |
| 行号 255: | 行号 284: |
| #!cplusplus | #!cplusplus |
| 行号 290: | 行号 319: |
| printf("%d\n", dequeue()); } }}} 1. {{{ #!cplusplus |
printf("%d\n", dequeue()); } }}} 1. {{{ #!cplusplus |
| 行号 315: | 行号 344: |
| #!cplusplus | #!cplusplus |
| 行号 329: | 行号 358: |
| #!cplusplus | #!cplusplus |
| 行号 338: | 行号 367: |
| 行号 350: | 行号 378: |
| 行号 353: | 行号 380: |
| #!cplusplus | #!cplusplus |
| 行号 356: | 行号 383: |
| return a < b ? a : b; | return a < b ? a : b; |
| 行号 364: | 行号 391: |
| #!cplusplus | #!cplusplus |
| 行号 367: | 行号 394: |
| return ++a; | return ++a; |
| 行号 376: | 行号 403: |
| #!cplusplus | #!cplusplus |
| 行号 387: | 行号 414: |
| return FALSE; return TRUE; |
return FALSE; return TRUE; |
| 行号 396: | 行号 423: |
| #!cplusplus | #!cplusplus |
| 行号 405: | 行号 432: |
| return curr; | return curr; |
| 行号 414: | 行号 441: |
| #!cplusplus | #!cplusplus |
| 行号 422: | 行号 449: |
| #!cplusplus | #!cplusplus |
| 行号 451: | 行号 478: |
| #!cplusplus | #!cplusplus |
| 行号 462: | 行号 489: |
| #!cplusplus | #!cplusplus |
| 行号 473: | 行号 500: |
| #!cplusplus | #!cplusplus |
| 行号 497: | 行号 524: |
| #!cplusplus | #!cplusplus |
| 行号 513: | 行号 540: |
| return c=='\n' || c=='\t' || c==' ' || c=='\r' || c=='\v' || c=='\f'; | return c=='\n' || c=='\t' || c==' ' || c=='\r' || c=='\v' || c=='\f'; |
| 行号 529: | 行号 556: |
| 行号 535: | 行号 561: |
| char s[]="hello"; | char s[]="hello"; |
| 行号 541: | 行号 567: |
| 行号 544: | 行号 568: |
| 1. 输出hello, world并换行 {{{#!cplusplus |
1. 输出hello, world并换行 {{{ #!cplusplus |
| 行号 560: | 行号 585: |
1. 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数 {{{#!cplusplus |
1. 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数 {{{ #!cplusplus |
| 行号 593: | 行号 616: |
| 行号 602: | 行号 622: |
1. 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数 {{{#!cplusplus |
1. 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数 {{{ #!cplusplus |
| 行号 626: | 行号 644: |
| 行号 634: | 行号 650: |
1. 把输入字符复制到输出 {{{#!cplusplus |
1. 把输入字符复制到输出 {{{ #!cplusplus |
| 行号 655: | 行号 669: |
1. 单词计数 {{{#!cplusplus |
1. 单词计数 {{{ #!cplusplus |
| 行号 704: | 行号 716: |
1. 求指数 {{{#!cplusplus |
1. 求指数 {{{ #!cplusplus |
| 行号 723: | 行号 733: |
| float power(base, n) | float power(base, n) |
| 行号 745: | 行号 755: |
1. 求字符串长度的函数 {{{#!cplusplus |
1. 求字符串长度的函数 {{{ #!cplusplus |
| 行号 764: | 行号 772: |
| 行号 769: | 行号 774: |
1. 栈的数据定义和操作 {{{#!cplusplus |
1. 栈的数据定义和操作 {{{ #!cplusplus |
| 行号 808: | 行号 811: |
| 行号 815: | 行号 815: |
1. 递归函数打印一个整数 {{{#!cplusplus |
1. 递归函数打印一个整数 {{{ #!cplusplus |
| 行号 838: | 行号 836: |
| 行号 843: | 行号 838: |
1. 读取整数的函数(getch函数和ungetch函数的定义省略) {{{#!cplusplus |
1. 读取整数的函数(getch函数和ungetch函数的定义省略) {{{ #!cplusplus |
| 行号 854: | 行号 847: |
| int getint(int *pn) | int getint(int *pn) |
| 行号 890: | 行号 883: |
| 行号 895: | 行号 885: |
| 行号 899: | 行号 886: |
| 1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 903: | 行号 891: |
| return (*a)++; | return (*a)++; |
| 行号 913: | 行号 901: |
| 行号 917: | 行号 906: |
1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 922: | 行号 911: |
| return x - (int)( x / y ) * y; | return x - (int)( x / y ) * y; |
| 行号 930: | 行号 919: |
| 行号 934: | 行号 924: |
1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 951: | 行号 941: |
| 行号 959: | 行号 950: |
1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 975: | 行号 966: |
| 行号 982: | 行号 974: |
1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 1004: | 行号 996: |
| 行号 1008: | 行号 1001: |
1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 1033: | 行号 1026: |
| printf("%d\n", dequeue()); | printf("%d\n", dequeue()); |
| 行号 1037: | 行号 1030: |
| 行号 1042: | 行号 1036: |
1. 程序(此题中假定int和unsigned为32位) {{{#!cplusplus |
1. 程序(此题中假定int和unsigned为32位) {{{ #!cplusplus |
| 行号 1062: | 行号 1054: |
| 行号 1067: | 行号 1060: |
1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 1073: | 行号 1066: |
| 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 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; |
| 行号 1084: | 行号 1077: |
| 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"); } |
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"); } |
| 行号 1101: | 行号 1094: |
| 行号 1105: | 行号 1099: |
1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 1130: | 行号 1124: |
| 行号 1133: | 行号 1128: |
1. 程序 {{{#!cplusplus |
1. 程序 {{{ #!cplusplus |
| 行号 1148: | 行号 1143: |
| 行号 1152: | 行号 1148: |
| 行号 1154: | 行号 1149: |
| 1. 打印输入的单词长度的直方图 {{{#!cplusplus |
1. 打印输入的单词长度的直方图 {{{ #!cplusplus |
| 行号 1171: | 行号 1167: |
| if(len > max) | if(len > max) |
| 行号 1187: | 行号 1183: |
| 行号 1191: | 行号 1188: |
| 行号 1202: | 行号 1200: |
| 1. 快速排序 {{{#!cplusplus |
1. 快速排序 {{{ #!cplusplus |
| 行号 1238: | 行号 1237: |
| 行号 1242: | 行号 1242: |
| 1. 编写一个ackerman函数,这个函数用于计算如下数学公式。编写一个main函数计算并输出A(3, 4)的值 {{{#!cplusplus |
1. 编写一个ackerman函数,这个函数用于计算如下数学公式。编写一个main函数计算并输出A(3, 4)的值 {{{ #!cplusplus |
| 行号 1260: | 行号 1260: |
1. 编写一个程序,将输入中的水平制表符替换成适当数目的空格,使空格充满到下一个制表符终止位。假设制表符终止位的位置是固定的,每隔10列就会出现一个制表符终止位。 {{{#!cplusplus |
1. 编写一个程序,将输入中的水平制表符替换成适当数目的空格,使空格充满到下一个制表符终止位。假设制表符终止位的位置是固定的,每隔10列就会出现一个制表符终止位。 {{{ #!cplusplus |
| 行号 1279: | 行号 1277: |
| } | } |
目录
找出以下程序中的错误并作出解释
- 输出hello, world并换行
- 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数
- 单词计数
1 #include <stdio.h> 2 enum STATUS{IN, OUT}; 3 main() { 4 int c, state; 5 int nl = nw = nc = 0; 6 while( c = getchar() != EOF) { 7 ++nc; 8 if(c == "\n") ; 9 ++nl; 10 if(c == ' ' && c == '\n' && c == '\t') 11 state = OUT; 12 else if(state = OUT) 13 state = IN; 14 ++nw; 15 } 16 printf("%d %d %d\n", nl, nw, nc); 17 }
- 单词计数新版
1 #include <stdio.h>
2 /* 所有的输入分两类字符:
3 一是分界符字符,用OUT表示;
4 二是非分界符字符,用IN表示
5 表示当前字符类别的state, 由OUT 变成 IN 时, 就有一个单词读入; */
6 enum STATUS{IN, OUT};
7 main() {
8 int c, state;
9 int nl,nw,nc;
10 nl = nw = nc = 0;
11 state = OUT;
12 while( (c = getchar()) != EOF) {
13 ++nc;
14 if(c == '\n')
15 ++nl;
16 if(c == ' ' || c == '\n' || c == '\t')
17 state = OUT;
18 else if(state == OUT){
19 state = IN;
20 ++nw;
21 }
22 }
23 printf("%d %d %d\n", nl, nw, nc);
24 }
- 求指数
- 读取字符串并求字符串长度
1 #include <stdio.h> 2 int strlen(char s) 3 { 4 int i = 0; 5 while(s[++i] != '0') ; 6 return i; 7 } 8 int getline(char s, int lim) 9 { 10 int c, i; 11 for (i = 0; i < lim-1; ++i) { 12 if( (c = getchar()) != EOF && c!='\n') 13 s[i] = c; 14 else 15 brake; 16 if (c == '\n') 17 s[++i] = c; 18 s[i] = '\0'; 19 return i; 20 } 21 main() 22 { 23 char s[]; 24 int len; 25 getline(s, 1000); 26 len = strlen(s); 27 printf("%d:%s", len, s); 28 }
- 栈的数据定义和操作
1 #define MAXVAL 100; 2 extern int sp = 0; 3 double val[MAXVAL]; 4 void push(double f) { 5 if(sp < MAXVAL) 6 val[++sp] = f; 7 else 8 printf("error: stack full"); 9 } 10 double pop(void) { 11 if(sp >= 0) 12 return val[--sp]; 13 else { 14 printf("error: stack empty"); 15 return 0.0; 16 } 17 } 18 19 main() { 20 int i; 21 for(i = 0; i < 5; i++) 22 push(i); 23 for(i = 0; i < 5; i++) 24 printf("%f", pop(i)); 25 }
- 递归函数打印一个整数
- 读取整数的函数
1 #include <ctype.h> 2 #include <stdio.h> 3 char buf[BUFSIZE]; 4 int bufp = 0; 5 int getch(void); /*从输入流读取一个字符*/ 6 { 7 return (bufp>0)?buf[--bufp]:getchar(); 8 } 9 void ungetch(int c); /*将一个字符放回输入流*/ 10 { 11 if(bufp >= BUFSIZE) 12 printf("ungetch: too many char\n"); 13 else 14 buf[bufp++] = c; 15 } 16 int getint(int *pn) 17 { 18 int c, sign; 19 while(isspace( getch())); 20 if(!isdigit(c) && c != EOF && c!='+' && c!='-') { 21 ungetch(c); 22 return 0; 23 } 24 sign = c == '-' ? -1 : 1; 25 if( c == '+' || c == '-') 26 c = getch(); 27 for( pn = 0; isdigit(c); c = getch()) 28 pn = 10 * pn + c – '0'; 29 pn *= sign; 30 if(c != EOF) 31 ungetch(c); 32 return c; 33 }
读程序,写出程序运行的结果
1 #include <stdio.h> 2 void selectsort(int v[], int n) { 3 int i, j, min, temp; 4 for( i = 0; i < n-1; i++) { 5 min = i; 6 for( j = i + 1; j < n; j++) 7 if(v[j] < v[min]) 8 min = j; 9 temp = v[min]; 10 v[min] = v[i]; 11 v[i] = temp; 12 } 13 } 14 int main() { 15 int i; 16 int s[] = { 5, 4, 3, 2, 1}; 17 selectsort(s, 5); 18 for(i = 0; i < 5; i++) 19 printf("%3d", s[i]); 20 }
- 假定int, unsigned是32位的。
1 #include <stdio.h> 2 #define MAXVAL 10 3 int queue[MAXVAL]; 4 int front = 0; 5 int back = 0; 6 int empty() { 7 return front == back; 8 } 9 int full() { 10 return (back + 1)%MAXVAL == front; 11 } 12 int size() { 13 return (back - front + MAXVAL) % MAXVAL; 14 } 15 void enqueue(int v) { 16 if(full()) 17 return; 18 queue[back++] = v; 19 back = back % MAXVAL; 20 } 21 int dequeue() { 22 int v; 23 if( empty() ) 24 return 0; 25 v = queue[front++]; 26 front = front % MAXVAL; 27 return v; 28 } 29 main() { 30 int i; 31 for(i = 1; !full(); i++) 32 enqueue(i); 33 print("%d\n", size()); 34 while(!empty()) 35 printf("%d\n", dequeue()); 36 }
1 #include <stdio.h> 2 #include <ctype.h> 3 int stricmp(char s[], char t[]) { 4 int i; 5 for( i = 0; s[i] != '\0' && t[i] != '\0'; i++) 6 if(tolower(s[i]) != tolower(t[i])) 7 break; 8 return tolower(s[i]) - tolower(t[i]); 9 } 10 main() { 11 char s1[] = { 'a', 'b', 'c', 'd', '\0'}; 12 char s2[] = "ABCD"; 13 char s3[] = "abcde"; 14 int s1s2 = stricmp(s1, s2); 15 int s2s3 = stricmp(s2, s3); 16 printf("%s is %s %s\n", s1, s1s2==0 ? "equal to" : s1s2 > 0 ? "great than" : "less than" , s2); 17 printf("%s is %s %s\n", s2, s2s3==0 ? "equal to" : s2s3 > 0 ? "great than" : "less than" , s3); 18 }
编程题,按要求编写程序
- 分别编写递归方式和非递归方式的reverse(s)函数,用于将字符串s倒过来
- 分别编写递归方式和非递归方式的fact(n)函数,用于计算n的阶乘n!
- 分别编写递归方式和非递归方式的combination(m, n),用于计算组合数
- 写一个程序统计输入的空格数,tab数,和换行数。
- 编写一个程序,把输入拷贝到输出,把其中连续的多个空格替换成一个空格。
- 编写一个程序,把输入中长度超过80个字符的行输出。
- 编写一个程序,把输入中每一行末尾的空格或者tab删除。
- 编写一个程序,将输入中的水平制表符替换成适当数目的空格,使空格充满到下一个制表符终止位。假设制表符终止位的位置是固定的,每隔10列就会出现一个制表符终止位。
- 编写一个程序,把超过50个字符的输入行折成短一些的两行或多行,折行的位置在输入行的第50、100、150……列的位置。
- 编写一个程序,把输入拷贝到输出,并替换退格符为\b,替换其中的tab为\t,替换反斜杠为\\,使得原来不可见的字符变得可见。
读程序,写出程序运行的结果
1 #include <stdio.h> 2 #include <math.h> 3 #define FALSE 0 4 #define TRUE 1 5 #define BOOL int 6 7 BOOL prime(int a) { 8 int i; 9 for( i = 2; i <= sqrt((double)a); i++) 10 if( a % i == 0) 11 return FALSE; 12 return TRUE; 13 } 14 main() { 15 int x = 37, y = 91; 16 printf("%d\n%d", prime(x), prime(y)); 17 }
1 #include <stdio.h> 2 3 main() { 4 int flag = 0; 5 int count = 0; 6 int c; 7 while( (c = getchar()) != EOF ) { 8 if(flag == 0) { 9 if(c =='c') 10 flag ++; 11 } 12 else if( flag == 1) { 13 if( c == 'z') 14 flag ++; 15 else 16 flag = 0; 17 } 18 else if( flag == 2) { 19 if( c == 'k') 20 count ++; 21 flag = 0; 22 } 23 } 24 printf("%d", count); 25 }
程序运行后输入czk loves c programming language! czk programs every day!
1 #include <stdio.h> 2 enum months{JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; 3 main() { 4 int days[12]; 5 int i; 6 days[JAN] = 31; 7 days[FEB] = 28; 8 days[MAR] = 31; 9 days[APR] = 30; 10 days[MAY] = 31; 11 days[JUN] = 30; 12 days[JUL] = 31; 13 days[AUG] = 31; 14 days[SEP] = 30; 15 days[OCT] = 31; 16 days[NOV] = 30; 17 days[DEC] = 31; 18 for( i = JAN; i <= DEC; ++i) { 19 printf("month %6d has %6d days\n", i, days[i]); 20 } 21 }
1 #include <stdio.h> 2 enum BOOL {FALSE, TRUE}; 3 int isdigit(int c) { 4 return c >='0' && c<='9'; 5 } 6 int islower(int c) { 7 return c >='a' && c<='z'; 8 } 9 int isupper(int c) { 10 return c >='A' && c<='Z'; 11 } 12 int isalpha(int c) { 13 return isupper(c) || islower(c); 14 } 15 int isspace(int c) { 16 return c=='\n' || c=='\t' || c==' ' || c=='\r' || c=='\v' || c=='\f'; 17 } 18 int isxdigit(int c) { 19 return isdigit(c) || (c>='a' && c <='f') || (c>='A' && c<='F'); 20 } 21 int isalnum(int c) { 22 return isalpha(c) || isdigit(c); 23 } 24 main() { 25 int c; 26 while( (c = getchar())!=EOF) { 27 printf("%2d%2d%2d%2d%2d%2d%2d\n", isdigit(c), islower(c), isupper(c), 28 isalpha(c), isspace(c), isxdigit(c), isalnum(c)); 29 } 30 }
输入(共计7个字符):b =0XF;
按要求编写程序
- 写一个函数reverse(s),将字符串s反转。比如
char s[]="hello"; reverse(s);
这时s变成"olleh" - 写一个函数invert(x,p,n),这个函数将整数x从右数第p位开始的n位反转(0变1,1变0),并将这个值返回。比如:invert(0x72, 4, 3)将返回0x6E.
- 请写一个程序分别统计输入当中空格、水平制表符(tab)和换行符(newline)的个数,即程序运行后输入任意一些字符,以Ctrl+Z结束输入,程序输出这些字符中空格的个数、水平制表符的个数以及换行符的个数。
找出以下程序中的错误,并作出解释
- 输出hello, world并换行
第一行漏了#,应该是#include <stdio.h>
/n应该改成\n
printf语句后面漏了;
- 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数
step没有赋初值
fahr是整数,格式化串应该对应的%3d而不是%3.0f
5*(fahr - 32)/9为整数,没有保留小数部分,应改为5.0*(fahr - 32.0) / 9.0
- 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数
#define宏后面不要有分号
for的三个字句应该用;分开,而不能用,隔开
(5.0/9.0)和(fahr-32)之间应该有*
- 把输入字符复制到输出
main函数后面应该有()
while条件应该为(c=getchar())!=EOF
变量c没有定义,应该定义为int c;
- 单词计数
1 #include <stdio.h>
2
3 #define OUT 1
4
5 #define IN 0
6
7 main() {
8
9 int c, state = OUT;
10
11 int nl = nw = nc = 0;
12
13 while((c=getchar()) != EOF) {
14
15 ++nc;
16
17 if(c == '\n')
18
19 ++nl;
20
21 if(c == ' ' && c == '\n' && c == '\t')
22
23 state = OUT;
24
25 else if(state = OUT) {
26
27 state = IN;
28
29 ++nw;
30
31 }
32
33 }
34
35 printf("%d %d %d\n", nl, nw, nc);
36
37 }
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 #include <stdio.h>
2
3 main() {
4
5 int i;
6
7 for(i = 0; i < 10; i++)
8
9 printf("%d %f %f\n",i, power(float 2.0f, int i), power(float -3.0f,int i));
10
11 return 0;
12
13 }
14
15 float power(base, n)
16
17 {
18
19 int i;
20
21 float p = 1.0f;
22
23 for(i = 1; i <= n; ++i)
24
25 p *= base;
26
27 return p;
28
29 }
函数power缺少声明
power函数调用的时候参数错误
power函数定义base和n缺少参数
- 求字符串长度的函数
'0'应该改为'\0'
- 栈的数据定义和操作
1 #define MAXVAL 100
2
3 extern int sp = 0;
4
5 double val[MAXVAL];
6
7 void push(double f) {
8
9 if(sp < MAXVAL)
10
11 val[++sp] = f;
12
13 else
14
15 printf("error: stack full");
16
17 }
18
19 double pop(void) {
20
21 if(sp >= 0)
22
23 return val[--sp];
24
25 else {
26
27 printf("error: stack empty");
28
29 return 0.0;
30
31 }
32
33 }
extern声明变量sp的时候不能赋初值
push函数当中val[sp++] = f;
- 递归函数打印一个整数
printd(n/10);语句前应该加上if(n/10)
- 读取整数的函数(getch函数和ungetch函数的定义省略)
1 #include <ctype.h>
2
3 int getch(void); /*从输入流读取一个字符*/
4
5 void ungetch(int); /*将一个字符放回输入流*/
6
7 int getint(int *pn)
8
9 {
10
11 int c, sign;
12
13 while(isspace(c = getch()));
14
15 if(!isdigit(c) && c != EOF && c!='+' && c!='-') {
16
17 ungetch(c);
18
19 return 0;
20
21 }
22
23 sign = c == '-' ? -1 : 1;
24
25 if( c == '+' || c == '-')
26
27 c = getch();
28
29 for( *pn = 0; isdigit(c); c = getch())
30
31 pn = 10 * pn + c – '0';
32
33 *pn *= sign;
34
35 if(c != EOF)
36
37 ungetch(c);
38
39 return c;
40
41 }
pn = 10 * pn + c – '0';应该改为*pn = 10 * *pn + c - '0';
读程序,写出程序运行的结果
- 程序
结果:
2 1
- 程序
结果
2.0 -1.0
- 程序
结果:
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
- 程序
结果:
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 #include <stdio.h>
2 #include <ctype.h>
3 int stricmp(char s[], char t[]) {
4 int i;
5 for( i = 0; s[i] != '\0' && t[i] != '\0'; i++)
6 if(tolower(s[i]) != tolower(t[i]))
7 break;
8 return tolower(s[i]) - tolower(t[i]);
9 }
10
11 main() {
12 char s1[] = { 'a', 'b', 'c', 'd', '\0'};
13 char s2[] = "ABCD";
14 char s3[] = "abc";
15 printf("%s and %s are %s equal\n", s1, s2, (stricmp(s1, s2)==0) ? "" : "not");
16 printf("%s and %s are %s equal\n", s2, s3, (stricmp(s2, s3)==0) ? "" : "not");
17 }
结果:
abcd and ABCD are equal ABCD and abc are not equal
- 程序
1 #include <stdio.h>
2 #define MAXVAL 10
3 int queue[MAXVAL];
4 int front = 0;
5 int back = 0;
6
7 void enqueue(int v) {
8 queue[back++] = v;
9 back = back % MAXVAL;
10 }
11
12 int dequeue() {
13 int v = queue[front++];
14 front = front % MAXVAL;
15 return v;
16 }
17
18 main() {
19 int i;
20 for(i = 1; i <= 3; i++)
21 enqueue(i);
22 for(i = 0; i < 3; i++)
23 printf("%d\n", dequeue());
24 }
结果:
1 2 3
- 程序(此题中假定int和unsigned为32位)
结果
2 28 24
- 程序
1 #include <stdio.h>
2
3 int weekday(int year, int month, int day) {
4 int y, c;
5 if(month == 1 || month == 2) {
6 month += 12;
7 year--;
8 }
9 y = year % 100;
10 c = year / 100;
11 return (y + y/4 + c / 4 - 2 * c + 26*(month+1) / 10 + day -1) % 7;
12 }
13
14 int main() {
15 int year = 2005;
16 int month = 12;
17 int day = 25;
18 printf("%d\n", weekday(year, month, day));
19 switch(weekday(year, month, day)) {
20 case 1: case 2: case 3: case 4: case 5:
21 printf("this is a work day\n");
22 break;
23 case 6: case 0:
24 printf("this is not a work day\n");
25 break;
26 default:
27 printf("this day does not exist\n");
28 }
29 }
结果:
0 this is not a work day
- 程序
1 #include <stdio.h>
2
3 void bubblesort(int v[], int n) {
4 int i, j;
5 for( i = n-1; i >= 0; i--)
6 for(j = 0; j < i; j++)
7 if(v[j] > v[j+1]) {
8 int temp = v[j];
9 v[j] = v[j+1];
10 v[j+1] = temp;
11 }
12 }
13
14 int main() {
15 int i;
16 int s[] = { 5, 4, 3, 2, 1};
17 bubblesort(s, 5);
18 for(i = 0; i < 5; i++)
19 printf("%3d", s[i]);
20 }
结果
1 2 3 4 5
- 程序
结果
6 8
程序填空题,按要求填写程序
- 打印输入的单词长度的直方图
1 #include <stdio.h>
2 #define MAXLENGTH 20
3
4 main() {
5 int c, i, len, max, nlength[MAXLENGTH+1];
6 for(i = 0; i <= MAXLENGTH; i++)
7 ___________________
8 max = 0;
9 len = 0;
10 while (__(c=getchar()) != EOF__) {
11 if( c != ' ' && c!= '\n' && c != '\t') {
12 ++len;
13 } else if(len != 0) {
14 if(len <= MAXLENGTH) {
15 __________________
16 if(len > max)
17 max = len;
18 }
19 len = 0;
20 }
21 }
22 for(i = 1; ___________; i++) {
23 int j = 0;
24 printf("length:%4d count:%4d ", i, nlength[i]);
25 for(j = 0; ________________; j++)
26 putchar('=');
27 putchar('\n');
28 }
29 }
程序输入:
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 #include <stdio.h>
2
3 void swap(int v[], int i, int j);
4 void qsort(int v[], int left, int right);
5
6 int main() {
7 int i;
8 int v[] = {15, 12, 31, 21 ,45};
9 qsort(_____________);
10 for(i = 0; i < 5; i++)
11 printf("%4d", v[i]);
12 }
13
14 void qsort(int v[], int left, int right) {
15 int i, last;
16 if(_______________)
17 return;
18 swap(v, left, (left+right)/2);
19 last = left;
20 for(i = left + 1; i <= right; i++)
21 if(v[i] < v[left])
22 swap(v, ++last, i);
23 swap(v, left, last);
24 qsort(_____________________);
25 qsort(_____________________);
26 }
27
28 void swap(int v[], int i, int j) {
29 ___________;
30 v[i] = v[j];
31 v[j] = temp;
32 }
程序输出结果
12 15 21 31 45
编程题,按要求编写程序
- 编写一个ackerman函数,这个函数用于计算如下数学公式。编写一个main函数计算并输出A(3, 4)的值
- 编写一个程序,将输入中的水平制表符替换成适当数目的空格,使空格充满到下一个制表符终止位。假设制表符终止位的位置是固定的,每隔10列就会出现一个制表符终止位。
1 #include<stdio.h>
2 #include<stdlib.h>
3 #define TABSIZE 10
4
5 main()
6 {
7 int c, i;
8 int count = 0;
9 while((c=getchar())!=EOF) {
10 if(c=='\t') {
11 int space = TABSIZE - count % TABSIZE;
12 for(i = 0; i < space; i++) {
13 putchar(' ');
14 count ++;
15 }
16 } else if (c == '\n'){
17 putchar(c);
18 count = 0;
19 } else {
20 putchar(c);
21 count ++;
22 }
23 }
24 }