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

  • you made it. -- ushiku (2009-11-20 16:53:01)
名前:
コメント:
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. main()
  4. {
  5. printf("hello, ");
  6. printf("world");
  7. printf("\n");
  8. }
  9.  
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.  
  3. main()
  4. {
  5. printf("hello\c, wo\cl\cd\c\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. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. float fahr, celsius;
  7. int lower, upper, step;
  8.  
  9. lower = 0;
  10. upper = 300;
  11. step = 20;
  12.  
  13. fahr = lower;
  14. printf("fahr celsius\n");
  15. while(fahr <= upper) {
  16. celsius = (5.0/9.0) * (fahr-32.0);
  17. printf("%3.0f %6.1f\n", fahr, celsius);
  18. fahr = fahr + step;
  19. }
  20. return 0;
  21. }
  22.  
Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main()
  5. {
  6. float fahr, celsius;
  7. int lower, upper, step;
  8.  
  9. lower = -20;
  10. upper = 150;
  11. step = 20;
  12.  
  13. printf("celsius fahr\n");
  14. celsius = lower;
  15. while(celsius <= upper) {
  16. fahr = (9.0/5.0)*celsius + 32.0;
  17. printf("%6.1f %3.0f\n", celsius, fahr);
  18. celsius = celsius + step;
  19. }
  20.  
  21. return 0;
  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. #include <stdlib.h>
  3.  
  4. int pr1_5()
  5. {
  6. float fahr, celsius;
  7. int lower, upper, step;
  8.  
  9. lower = 0;
  10. upper = 300;
  11. step = 20;
  12.  
  13. fahr = upper;
  14. printf("fahr celsius\n");
  15. while(lower <= fahr) {
  16. celsius = (5.0/9.0) * (fahr-32.0);
  17. printf("%3.0f %6.1f\n", fahr, celsius);
  18. fahr = fahr - step;
  19. }
  20. return 0;
  21. }
  22.  
Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int pr1_6() {
  5. int c;
  6.  
  7. while(1)
  8. printf("%d\n", getchar() != EOF);
  9.  
  10. return 0;
  11. }
  12.  
Exercise 1-7. Write a program to print the value of EOF.
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int pr1_7()
  5. {
  6. printf("%d\n", EOF);
  7.  
  8. return 0;
  9. }
  10.  
Exercise 1-8. Write a program to count blanks, tabs, and newlines.
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int pr1_8()
  5. {
  6. int c, nl;
  7. int nTab, nSpace;
  8.  
  9. nl = 0;
  10. nSpace = 0;
  11. nTab = 0;
  12. while((c = getchar()) != EOF) {
  13. if(c == '\n')
  14. ++nl;
  15. if(c == ' ')
  16. ++nSpace;
  17. if(c == '\t')
  18. ++nTab;
  19. }
  20. printf("number of line: %d\n", nl);
  21. printf("number of space: %d\n", nSpace);
  22. printf("number of tab: %d\n", nTab);
  23.  
  24. return 0;
  25. }
  26.  
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. #include<stdlib.h>
  3.  
  4. int pr1_9()
  5. {
  6. int c;
  7.  
  8. while((c = getchar()) != EOF)
  9. if(c == ' ') {
  10. while((c = getchar()) == ' ')
  11. ;
  12. ungetc(c, stdin);
  13. putchar(' ');
  14. } else
  15. putchar(c);
  16.  
  17. return 0;
  18. }
  19.  
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. #include<stdlib.h>
  3.  
  4. int pr1_10()
  5. {
  6. int c;
  7.  
  8. while((c = getchar()) != EOF) {
  9. if(c == '\t')
  10. printf("\\t");
  11. else if (c == '\b')
  12. printf("\\b");
  13. else if (c == '\\')
  14. printf("\\\\");
  15. else
  16. putchar(c);
  17. }
  18.  
  19. return 0;
  20. }
  21.  
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. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define IN 1
  5. #define OUT 0
  6.  
  7. int pr1_11()
  8. {
  9. int c, nl, nw, nc, state;
  10.  
  11. state = OUT;
  12. nl = nw = nc = 0;
  13. while((c = getchar()) != EOF) {
  14. ++nc;
  15. if(c == '\n')
  16. ++nl;
  17. if(c == ' ' || c == '\n' || c == '\t')
  18. state = OUT;
  19. else if (state == OUT) {
  20. state = IN;
  21. ++nw;
  22. }
  23. }
  24. printf("nl:%d nw:%d nc:%d\n", nl, nw, nc);
  25.  
  26. return 0;
  27. }
  28.  
Exercise 1-12. Write a program that prints its input one word per line.
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. enum {
  5. IN = 1,
  6. OUT = 0
  7. };
  8.  
  9. int pr1_12()
  10. {
  11. int c, nl, nw, nc, state;
  12.  
  13. state = OUT;
  14. nl = nw = nc = 0;
  15. while((c = getchar()) != EOF) {
  16. ++nc;
  17. if(c == '\n')
  18. ++nl;
  19. if(c == ' ' || c == '\n' || c == '\t') {
  20. state = OUT;
  21. printf("\n");
  22. } else {
  23. putchar(c);
  24.  
  25. if(state == OUT) {
  26. state = IN;
  27. ++nw;
  28. }
  29. }
  30. }
  31. printf("nl:%d nw:%d nc:%d\n", nl, nw, nc);
  32.  
  33. return 0;
  34. }
  35.  
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. #include<stdlib.h>
  3.  
  4. enum {
  5. IN = 1,
  6. OUT = 0
  7. };
  8.  
  9. void printStar(int num);
  10.  
  11. int pr1_13()
  12. {
  13. int c, nl, nw, nc, state;
  14. int sw;
  15. int nWordCount[100];
  16. int i;
  17.  
  18. for(i = 0; i < 10; i++)
  19. nWordCount[i] = 0;
  20. state = OUT;
  21. nl = nw = nc = 0;
  22. sw = 0; //start word
  23. while((c = getchar()) != EOF) {
  24. ++nc;
  25. if(c == '\n')
  26. ++nl;
  27. if(c == ' ' || c == '\n' || c == '\t') {
  28. state = OUT;
  29. nWordCount[nc-sw]++;
  30. }
  31. else if (state == OUT) {
  32. state = IN;
  33. sw = nc;
  34. ++nw;
  35. }
  36. }
  37. printf("nl:%d nw:%d nc:%d\n", nl, nw, nc);
  38.  
  39. for(i = 0; i < 10; i++) {
  40. printf("[%d]: ", i);
  41. printStar(nWordCount[i]);
  42. }
  43.  
  44. return 0;
  45. }
  46.  
  47. void printStar(int num)
  48. {
  49. int i;
  50. for(i = 0; i < num; i++)
  51. printf("*");
  52. printf("\n");
  53. }
  54.  
Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.
  1. ginclude<stdio.h>
  2. #include<string.h>
  3. enum { MAX_BUFF = 128 };
  4.  
  5. int printHist(int *histArray, int n)
  6. {
  7. int ch,j;
  8.  
  9. for(ch = ' '; ch < n; ch++) {
  10. printf("%c: ", ch);
  11. for(j = 0; j < histArray[ch]; j++) printf("*");
  12. printf("\n");
  13. }
  14.  
  15. return 0;
  16. }
  17.  
  18. int pr1_14()
  19. {
  20. int ch,i;
  21. int buffer[MAX_BUFF];
  22.  
  23. for(i = 0; i < MAX_BUFF; i++) buffer[i] = 0;
  24.  
  25. while((ch = getchar()) != EOF) {
  26. buffer[ch]++;
  27. }
  28.  
  29. printHist(buffer, MAX_BUFF);
  30.  
  31. return 0;
  32. }
  33.  
Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
  1. #include<stdio.h>
  2.  
  3.  
  4. int conversionTemperature(int fahr)
  5. {
  6. return 5 * (fahr-32) / 9;
  7. }
  8.  
  9. int pr1_15()
  10. {
  11. int fahr, celsius;
  12. int lower, upper, step;
  13.  
  14. lower = 0;
  15. upper = 300;
  16. step = 20;
  17.  
  18. fahr = lower;
  19. while(fahr <= upper) {
  20. celsius = conversionTemperature(fahr);
  21. printf("%d\t%d\n", fahr, celsius);
  22. fahr = fahr + step;
  23. }
  24. }
  25.  
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. void copy(char to[], char from[]);
  6.  
  7. int pr1_16()
  8. {
  9. int len,index;
  10. int arbitaryLenth;
  11. char line[MAXLINE];
  12. char longest[MAXLINE][MAXLINE] = {NULL};
  13. int i;
  14.  
  15. index = 0;
  16. arbitaryLenth = 0;
  17. printf("input arbitaryLength: ");
  18. scanf("%d ", &arbitaryLenth);
  19. while((len = getline(line, MAXLINE)) > 0)
  20. if(len == arbitaryLenth) {
  21. copy(longest[index], line);
  22. index++;
  23. }
  24. for(i = 0; i < index; i++)
  25. printf("%s", longest[i]);
  26. return 0;
  27. }
  28.  
  29. int getline(char s[], int lim)
  30. {
  31. int c, i;
  32.  
  33. for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
  34. s[i] = c;
  35. if (c == '\n') {
  36. s[i] = c;
  37. ++i;
  38. }
  39. s[i] = '\0';
  40. return i;
  41. }
  42.  
  43. void copy(char to[], char from[])
  44. {
  45. int i;
  46.  
  47. i = 0;
  48. while((to[i] = from[i]) != '\0')
  49. ++i;
  50. }
  51.  
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.  
  4. int getline(char line[], int maxline);
  5.  
  6. //pr1_17.c: output line where length of line is over 80 character
  7. int pr1_17()
  8. {
  9. int len;
  10. int threshold;
  11. char line[MAXLINE];
  12.  
  13. threshold = 80;
  14. while((len = getline(line, MAXLINE)) > 0)
  15. if(len > threshold) {
  16. printf("%s", line);
  17. }
  18.  
  19. return 0;
  20. }
  21.  
  22. static int getline(char s[], int lim)
  23. {
  24. int c, i;
  25.  
  26. for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
  27. s[i] = c;
  28. if (c == '\n') {
  29. s[i] = c;
  30. ++i;
  31. }
  32. s[i] = '\0';
  33. return i;
  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.  
  4. int getline(char line[], int maxline);
  5.  
  6. //pr1_18: output line which removed trailing blancs and tabs from each line of input,
  7. // and output line which is no entirely blank lines.
  8. int pr1_18()
  9. {
  10. int len;
  11. int i;
  12. char line[MAXLINE];
  13.  
  14. while((len = getline(line, MAXLINE)) > 0) {
  15. for(i = len-1; (line[i] == ' ' || line[i] == '\t' || line[i] == '\n') && i > 0; i--)
  16. ;
  17. if(i > 0 ) {
  18. line[i+1] = '\n';
  19. line[i+2] = '\0';
  20. printf("%s", line);
  21. }
  22. }
  23.  
  24. return 0;
  25. }
  26.  
  27. static int getline(char s[], int lim)
  28. {
  29. int c, i;
  30.  
  31. for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
  32. s[i] = c;
  33. if (c == '\n') {
  34. s[i] = c;
  35. ++i;
  36. }
  37. s[i] = '\0';
  38. return i;
  39. }
  40.  
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.  
  3. static enum { MAX_BUFF = 256 };
  4.  
  5. int length(char *str)
  6. {
  7. int nchar;
  8. nchar = 0;
  9. while(*str++ != '\0')
  10. nchar++;
  11. return nchar;
  12. }
  13.  
  14. int reverse(char *str)
  15. {
  16. int i,j,num;
  17. char revStr[MAX_BUFF];
  18. //printf("%s", str);
  19. //printf("%d\n", length(str));
  20.  
  21. j = 0;
  22. for(i = length(str)-2; i >= 0; i--) {
  23. revStr[j] = str[i];
  24. j++;
  25. }
  26. revStr[j] = '\n';
  27. revStr[j+1] = '\0';
  28.  
  29. num = length(str);
  30. for(i = 0; i < num; i++) {
  31. str[i] = revStr[i];
  32. }
  33.  
  34. return 0;
  35. }
  36.  
  37. int pr1_19()
  38. {
  39. int nc;
  40. char str[MAX_BUFF];
  41. int ch;
  42.  
  43. //printf("%d\n", MAX_BUFF);
  44. for(nc=0;(ch = getchar()) != EOF && nc < MAX_BUFF; nc++) {
  45. str[nc] = ch;
  46. if(ch == '\n') {
  47. str[nc+1] = '\0';
  48. reverse(str);
  49. printf("%s", str);
  50. nc = -1;
  51. }
  52. }
  53.  
  54. return 0;
  55. }
  56.  
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.  
  3. int pr1_20()
  4. {
  5. int ch;
  6.  
  7. while((ch = getchar()) != EOF) {
  8. if(ch == '\t') {
  9. printf(" ");
  10. } else {
  11. putchar(ch);
  12. }
  13. }
  14. return 0;
  15. }
  16.  
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.  
  3. //entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing.
  4. int pr1_21()
  5. {
  6. int ch;
  7. int nwh;
  8. int nt;
  9. int nb;
  10. int i;
  11.  
  12. while((ch = getchar()) != EOF) {
  13. if(ch == ' ') {
  14. nwh = 1;
  15. while((ch = getchar()) == ' ' && ch != EOF) { nwh++; }
  16. nt = nwh / 8;
  17. nb = nwh % 8;
  18. for(i = 0; i < nt; i++) {
  19. printf("\t");
  20. //printf("t");
  21. }
  22. for(i = 0; i < nb; i++) {
  23. printf(" ");
  24. //printf("w");
  25. }
  26. }
  27. putchar(ch);
  28. }
  29. }
  30.  
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.  
  3. int pr1_22()
  4. {
  5. int ch, n;
  6. int nc;
  7. n = 5;
  8.  
  9. nc = 0;
  10. while((ch = getchar()) != EOF) {
  11. if(ch != ' ') {
  12. nc++;
  13. if(nc == n) {
  14. putchar('\n');
  15. nc = 1;
  16. }
  17. }
  18. putchar(ch);
  19. }
  20. return 0;
  21. }
  22.  
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.  
  3. //skip comment
  4. int pr1_23()
  5. {
  6. int ch;
  7.  
  8. while((ch = getchar()) != EOF) {
  9. if(ch == '/') {
  10. ch = getchar();
  11. if(ch == '*') {
  12. while((ch = getchar()) != '*' && ch != EOF)
  13. ;
  14. while((ch = getchar()) != '/' && ch != EOF)
  15. ;
  16. if(ch == '/') {
  17. ch = getchar();
  18. } else {
  19. return 1;
  20. }
  21. }
  22. if(ch == '/') {
  23. while((ch = getchar()) != '\n' && ch != EOF)
  24. ;
  25. if(ch == EOF) {
  26. return 1;
  27. }
  28. }
  29. }
  30. putchar(ch);
  31. }
  32.  
  33. return 0;
  34. }
  35.  
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.  
  3. int pr1_24()
  4. {
  5. int ch;
  6. int parenthese_flag;
  7. int brackets_flag;
  8. int braces_flag;
  9.  
  10. parenthese_flag = 0;
  11. brackets_flag = 0;
  12. braces_flag = 0;
  13. while((ch = getchar()) != EOF) {
  14. //check parenthese
  15. if(ch == '(') {
  16. parenthese_flag++;
  17. }
  18. if(ch == ')') {
  19. parenthese_flag--;
  20. }
  21.  
  22. //check brachets
  23. if(ch == '{') {
  24. brackets_flag++;
  25. }
  26. if(ch == '}') {
  27. brackets_flag--;
  28. }
  29.  
  30. //check braces
  31. if(ch == '[') {
  32. braces_flag++;
  33. }
  34. if(ch == ']') {
  35. braces_flag--;
  36. }
  37.  
  38. //check comment
  39. if(ch == '/') {
  40. ch = getchar();
  41. if(ch == '*') {
  42. while((ch = getchar()) != '*' && ch != EOF)
  43. ;
  44. while((ch = getchar()) != '/' && ch != EOF)
  45. ;
  46. if(ch == '/') {
  47. ch = getchar();
  48. } else {
  49. return 1;
  50. }
  51. }
  52. if(ch == '/') {
  53. while((ch = getchar()) != '\n' && ch != EOF)
  54. ;
  55. if(ch == EOF) {
  56. return 1;
  57. }
  58. }
  59. }
  60.  
  61. //output
  62. //putchar(ch);
  63. }
  64.  
  65. if(parenthese_flag != 0) {
  66. printf("unmatch parenthese\n");
  67. return 1;
  68. }
  69. if(brackets_flag != 0) {
  70. printf("unmatch brackets\n");
  71. return 1;
  72. }
  73. if(braces_flag != 0) {
  74. printf("unmatch braces\n");
  75. return 1;
  76. }
  77.  
  78. return 0;
  79. }
  80.  

  • test -- test (2009-11-03 10:50:44)
名前:
コメント: