此页代码仅供学习C语言参考之用,以[http://www.gnu.org/copyleft/gpl.html GPL]协议发布。

   1 /*Exercise 1-1. Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get.*/
   2 #include <stdio.h>
   3 
   4 main()
   5 {
   6     printf("hello, world\n");
   7 }

   1 /*Exercise 1-2. Experiment to find out what happens when prints's argument string contains \c, where c is some character not listed above.*/
   2 #include <stdio.h>
   3 
   4 main()
   5 {
   6     printf("\a");
   7 }

   1 /*Exercise 1-3. Modify the temperature conversion program to print a heading above the table.*/
   2 #include <stdio.h>
   3 
   4 /* print Fahrenheit-Celsius table
   5 for fahr = 0, 20, ..., 300; floating-point version */
   6 main()
   7 {
   8     float fahr, celsius;
   9     int lower, upper, step;
  10     
  11     lower = 0; /* lower limit of temperatuire scale */
  12     upper = 300; /* upper limit */
  13     step = 20; /* step size */
  14     
  15     printf("fahr celsius\n");
  16     
  17     fahr = lower;
  18     while (fahr <= upper) {
  19         celsius = (5.0/9.0) * (fahr-32.0);
  20         printf("%4.0f %7.1f\n", fahr, celsius);
  21         fahr = fahr + step;
  22     }
  23 }

   1 /*Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.*/
   2 #include <stdio.h>
   3 
   4 /* print Celsius-Fahrenheit table
   5 for celsius = 0, 20, ..., 300; floating-point version */
   6 main()
   7 {
   8     float fahr, celsius;
   9     int lower, upper, step;
  10     
  11     lower = 0; /* lower limit of temperatuire scale */
  12     upper = 300; /* upper limit */
  13     step = 20; /* step size */
  14     
  15     celsius = lower;
  16     while (celsius <= upper) {
  17         fahr =  celsius * (9.0/5.0) + 32.0;
  18         printf("%3.0f %6.1f\n", celsius, fahr);
  19         celsius = celsius + step;
  20     }
  21 }

   1 /*Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.*/
   2 #include <stdio.h>
   3 
   4 /* print Fahrenheit-Celsius table
   5 for fahr = 300, 280, ..., 0; floating-point version */
   6 main()
   7 {
   8     int fahr;
   9    
  10     for(fahr = 300; fahr >= 0; fahr = fahr - 20)
  11         printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
  12 }

   1 /*Exercsise 1-6. Verify that the expression getchar() != EOF is 0 or 1.*/
   2 #include <stdio.h>
   3 
   4 /* Verify that the expression getchar() != EOF is 0 or 1. */
   5 main()
   6 {
   7     int c;
   8     
   9     while(c = (getchar() !=EOF))
  10         printf("%d\n", c);
  11     printf("%d\n", c);
  12 }

   1 /*Exercsise 1-6. Verify that the expression getchar() != EOF is 0 or 1.*/
   2 #include <stdio.h>
   3 
   4 /* Write a program to print the value of EOF. */
   5 main()
   6 {
   7     printf("%d\n", EOF);
   8 }

   1 /*Exercise 1-8. Write a program to count blanks, tabs, and newlines.*/
   2 #include <stdio.h>
   3 
   4 /* Write a program to count blanks, tabs, and newlines. */
   5 main()
   6 {
   7     int c, nl, nb, nt;
   8     nl = 0;
   9     nb = 0;
  10     nt = 0;
  11     while ((c = getchar()) != EOF)
  12         if (c == '\n')
  13             ++nl;
  14         else if(c == '\t')
  15             ++nt;
  16         else if(c == ' ')
  17             ++nb;
  18     printf("%d %d %d\n", nb, nt, nl);
  19 }

   1 /*Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.*/
   2 #include <stdio.h>
   3 
   4 #define TRUE 1
   5 #define FALSE 0
   6 
   7 /* Write a program to copy its input to its output, replacing each string of one or
   8 more blanks by a single blank. */
   9 main()
  10 {
  11     int c, is_blank;
  12     while ((c = getchar()) != EOF)
  13         if (c != ' ') {
  14             putchar(c);
  15             is_blank = FALSE;
  16         }
  17         else if(c == ' ' && !last_blank) {
  18             putchar(c);
  19             is_blank = FALSE;
  20         }
  21 }

   1 /*Exercise 1-10. Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way.*/
   2 #include <stdio.h>
   3 
   4 #define TRUE 1
   5 #define FALSE 0
   6 
   7 /* Write a program to copy its input to its output, replacing each tab by \t, each
   8 backspace by \b, and each backslash by \\. This makes tabs and backspaces visible in an
   9 unambiguous way. */
  10 main()
  11 {
  12     int c;
  13     while ((c = getchar()) != EOF)
  14         if (c == '\t') {
  15             putchar('\\');
  16             putchar('t');
  17         } else if(c == '\b') {
  18             putchar('\\');
  19             putchar('b');
  20         } else if(c == '\\') {
  21             putchar('\\');
  22             putchar('\\');
  23         } else {
  24             putchar(c);
  25         }
  26 }

   1 /*Exercise 1-11. How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?*/
   2 #include <stdio.h>
   3 #define IN 1 /* inside a word */
   4 #define OUT 0 /* outside a word */
   5 
   6 /* count lines, words, and characters in input */
   7 main()
   8 {
   9     int c, nl, nw, nc, state;
  10     state = OUT;
  11     nl = nw = nc = 0;
  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 /*Exercise 1-12. Write a program that prints its input one word per line.*/
   2 #include <stdio.h>
   3 #define IN 1 /* inside a word */
   4 #define OUT 0 /* outside a word */
   5 
   6 /* Write a program that prints its input one word per line. */
   7 main()
   8 {
   9     int c, state;
  10     state = OUT;
  11     while ((c = getchar()) != EOF) {
  12         if( c != ' ' && c!= '\n' && c != '\t') {
  13             state = IN;
  14             putchar(c);
  15         } else if(state == IN) {
  16             state = OUT;
  17             putchar('\n');
  18         }
  19     }
  20     printf("%d %d %d\n", nl, nw, nc);
  21 }

   1 /*Exercise 1-13. Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.*/
   2 #include <stdio.h>
   3 #define MAXLENGTH 20
   4 
   5 /* Write a program to print a histogram of the lengths of words in its input. It is
   6 easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging. */
   7 main()
   8 {
   9     int c, i, len, nlength[MAXLENGTH+1], state, max;
  10     for(i = 0; i <= MAXLENGTH; i++)
  11         nlength[i] = 0;
  12     while ((c = getchar()) != EOF) {
  13         if( c != ' ' && c!= '\n' && c != '\t') {
  14             ++len;
  15         } else if(len != 0) {
  16             if(len <= MAXLENGTH)
  17                 ++nlength[len];
  18             len = 0;
  19         }
  20     }
  21     for(i = 1; i <= MAXLENGTH; i++) {
  22         int j = 0;
  23         printf("%4d %4d ", i, nlength[i]);
  24         for(j = 0; j < nlength[i]; j++)
  25             putchar('=');
  26         putchar('\n');
  27     }
  28     
  29     max = 0;
  30     for(i = 1; i <= MAXLENGTH; i++)
  31         if(nlength[i] > max)
  32             max = nlength[i];
  33     for(len = max; len >=1; len--) {
  34         for(i = 1; i <= MAXLENGTH; i++)
  35             printf("%3c", nlength[i] >= len ? '|' : ' ');
  36         printf("\n");
  37     }
  38     for(i = 1; i <= MAXLENGTH; i++)
  39         printf("%3d", i);
  40     printf("\n");      
  41 }
ch3n2k.com | Copyright (c) 2004-2020 czk.