TableOfContents

找出以下程序中的错误并作出解释

  1. 输出hello, world并换行

       1 include <stdio>
       2 main {
       3     printf(hello, world/n)
       4 }
    
  2. 打印华氏温度与摄氏温度的对照表,输出摄氏度保留一位小数

       1 #include "stdio.h"
       2 main()
       3 {
       4     lower = 0;
       5     upper = 300;
       6     fahr = lower;
       7     while( fahr <= upper ) {
       8         celsius = 5 * (fahr - 32)/9;
       9         printf("%3.0f %6.1f\n", fahr, celsius);
      10         fahr += step;
      11     }
      12 }
    
  3. 单词计数

       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 }
    
  4. 求指数

       1 #include <stdio.h>
       2 main() {
       3     int i;
       4     for(i = 0, i < 10, i++)
       5         printf("%d %f %f\n", i, power(2.0f, i), power(-3.0f, i));
       6     return 0;
       7 }
       8 float power(base, n) 
       9 {
      10     int i;
      11     float p;
      12     for(i = 1; i < n; ++i)
      13         p *= base;
      14     return p;
      15 }
    
  5. 读取字符串并求字符串长度

       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 }
    
  6. 栈的数据定义和操作

       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 }
    
  7. 递归函数打印一个整数

       1 #include <stdio.h>
       2 void printd(int n) {
       3     printd(n / 10);
       4     putchar(n % 10 + '0');
       5 }
       6 
       7 main()
       8 {
       9     int i = 1234;
      10     printd(i);
      11 }
    
  8. 读取整数的函数

       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.    1 #include <stdio.h>
       2 int dec(int *a) {
       3     return (*a)--;  
       4 }
       5 int inc(int a) {
       6     return a++;
       7 }
       8 main() {
       9     int x = 1, y;
      10     y = dec( &x );
      11     y = inc(x);
      12     printf("%d\n%d\n", x, y);
      13 
      14 }
    
  2.    1 #include <stdio.h>
       2 #define min(x,y) (((x)<(y))?(x):(y))
       3 #ifdef EOF
       4 #define square(x)  x*x
       5 #else
       6 #define square(x)  ((x)*(x))
       7 #endif
       8 int main() {
       9     int a = 1, b = 2;
      10     printf("%d\n", square(a+b));
      11     min(a++, b++);
      12     printf("%d\n ", min(a, b));
      13 }
    
  3.    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 }
    
  4. 假定int, unsigned是32位的。

       1 #include <stdio.h>
       2 int bitcount(unsigned x) {
       3     int b;
       4     x = ~x;
       5     for(b = 0; x != 0; x >>= 1)
       6         if(x & 1)
       7             b++;
       8     return b;
       9 }
      10 main() {
      11     printf("%d\n%d\n%d\n", bitcount( ~ 0xA), bitcount(017), bitcount(255) );
      12 }
    
  5.    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 }
    
  6.    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 }
    
  7.    1 #include <stdio.h>
       2 #define INITVAL 5
       3 int count() {
       4     static int c = INITVAL;
       5     return c--;
       6 }
       7 main() {
       8     int i;
       9     while(i = count() )
      10         printf("%d\n", i);
      11 }
    
  8.    1 #include <stdio.h>
       2 double mod(double x, double y) {
       3         return  x - (int)( x / y ) * y;
       4 }
       5 main() {
       6     printf("%6.1f\n%6.1f\n", mod(23, 3), mod(-36.5, 7.5) );
       7 }
    

编程题,按要求编写程序

  1. 分别编写递归方式和非递归方式的reverse(s)函数,用于将字符串s倒过来
  2. 分别编写递归方式和非递归方式的fact(n)函数,用于计算n的阶乘n!
  3. 分别编写递归方式和非递归方式的combination(m, n),用于计算组合数
  4. 写一个程序统计输入的空格数,tab数,和换行数。
  5. 编写一个程序,把输入拷贝到输出,把其中连续的多个空格替换成一个空格。
  6. 编写一个程序,把输入中长度超过80个字符的行输出。
  7. 编写一个程序,把输入中每一行末尾的空格或者tab删除。
  8. 编写一个程序,将输入中的水平制表符替换成适当数目的空格,使空格充满到下一个制表符终止位。假设制表符终止位的位置是固定的,每隔10列就会出现一个制表符终止位。
  9. 编写一个程序,把超过50个字符的输入行折成短一些的两行或多行,折行的位置在输入行的第50、100、150……列的位置。
  10. 编写一个程序,把输入拷贝到输出,并替换退格符为\b,替换其中的tab为\t,替换反斜杠为\\,使得原来不可见的字符变得可见。

读程序,写出程序运行的结果

  1.    1 #include <stdio.h>
       2 int min(int a, int b) {
       3     return a < b ? a : b;  
       4 }
       5 main() {
       6     int x = -1, y = -2;
       7     printf("%d", min(x, y));
       8 }
    
  2.    1 #include <stdio.h>
       2 int inc(int a) {
       3     return ++a;  
       4 }
       5 main() {
       6     int x = -1, y;
       7     y = inc(x);
       8     printf("%d\n%d", x, y);
       9 }
    
  3.    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 }
    
  4.    1 #include <stdio.h>
       2 int fibonacci(int a) {
       3     int i, curr = 1, next = 1, temp;
       4     for( i = 1; i < a; i++) {
       5         temp = next + curr;
       6         curr = next;
       7         next = temp;
       8     }
       9     return curr;   
      10 }
      11 main() {
      12     int i;
      13     for(i = 1; i < 10; i++)
      14         printf("%d\t", fibonacci(i));
      15 }
    
  5.    1 #include <stdio.h>
       2 main() {
       3     int x = 2, y = 1; /*assume int is 32-bit*/
       4     printf("%d", (~x | ~y) + (x ^ y) );
       5 }
    
  6.    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!

  7.    1 #include <stdio.h>
       2 
       3 main() {
       4     int x, y;
       5     x = 0!=4<=1+2*3/4<<1;
       6     y = (1 << 2 | 0xF0 ) ^ 071 & 8;
       7     printf("%d\n%d", x, y);
       8 }
    
  8.    1 #include <stdio.h>
       2 #define PI 3.14
       3 main() {
       4     double r;
       5     for( r  = 1.0; r <= 4.5; r += 1.0) {
       6         printf("%6.0f%6.2f%6.2f\n", r, PI*r*2, PI*r*r);
       7 }
       8 }
    
  9.    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 }
    
  10.    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;

按要求编写程序

  1. 写一个函数reverse(s),将字符串s反转。比如

    char s[]="hello"; 
    reverse(s);
    这时s变成"olleh"
  2. 写一个函数invert(x,p,n),这个函数将整数x从右数第p位开始的n位反转(0变1,1变0),并将这个值返回。比如:invert(0x72, 4, 3)将返回0x6E.
  3. 请写一个程序分别统计输入当中空格、水平制表符(tab)和换行符(newline)的个数,即程序运行后输入任意一些字符,以Ctrl+Z结束输入,程序输出这些字符中空格的个数、水平制表符的个数以及换行符的个数。
ch3n2k.com | Copyright (c) 2004-2020 czk.