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. #include<stdio.h>
  2.  
  3. #define Kyou int
  4. #define No0 main(){
  5. #define Boku printf(
  6. #define No1 "Hello"
  7. #define Bangohann " World¥n"
  8. #define Ha );
  9. #define Chicken return
  10. #define Curry 0;}
  11.  
  12. Kyou No0
  13.  
  14. Boku No1 Bangohann Ha
  15.  
  16. Chicken Curry
  17.  
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("hello world¥c");
  5. return 0;
  6. }
  7.  
Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
  1. #include<stdio.h>
  2.  
  3. int main(void)
  4. {
  5. float fahr, celsius;
  6. int lower, upper, step;
  7.  
  8. lower = 0;
  9. upper = 300;
  10. step = 20;
  11.  
  12. fahr = lower;
  13. puts("----------------");
  14. puts("| F | C |");
  15. puts("----------------");
  16. while(fahr <= upper){
  17. celsius = (5.0/9.0) * (fahr - 32.0);
  18. printf("| %3.0f | %6.1f |¥n", fahr, celsius);
  19. fahr = fahr + step;
  20. }
  21. puts("----------------");
  22. }
  23.  
Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
  1. #include<stdio.h>
  2.  
  3. int main(void)
  4. {
  5. float fahr, celsius;
  6. int lower, upper, step;
  7.  
  8. lower = -17;
  9. upper = 148;
  10. step = 20;
  11.  
  12. celsius = lower;
  13. puts("----------------");
  14. puts("| C | F |");
  15. puts("----------------");
  16. while(celsius <= upper){
  17. fahr = (9.0/5.0)*celsius + 32.0;
  18. printf("| %3.0f | %6.1f |¥n", celsius, fahr);
  19. celsius += step;
  20. }
  21. puts("----------------");
  22. }
  23.  
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.  
  3. int main(void)
  4. {
  5. float fahr, celsius;
  6. int lower, upper, step;
  7.  
  8. lower = 0;
  9. upper = 300;
  10. step = 20;
  11.  
  12. fahr = upper;
  13. puts("----------------");
  14. puts("| F | C |");
  15. puts("----------------");
  16. while(fahr >= lower){
  17. celsius = (5.0/9.0) * (fahr - 32.0);
  18. printf("| %3.0f | %6.1f |¥n", fahr, celsius);
  19. fahr = fahr - step;
  20. }
  21. puts("----------------");
  22. }
  23.  
Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. int value = 0;
  5. do{
  6. printf("getchar() != EOF : %d¥n", (value = getchar()) != EOF);
  7. }while(value != EOF);
  8. return 0;
  9. }
  10.  
Exercise 1-7. Write a program to print the value of EOF.
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. char value = getchar();
  5. printf("EOF: d: %d c: %c ¥n", value, value);
  6. return 0;
  7. }
  8.  
Exercise 1-8. Write a program to count blanks, tabs, and newlines.
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. int tabs = 0, spaces = 0, returns = 0;
  5. char c;
  6. while( (c = getchar()) != EOF)
  7. {
  8. if(c == '¥n')
  9. returns++;
  10. if(c == '¥t')
  11. tabs++;
  12. if(c == ' ')
  13. spaces++;
  14. }
  15. printf("TAB: %d times ¥n"
  16. "SPACE: %d times ¥n"
  17. "RETURN: %d times ¥n", tabs, spaces, returns);
  18. return 0;
  19. }
  20.  
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. #include<stdio.h>
  2. int main(void)
  3. {
  4. char c;
  5. while((c = getchar()) != EOF)
  6. {
  7. if(c == ' '){
  8. putchar(' ');
  9. while((c = getchar()) == ' '){}
  10. if(c != EOF)
  11. putchar(c);
  12. }
  13. putchar(c);
  14. }
  15. return 0;
  16. }
  17.  
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. #include<stdio.h>
  2. int main(void)
  3. {
  4. char c;
  5. while((c = getchar()) != EOF)
  6. {
  7. if(c == '¥b'){
  8. printf("¥¥b");
  9. continue;
  10. }
  11. if(c == '¥t'){
  12. printf("¥¥t");
  13. continue;
  14. }
  15. if(c == '¥¥'){
  16. printf("¥¥¥¥");
  17. continue;
  18. }
  19. putchar(c);
  20. }
  21. return 0;
  22. }
  23.  
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. I think that the program have to be examined with the set of sentence we already counted it up.
  2. It's of course, needless to say, the set of sentence have to be contained with all the factors that the sentence would have, spaces, newlines,etc...
  3.  
Exercise 1-12. Write a program that prints its input one word per line.
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. char c;
  5. while((c = getchar()) != EOF)
  6. {
  7. if(c == '¥n' || c == '¥t' || c == ' ')
  8. {
  9. puts("");
  10. while((c = getchar()) == '¥n' || c == '¥t' || c == ' '){}
  11. }
  12. putchar(c);
  13. }
  14. return 0;
  15. }
  16.  
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. #include<stdio.h>
  2. #define WORD_LENGTH_MAX 20
  3. int main(void){
  4. char c;
  5. int i;
  6. int finish_flag = 0;
  7. int length_counter = 0;
  8. int words[WORD_LENGTH_MAX] = {0};
  9. while((c = getchar()) != EOF){
  10. if( c == '\n' || c == '\t' || c == ' '){
  11. if(length_counter == 0){
  12. continue;
  13. }
  14. words[length_counter]++;
  15. length_counter = 0;
  16. continue;
  17. }
  18. length_counter++;
  19. }
  20. for(i = 0;i < WORD_LENGTH_MAX; i++){
  21. printf("%3d", i);
  22. }
  23. while(!finish_flag){
  24. finish_flag = 1;
  25. puts("");
  26. for(i = 0;i < WORD_LENGTH_MAX; i++){
  27. if(words[i]-- > 0){
  28. finish_flag = 0;
  29. printf(" *");
  30. }
  31. else
  32. {
  33. printf(" ");
  34. }
  35. }
  36. }
  37. puts("");
  38. return 0;
  39. }
  40.  
Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.
  1. #include<stdio.h>
  2.  
  3. int main(){
  4. char c;
  5. int i;
  6. int char_count[128] = {0};
  7. while(( c = getchar()) != EOF){
  8. char_count[(int)c]++;
  9. }
  10. for(i = 0;i < 128; i++){
  11. printf("%c: ", i);
  12. while(char_count[i]-- > 0)
  13. printf(" * ");
  14. puts("");
  15. }
  16. }
  17.  
Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
  1. #include<stdio.h>
  2. #define LOWER 0
  3. #define UPPER 300
  4. #define STEP 20
  5. int conversion(int fahr)
  6. {
  7. return 5 * (fahr - 32) / 9;
  8. }
  9.  
  10. int main(void)
  11. {
  12. int fahr;
  13. fahr = LOWER;
  14. printf(" F C \n");
  15. while(fahr <= UPPER){
  16. printf(" %3d %3d \n", fahr, conversion(fahr));
  17. fahr += STEP;
  18. }
  19. }
  20.  
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. #include<stdio.h>
  2. #define MAXLINE 1000
  3.  
  4. int getline(char line[],int maxline);
  5.  
  6. main()
  7. {
  8. int len,max;
  9. int target;
  10. char line[MAXLINE], longest[MAXLINE];
  11.  
  12. max = 0;
  13. printf("input the number of the words you want to fetch\n");
  14. scanf("%d", &target);
  15. while((len = getline(line, MAXLINE)) != -1 )
  16. if(len == target)
  17. printf("%d letters: %s", len, line);
  18. }
  19. int getline(char s[], int lim)
  20. {
  21. int c,i;
  22. for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c!='\n';++i)
  23. s[i] = c;
  24. if(c == '\n')
  25. {
  26. s[i] = c;
  27. ++i;
  28. }
  29. s[i] = '\0';
  30. if(c != EOF)
  31. return i-1;
  32. else
  33. return -1;
  34. }
  35.  
Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
  1. #include<stdio.h>
  2. #define MAXLINE 1000
  3. #define WORDSPERLINE 500
  4.  
  5. typedef struct{
  6. char letters[WORDSPERLINE];
  7. int wordsCounter;
  8. }WORD;
  9. int getline(char array[]);
  10.  
  11. int main(){
  12. WORD lp80[MAXLINE];
  13. int i = 0,j;
  14.  
  15. while((lp80[i].wordsCounter = getline(lp80[i].letters)) > 0){
  16. if(lp80[i].wordsCounter >= 80)
  17. i++;
  18. }
  19. for(j = 0; j < i; j++){
  20. printf("%d letters : %s\n", lp80[j].wordsCounter, lp80[j].letters);
  21. }
  22. }
  23.  
  24. int getline(char array[])
  25. {
  26. char c;
  27. int i = 0,count = 0;
  28.  
  29. while((c = getchar()) != EOF && c != '\n'){
  30. array[i++] = c;
  31. count++;
  32. }
  33. return count;
  34. }
  35.  
Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.
  1. #include<stdio.h>
  2. #define MAXLINE 1000
  3. #define WORDSPERLINE 500
  4.  
  5. typedef struct{
  6. char letters[WORDSPERLINE];
  7. int wordsCounter;
  8. }WORD;
  9. int getline(char array[]);
  10. int backSearch(WORD text[],int i);
  11. int finish_flag = 0;
  12.  
  13. int main(){
  14. WORD text[MAXLINE];
  15. int ai;
  16. int i = 0,j;
  17.  
  18. while((text[i].wordsCounter = getline(text[i].letters)) >= 0){
  19. if(finish_flag == 1)
  20. break;
  21. if(text[i].wordsCounter >= 1)
  22. i++;
  23. }
  24. ai = backSearch(text, i);
  25. puts("---------------------------RESULT----------------------------");
  26. for(j = 0; j < ai; j++){
  27. printf("%d letters : %s\n", text[j].wordsCounter, text[j].letters);
  28. }
  29. }
  30.  
  31. int getline(char array[])
  32. {
  33. char c;
  34. int i = 0,count = 0;
  35.  
  36. while((c = getchar()) != EOF && c != '\n'){
  37. array[i++] = c;
  38. count++;
  39. }
  40. if(c == EOF)
  41. finish_flag = 1;
  42. return count;
  43. }
  44. int backSearch(WORD text[],int i){
  45. int j,k;
  46. for(j = i-1; j >= 0; j--){
  47. for(k = text[j].wordsCounter - 1; k >= 0; k--){
  48. if(text[j].letters[k] != '\n' && text[j].letters[k] != '\t')
  49. break;
  50. if(--text[j].wordsCounter == 0)
  51. i--;
  52. }
  53. }
  54. return i;
  55. }
  56.  
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. #include<stdio.h>
  2. #include<string.h>
  3. #define MAXLETTERS 500
  4. void reverse(char s[]);
  5.  
  6. int main(void){
  7. char sentence[MAXLETTERS];
  8. char letter_checked;
  9. char reversed[MAXLETTERS];
  10. int i = 0, num;
  11. while((letter_checked = getchar()) != EOF || (i == MAXLETTERS - 1)){
  12. sentence[i++] = letter_checked;
  13. }
  14. sentence[i++] = '\0';
  15. printf("%s", sentence);
  16. reverse(sentence);
  17. printf("%s", sentence);
  18. }
  19.  
  20. void reverse(char s[]){
  21. char reversed[MAXLETTERS];
  22. int i;
  23. int last_point;
  24. for(i = 0; i < MAXLETTERS; i++){
  25. if(s[i] == '\0'){
  26. last_point = i-2;
  27. break;
  28. }
  29. }
  30. for(i = last_point; i >= 0; i--){
  31. reversed[i] = s[last_point - i];
  32. }
  33. reversed[last_point + 1] = '\0';
  34. strcpy(s, reversed);
  35. return;
  36. }
  37.  
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. #include<stdio.h>
  2. #include<string.h>
  3. #define WORD_MAX 1000
  4. #define TABtoSPACE 1
  5. void detab(char s[]);
  6.  
  7. int main(void){
  8. char strings[WORD_MAX];
  9. char check_c;
  10. int i = 0;
  11. while((check_c = getchar()) != EOF)
  12. strings[i++] = check_c;
  13. strings[i] = '\0';
  14. detab(strings);
  15. printf("%s\n", strings);
  16. }
  17.  
  18. void detab(char s[]){
  19. int i = 0,j,new = 0;
  20. char fixedOne[WORD_MAX];
  21. while(i < WORD_MAX-1 && s[i] != '\0'){
  22. if(s[i] == '\t'){
  23. for(j = 0;j < TABtoSPACE; j++)
  24. fixedOne[new++] = ' ';
  25. i++;
  26. }
  27. fixedOne[new++] = s[i++];
  28. }
  29. fixedOne[new] = '\0';
  30. strcpy(s, fixedOne);
  31. }
  32.  
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. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX_WORD 500
  4. void entab(char s[]);
  5.  
  6. int main(void){
  7. char string[500];
  8. int i = 0;
  9. char check_c;
  10. while((check_c = getchar()) != EOF && i < 500)
  11. string[i++] = check_c;
  12. string[i] = '\0';
  13. entab(string);
  14. printf("%s\n", string);
  15. }
  16.  
  17. void entab(char s[]){
  18. int i = 0, j = 0;
  19. char fixedOne[MAX_WORD];
  20. while(s[i] != '\0'){
  21. if(s[i] == ' '){
  22. fixedOne[j++] = s[i];
  23. while(s[++i] == ' '){}
  24. }else if(s[i] == '\t'){
  25. fixedOne[j++] = s[i];
  26. while(s[++i] == '\t'){}
  27. }else
  28. fixedOne[j++] = s[i++];
  29. }
  30. fixedOne[j] = '\0';
  31. strcpy(s, fixedOne);
  32. }
  33.  
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. #include<stdio.h>
  2. #define MAX_WORD 500
  3. #define FOLD_POINT 20
  4. #define MAX_ROW 10
  5. void fold(char s[]);
  6.  
  7. int main(void){
  8. char string[MAX_WORD];
  9. int i = 0;
  10. int check_c;
  11. while((check_c = getchar()) != EOF)
  12. string[i++] = check_c;
  13. string[i] = '\0';
  14.  
  15. fold(string);
  16. }
  17. void fold(char s[]){
  18.  
  19. int blank_point = -1;
  20. int flag = 0;
  21. int i = 0, j = 0, row = 0, p = 0;
  22. char foldedString[MAX_ROW][MAX_WORD + 1];
  23.  
  24. while(s[i] != '\0'){ //find fold_point?
  25. if(s[i] == '\t' || s[i] == ' '){
  26. blank_point = j;
  27. foldedString[row][j++] = s[i++];
  28. }else if(s[i] == '\n'){
  29. foldedString[row++][j] = '\0';
  30. j = 0;
  31. flag = 0;
  32. blank_point = -1;
  33. }
  34. if(j == FOLD_POINT){
  35. if(blank_point != -1){
  36. flag = 1;
  37. foldedString[row][blank_point] = '~';
  38. // foldedString[row][MAX_WORD] = '1';
  39. }
  40. }
  41. foldedString[row][j++] = s[i++];
  42. }
  43. //deal with backwards
  44. while(p <= row){
  45. for(i = 0; foldedString[p][i] != '~' && i < MAX_WORD; i++){}
  46. foldedString[p++][++i] = '\0';
  47. }
  48. puts("-----------------------------------------------------");
  49. for(p = 0; p <= row; p++)
  50. printf("%s\n", foldedString[p]);
  51. }
  52.  
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. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX_CHAR 500
  4.  
  5. void removeComment(char s[]);
  6.  
  7. int main(void){
  8. char string[MAX_CHAR];
  9. char check_c;
  10. int i = 0;
  11. while((check_c = getchar()) != EOF)
  12. string[i++] = check_c;
  13. string[i] = '\0';
  14.  
  15. removeComment(string);
  16. printf("%s\n", string);
  17. }
  18. void removeComment(char s[]){
  19. int i = 0,j = 0;
  20. char removed[MAX_CHAR];
  21. while(s[i] != '\0'){
  22. if(s[i] == '/' && s[i+1] == '/'){
  23. i += 2;
  24. while(s[i] != '\n'){i++;}
  25. removed[j++] = '\n';
  26. }else if(s[i] == '/' && s[i+1] == '*'){
  27. i += 2;
  28. while(s[i] != '*' || s[i+1] != '/'){i++;}
  29. i += 2;
  30. }else
  31. removed[j++] = s[i++];
  32. }
  33. removed[j] = '\0';
  34. strcpy(s, removed);
  35. }
  36.  
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. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX_CHAR 500
  4. void removeComment(char s[]);
  5. int parenthis_check(char s[]);
  6. int main(void){
  7. int i = 0;
  8. char check_c, string[MAX_CHAR];
  9. while((check_c = getchar()) != EOF)
  10. string[i++] = check_c;
  11. string[i] = '\0';
  12.  
  13. removeComment(string);
  14. if(parenthis_check(string))
  15. printf("syntax error\n");
  16. else
  17. printf("Correct\n");
  18. printf("%s\n", string);
  19. }
  20. void removeComment(char s[]){
  21. int i = 0,j = 0;
  22. char removed[MAX_CHAR];
  23. while(s[i] != '\0'){
  24. if(s[i] == '/' && s[i+1] == '/'){
  25. i += 2;
  26. while(s[i] != '\n'){i++;}
  27. removed[j++] = '\n';
  28. }else if(s[i] == '/' && s[i+1] == '*'){
  29. i += 2;
  30. while(s[i] != '*' || s[i+1] != '/'){i++;}
  31. i += 2;
  32. }else
  33. removed[j++] = s[i++];
  34. }
  35. removed[j] = '\0';
  36. strcpy(s, removed);
  37. }
  38. int parenthis_check(char s[]){
  39. int i = 0, q_p = 50,init;
  40. char queue[100];
  41. int error_flag = 0;
  42. for(init = 0;init < 100; init++)
  43. queue[init] = '0';
  44. while(s[i] != '\0'){
  45. if(s[i] == '(')
  46. queue[q_p++] = s[i++];
  47. else if(s[i] == '[')
  48. queue[q_p++] = s[i++];
  49. else if(s[i] == '{')
  50. queue[q_p++] = s[i++];
  51. else if(s[i] == '<')
  52. queue[q_p++] = s[i++];
  53. else if(s[i] == ')'){
  54. i++;
  55. if('(' != queue[--q_p]){
  56. error_flag = 1;
  57. break;
  58. }
  59. }
  60. else if(s[i] == ']'){
  61. i++;
  62. if('[' != queue[--q_p]){
  63. error_flag = 1;
  64. break;
  65. }
  66. }
  67. else if(s[i] == '}'){
  68. i++;
  69. if('{' != queue[--q_p]){
  70. error_flag = 1;
  71. break;
  72. }
  73. }
  74. else if(s[i] == '>'){
  75. i++;
  76. if('<' != queue[--q_p]){
  77. error_flag = 1;
  78. break;
  79. }
  80. }else
  81. i++;
  82. }
  83. if(error_flag) return 1;
  84. return 0;
  85. }
  86.  

名前:
コメント:
最終更新:2009年11月09日 08:38