※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

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.
  1. int strlen(const char *str) {
  2. int c = 0;
  3. while (str[c] != 0 ) c++;
  4. return c;
  5. }
  6.  
  7. void puts (const char *msg) {
  8. write(1, msg , strlen(msg)) ;
  9. write(1, "\n" , 1) ;
  10. }
  11.  
  12. void main(){
  13. puts("こんちは。");
  14. }
  15.  
Exercise 1-2. Experiment to find out what happens when printf's argument string contains \c, where c is some character not listed above.
  1. #include <stdio.h>
  2. int main ( void )
  3. {
  4. printf("tab\t tab\t tab\n");
  5. printf("beep!\a\n");
  6. return 0;
  7. }
  8.  
Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
  1. #include <stdio.h>
  2. int main ( void )
  3. {
  4. int lower, upper, step;
  5. float fahr, celsius;
  6.  
  7. lower = 0;
  8. upper = 300;
  9. step = -20;
  10.  
  11. fahr = upper;
  12. while ( fahr >= lower ) {
  13. celsius = (5.0/9.0) * ( fahr - 32.0 );
  14. printf("%4.0f %6.1f\n", fahr, celsius);
  15. fahr = fahr + step;
  16. }
  17. }
  18.  
Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
  1. int main(){
  2. int lower, upper, step;
  3. float fahr, celsius;
  4.  
  5. lower = 0;
  6. upper = 300;
  7. step = 20;
  8.  
  9. celsius = lower;
  10. while ( celsius <= upper ) {
  11. fahr = (celsius + 32.0) * (9.0/5.0);
  12. printf("%6.f %4.1f\n", celsius, fahr);
  13. celsius = celsius + step;
  14. }
  15. }
  16.  
Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
  1. #include <stdio.h>
  2. int main ( void )
  3. {
  4. int lower, upper, step;
  5. float fahr, celsius;
  6.  
  7. lower = 0;
  8. upper = 300;
  9. step = -20;
  10.  
  11. fahr = upper;
  12. while ( fahr >= lower ) {
  13. celsius = (5.0/9.0) * ( fahr - 32.0 );
  14. printf("%4.0f %6.1f\n", fahr, celsius);
  15. fahr = fahr + step;
  16. }
  17. }
  18.  
Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
  1. int main () {
  2. while ( (getchar() != EOF) == 1 ) {
  3. printf("1");
  4. }
  5. printf("0");
  6.  
Exercise 1-7. Write a program to print the value of EOF.
  1. int main() {
  2. printf("EOF= %d\n",EOF);
  3. }
  4.  
Exercise 1-8. Write a program to count blanks, tabs, and newlines.
  1. int main(){
  2. int c;
  3. int cnt_sp, cnt_nl, cnt_tab;
  4.  
  5. cnt_sp=0; cnt_nl=0; cnt_tab=0;
  6.  
  7. while ( ( c = getchar() ) != EOF ) {
  8. switch(c) {
  9. case '\n': cnt_nl++; break;
  10. case '\t': cnt_tab++; break;
  11. case ' ': cnt_sp++; break;
  12. }
  13. }
  14. printf("\n count:NL=%d, TAB=%d, SPACE=%d\n", cnt_nl, cnt_tab, cnt_sp);
  15. }
  16.  
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.
  1. int main() {
  2. int c;
  3. while( (c=getchar()) != EOF ) {
  4. putchar(c);
  5. putchar(' ');
  6. }
  7. }
  8.  
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.
  1. int main() {
  2. int c;
  3. while ( (c=getchar()) != EOF ) {
  4. switch(c){
  5. case '\t': printf("\\t"); break;
  6. case '\b': printf("\\\\"); break;
  7. default: putchar(c);
  8. }
  9. }
  10. }
  11.  
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?
  1. int wordcount() {
  2. int c, ret, count_on;
  3. ret = 0;
  4. count_on = 0; // OFF = 0, ON = 1 以上
  5.  
  6. while ( (c=getchar()) != EOF ) {
  7. if ( 126 >= c && c >= 33) { // ASCII CODE で制御文字 以外
  8. if ( count_on == 0 ) ret++;
  9. count_on++;
  10. } else count_on = 0;
  11. }
  12. return ret;
  13. }
  14.  
  15. int main() {
  16. printf("word count = %d", wordcount());
  17. }
  18.  
Exercise 1-12. Write a program that prints its input one word per line.
  1. int wordperline(){
  2. int c, ret, count_on;
  3. ret = 0;
  4. count_on = 0; // OFF = 0, ON = 1 以上
  5.  
  6. while ( (c=getchar()) != EOF ) {
  7. if ( 126 >= c && c >= 33) { // ASCII CODE で制御文字 以外
  8. putchar(c);
  9. count_on++;
  10. } else {
  11. if ( count_on > 0) putchar('\n');
  12. count_on=0;
  13. }
  14. }
  15. return ret;
  16. }
  17. int main() {
  18. wordperline();
  19. }
  20.  
  21.  
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.
  1.  
Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.
  1.  
Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
  1.  
Exercise 1-16. Revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text.
  1.  
Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
  1.  
Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.
  1.  
Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time.
  1.  
Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?
  1.  
Exercise 1-21. Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?
  1.  
Exercise 1-22. Write a program to ``fold'' long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
  1.  
Exercise 1-23. Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments don't nest.
  1.  
Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unmatched parentheses,brackets and braces. Don't forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.)
  1.  

名前:
コメント: