「C1_koizumi」の編集履歴(バックアップ)一覧はこちら

C1_koizumi」(2009/11/20 (金) 16:53:01) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

#comment() &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> main() { printf("hello, "); printf("world"); printf("\n"); } }} &bold(){Exercise 1-2.} Experiment to find out what happens when printf's argument string contains \c, where c is some character not listed above. #highlight(linenumber,c){{ #include<stdio.h> main() { printf("hello\c, wo\cl\cd\c\n"); return 0; } }} &bold(){Exercise 1-3.} Modify the temperature conversion program to print a heading above the table. #highlight(linenumber,c){{ #include <stdio.h> #include <stdlib.h> int main() { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; printf("fahr celsius\n"); while(fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } return 0; } }} &bold(){Exercise 1-4.} Write a program to print the corresponding Celsius to Fahrenheit table. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int main() { float fahr, celsius; int lower, upper, step; lower = -20; upper = 150; step = 20; printf("celsius fahr\n"); celsius = lower; while(celsius <= upper) { fahr = (9.0/5.0)*celsius + 32.0; printf("%6.1f %3.0f\n", celsius, fahr); celsius = celsius + step; } return 0; } }} &bold(){Exercise 1-5.} Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. #highlight(linenumber,c){{ #include <stdio.h> #include <stdlib.h> int pr1_5() { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = upper; printf("fahr celsius\n"); while(lower <= fahr) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr - step; } return 0; } }} &bold(){Exercise 1-6.} Verify that the expression getchar() != EOF is 0 or 1. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_6() { int c; while(1) printf("%d\n", getchar() != EOF); return 0; } }} &bold(){Exercise 1-7.} Write a program to print the value of EOF. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_7() { printf("%d\n", EOF); return 0; } }} &bold(){Exercise 1-8.} Write a program to count blanks, tabs, and newlines. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_8() { int c, nl; int nTab, nSpace; nl = 0; nSpace = 0; nTab = 0; while((c = getchar()) != EOF) { if(c == '\n') ++nl; if(c == ' ') ++nSpace; if(c == '\t') ++nTab; } printf("number of line: %d\n", nl); printf("number of space: %d\n", nSpace); printf("number of tab: %d\n", nTab); return 0; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_9() { int c; while((c = getchar()) != EOF) if(c == ' ') { while((c = getchar()) == ' ') ; ungetc(c, stdin); putchar(' '); } else putchar(c); return 0; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_10() { int c; while((c = getchar()) != EOF) { if(c == '\t') printf("\\t"); else if (c == '\b') printf("\\b"); else if (c == '\\') printf("\\\\"); else putchar(c); } return 0; } }} &bold(){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? #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> #define IN 1 #define OUT 0 int pr1_11() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while((c = getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("nl:%d nw:%d nc:%d\n", nl, nw, nc); return 0; } }} &bold(){Exercise 1-12.} Write a program that prints its input one word per line. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> enum { IN = 1, OUT = 0 }; int pr1_12() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while((c = getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') { state = OUT; printf("\n"); } else { putchar(c); if(state == OUT) { state = IN; ++nw; } } } printf("nl:%d nw:%d nc:%d\n", nl, nw, nc); return 0; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> enum { IN = 1, OUT = 0 }; void printStar(int num); int pr1_13() { int c, nl, nw, nc, state; int sw; int nWordCount[100]; int i; for(i = 0; i < 10; i++) nWordCount[i] = 0; state = OUT; nl = nw = nc = 0; sw = 0; //start word while((c = getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') { state = OUT; nWordCount[nc-sw]++; } else if (state == OUT) { state = IN; sw = nc; ++nw; } } printf("nl:%d nw:%d nc:%d\n", nl, nw, nc); for(i = 0; i < 10; i++) { printf("[%d]: ", i); printStar(nWordCount[i]); } return 0; } void printStar(int num) { int i; for(i = 0; i < num; i++) printf("*"); printf("\n"); } }} &bold(){Exercise 1-14.} Write a program to print a histogram of the frequencies of different characters in its input. #highlight(linenumber,c){{ ginclude<stdio.h> #include<string.h> enum { MAX_BUFF = 128 }; int printHist(int *histArray, int n) { int ch,j; for(ch = ' '; ch < n; ch++) { printf("%c: ", ch); for(j = 0; j < histArray[ch]; j++) printf("*"); printf("\n"); } return 0; } int pr1_14() { int ch,i; int buffer[MAX_BUFF]; for(i = 0; i < MAX_BUFF; i++) buffer[i] = 0; while((ch = getchar()) != EOF) { buffer[ch]++; } printHist(buffer, MAX_BUFF); return 0; } }} &bold(){Exercise 1-15.} Rewrite the temperature conversion program of Section 1.2 to use a function for conversion. #highlight(linenumber,c){{ #include<stdio.h> int conversionTemperature(int fahr) { return 5 * (fahr-32) / 9; } int pr1_15() { int fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; while(fahr <= upper) { celsius = conversionTemperature(fahr); printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); void copy(char to[], char from[]); int pr1_16() { int len,index; int arbitaryLenth; char line[MAXLINE]; char longest[MAXLINE][MAXLINE] = {NULL}; int i; index = 0; arbitaryLenth = 0; printf("input arbitaryLength: "); scanf("%d ", &arbitaryLenth); while((len = getline(line, MAXLINE)) > 0) if(len == arbitaryLenth) { copy(longest[index], line); index++; } for(i = 0; i < index; i++) printf("%s", longest[i]); return 0; } int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } void copy(char to[], char from[]) { int i; i = 0; while((to[i] = from[i]) != '\0') ++i; } }} &bold(){Exercise 1-17.} Write a program to print all input lines that are longer than 80 characters. #highlight(linenumber,c){{ #include<stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); //pr1_17.c: output line where length of line is over 80 character int pr1_17() { int len; int threshold; char line[MAXLINE]; threshold = 80; while((len = getline(line, MAXLINE)) > 0) if(len > threshold) { printf("%s", line); } return 0; } static int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } }} &bold(){Exercise 1-18.} Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. #highlight(linenumber,c){{ #include<stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); //pr1_18: output line which removed trailing blancs and tabs from each line of input, // and output line which is no entirely blank lines. int pr1_18() { int len; int i; char line[MAXLINE]; while((len = getline(line, MAXLINE)) > 0) { for(i = len-1; (line[i] == ' ' || line[i] == '\t' || line[i] == '\n') && i > 0; i--) ; if(i > 0 ) { line[i+1] = '\n'; line[i+2] = '\0'; printf("%s", line); } } return 0; } static int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> static enum { MAX_BUFF = 256 }; int length(char *str) { int nchar; nchar = 0; while(*str++ != '\0') nchar++; return nchar; } int reverse(char *str) { int i,j,num; char revStr[MAX_BUFF]; //printf("%s", str); //printf("%d\n", length(str)); j = 0; for(i = length(str)-2; i >= 0; i--) { revStr[j] = str[i]; j++; } revStr[j] = '\n'; revStr[j+1] = '\0'; num = length(str); for(i = 0; i < num; i++) { str[i] = revStr[i]; } return 0; } int pr1_19() { int nc; char str[MAX_BUFF]; int ch; //printf("%d\n", MAX_BUFF); for(nc=0;(ch = getchar()) != EOF && nc < MAX_BUFF; nc++) { str[nc] = ch; if(ch == '\n') { str[nc+1] = '\0'; reverse(str); printf("%s", str); nc = -1; } } return 0; } }} &bold(){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? #highlight(linenumber,c){{ #include<stdio.h> int pr1_20() { int ch; while((ch = getchar()) != EOF) { if(ch == '\t') { printf(" "); } else { putchar(ch); } } return 0; } }} &bold(){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? #highlight(linenumber,c){{ #include<stdio.h> //entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. int pr1_21() { int ch; int nwh; int nt; int nb; int i; while((ch = getchar()) != EOF) { if(ch == ' ') { nwh = 1; while((ch = getchar()) == ' ' && ch != EOF) { nwh++; } nt = nwh / 8; nb = nwh % 8; for(i = 0; i < nt; i++) { printf("\t"); //printf("t"); } for(i = 0; i < nb; i++) { printf(" "); //printf("w"); } } putchar(ch); } } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> int pr1_22() { int ch, n; int nc; n = 5; nc = 0; while((ch = getchar()) != EOF) { if(ch != ' ') { nc++; if(nc == n) { putchar('\n'); nc = 1; } } putchar(ch); } return 0; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> //skip comment int pr1_23() { int ch; while((ch = getchar()) != EOF) { if(ch == '/') { ch = getchar(); if(ch == '*') { while((ch = getchar()) != '*' && ch != EOF) ; while((ch = getchar()) != '/' && ch != EOF) ; if(ch == '/') { ch = getchar(); } else { return 1; } } if(ch == '/') { while((ch = getchar()) != '\n' && ch != EOF) ; if(ch == EOF) { return 1; } } } putchar(ch); } return 0; } }} &bold(){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.) #highlight(linenumber,c){{ #include<stdio.h> int pr1_24() { int ch; int parenthese_flag; int brackets_flag; int braces_flag; parenthese_flag = 0; brackets_flag = 0; braces_flag = 0; while((ch = getchar()) != EOF) { //check parenthese if(ch == '(') { parenthese_flag++; } if(ch == ')') { parenthese_flag--; } //check brachets if(ch == '{') { brackets_flag++; } if(ch == '}') { brackets_flag--; } //check braces if(ch == '[') { braces_flag++; } if(ch == ']') { braces_flag--; } //check comment if(ch == '/') { ch = getchar(); if(ch == '*') { while((ch = getchar()) != '*' && ch != EOF) ; while((ch = getchar()) != '/' && ch != EOF) ; if(ch == '/') { ch = getchar(); } else { return 1; } } if(ch == '/') { while((ch = getchar()) != '\n' && ch != EOF) ; if(ch == EOF) { return 1; } } } //output //putchar(ch); } if(parenthese_flag != 0) { printf("unmatch parenthese\n"); return 1; } if(brackets_flag != 0) { printf("unmatch brackets\n"); return 1; } if(braces_flag != 0) { printf("unmatch braces\n"); return 1; } return 0; } }} - test -- test (2009-11-03 10:50:44) #comment()
- you made it. -- ushiku (2009-11-20 16:53:01) #comment() &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> main() { printf("hello, "); printf("world"); printf("\n"); } }} &bold(){Exercise 1-2.} Experiment to find out what happens when printf's argument string contains \c, where c is some character not listed above. #highlight(linenumber,c){{ #include<stdio.h> main() { printf("hello\c, wo\cl\cd\c\n"); return 0; } }} &bold(){Exercise 1-3.} Modify the temperature conversion program to print a heading above the table. #highlight(linenumber,c){{ #include <stdio.h> #include <stdlib.h> int main() { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; printf("fahr celsius\n"); while(fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } return 0; } }} &bold(){Exercise 1-4.} Write a program to print the corresponding Celsius to Fahrenheit table. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int main() { float fahr, celsius; int lower, upper, step; lower = -20; upper = 150; step = 20; printf("celsius fahr\n"); celsius = lower; while(celsius <= upper) { fahr = (9.0/5.0)*celsius + 32.0; printf("%6.1f %3.0f\n", celsius, fahr); celsius = celsius + step; } return 0; } }} &bold(){Exercise 1-5.} Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. #highlight(linenumber,c){{ #include <stdio.h> #include <stdlib.h> int pr1_5() { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = upper; printf("fahr celsius\n"); while(lower <= fahr) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr - step; } return 0; } }} &bold(){Exercise 1-6.} Verify that the expression getchar() != EOF is 0 or 1. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_6() { int c; while(1) printf("%d\n", getchar() != EOF); return 0; } }} &bold(){Exercise 1-7.} Write a program to print the value of EOF. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_7() { printf("%d\n", EOF); return 0; } }} &bold(){Exercise 1-8.} Write a program to count blanks, tabs, and newlines. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_8() { int c, nl; int nTab, nSpace; nl = 0; nSpace = 0; nTab = 0; while((c = getchar()) != EOF) { if(c == '\n') ++nl; if(c == ' ') ++nSpace; if(c == '\t') ++nTab; } printf("number of line: %d\n", nl); printf("number of space: %d\n", nSpace); printf("number of tab: %d\n", nTab); return 0; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_9() { int c; while((c = getchar()) != EOF) if(c == ' ') { while((c = getchar()) == ' ') ; ungetc(c, stdin); putchar(' '); } else putchar(c); return 0; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> int pr1_10() { int c; while((c = getchar()) != EOF) { if(c == '\t') printf("\\t"); else if (c == '\b') printf("\\b"); else if (c == '\\') printf("\\\\"); else putchar(c); } return 0; } }} &bold(){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? #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> #define IN 1 #define OUT 0 int pr1_11() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while((c = getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("nl:%d nw:%d nc:%d\n", nl, nw, nc); return 0; } }} &bold(){Exercise 1-12.} Write a program that prints its input one word per line. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> enum { IN = 1, OUT = 0 }; int pr1_12() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while((c = getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') { state = OUT; printf("\n"); } else { putchar(c); if(state == OUT) { state = IN; ++nw; } } } printf("nl:%d nw:%d nc:%d\n", nl, nw, nc); return 0; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> #include<stdlib.h> enum { IN = 1, OUT = 0 }; void printStar(int num); int pr1_13() { int c, nl, nw, nc, state; int sw; int nWordCount[100]; int i; for(i = 0; i < 10; i++) nWordCount[i] = 0; state = OUT; nl = nw = nc = 0; sw = 0; //start word while((c = getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') { state = OUT; nWordCount[nc-sw]++; } else if (state == OUT) { state = IN; sw = nc; ++nw; } } printf("nl:%d nw:%d nc:%d\n", nl, nw, nc); for(i = 0; i < 10; i++) { printf("[%d]: ", i); printStar(nWordCount[i]); } return 0; } void printStar(int num) { int i; for(i = 0; i < num; i++) printf("*"); printf("\n"); } }} &bold(){Exercise 1-14.} Write a program to print a histogram of the frequencies of different characters in its input. #highlight(linenumber,c){{ ginclude<stdio.h> #include<string.h> enum { MAX_BUFF = 128 }; int printHist(int *histArray, int n) { int ch,j; for(ch = ' '; ch < n; ch++) { printf("%c: ", ch); for(j = 0; j < histArray[ch]; j++) printf("*"); printf("\n"); } return 0; } int pr1_14() { int ch,i; int buffer[MAX_BUFF]; for(i = 0; i < MAX_BUFF; i++) buffer[i] = 0; while((ch = getchar()) != EOF) { buffer[ch]++; } printHist(buffer, MAX_BUFF); return 0; } }} &bold(){Exercise 1-15.} Rewrite the temperature conversion program of Section 1.2 to use a function for conversion. #highlight(linenumber,c){{ #include<stdio.h> int conversionTemperature(int fahr) { return 5 * (fahr-32) / 9; } int pr1_15() { int fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; while(fahr <= upper) { celsius = conversionTemperature(fahr); printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); void copy(char to[], char from[]); int pr1_16() { int len,index; int arbitaryLenth; char line[MAXLINE]; char longest[MAXLINE][MAXLINE] = {NULL}; int i; index = 0; arbitaryLenth = 0; printf("input arbitaryLength: "); scanf("%d ", &arbitaryLenth); while((len = getline(line, MAXLINE)) > 0) if(len == arbitaryLenth) { copy(longest[index], line); index++; } for(i = 0; i < index; i++) printf("%s", longest[i]); return 0; } int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } void copy(char to[], char from[]) { int i; i = 0; while((to[i] = from[i]) != '\0') ++i; } }} &bold(){Exercise 1-17.} Write a program to print all input lines that are longer than 80 characters. #highlight(linenumber,c){{ #include<stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); //pr1_17.c: output line where length of line is over 80 character int pr1_17() { int len; int threshold; char line[MAXLINE]; threshold = 80; while((len = getline(line, MAXLINE)) > 0) if(len > threshold) { printf("%s", line); } return 0; } static int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } }} &bold(){Exercise 1-18.} Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. #highlight(linenumber,c){{ #include<stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); //pr1_18: output line which removed trailing blancs and tabs from each line of input, // and output line which is no entirely blank lines. int pr1_18() { int len; int i; char line[MAXLINE]; while((len = getline(line, MAXLINE)) > 0) { for(i = len-1; (line[i] == ' ' || line[i] == '\t' || line[i] == '\n') && i > 0; i--) ; if(i > 0 ) { line[i+1] = '\n'; line[i+2] = '\0'; printf("%s", line); } } return 0; } static int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> static enum { MAX_BUFF = 256 }; int length(char *str) { int nchar; nchar = 0; while(*str++ != '\0') nchar++; return nchar; } int reverse(char *str) { int i,j,num; char revStr[MAX_BUFF]; //printf("%s", str); //printf("%d\n", length(str)); j = 0; for(i = length(str)-2; i >= 0; i--) { revStr[j] = str[i]; j++; } revStr[j] = '\n'; revStr[j+1] = '\0'; num = length(str); for(i = 0; i < num; i++) { str[i] = revStr[i]; } return 0; } int pr1_19() { int nc; char str[MAX_BUFF]; int ch; //printf("%d\n", MAX_BUFF); for(nc=0;(ch = getchar()) != EOF && nc < MAX_BUFF; nc++) { str[nc] = ch; if(ch == '\n') { str[nc+1] = '\0'; reverse(str); printf("%s", str); nc = -1; } } return 0; } }} &bold(){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? #highlight(linenumber,c){{ #include<stdio.h> int pr1_20() { int ch; while((ch = getchar()) != EOF) { if(ch == '\t') { printf(" "); } else { putchar(ch); } } return 0; } }} &bold(){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? #highlight(linenumber,c){{ #include<stdio.h> //entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. int pr1_21() { int ch; int nwh; int nt; int nb; int i; while((ch = getchar()) != EOF) { if(ch == ' ') { nwh = 1; while((ch = getchar()) == ' ' && ch != EOF) { nwh++; } nt = nwh / 8; nb = nwh % 8; for(i = 0; i < nt; i++) { printf("\t"); //printf("t"); } for(i = 0; i < nb; i++) { printf(" "); //printf("w"); } } putchar(ch); } } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> int pr1_22() { int ch, n; int nc; n = 5; nc = 0; while((ch = getchar()) != EOF) { if(ch != ' ') { nc++; if(nc == n) { putchar('\n'); nc = 1; } } putchar(ch); } return 0; } }} &bold(){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. #highlight(linenumber,c){{ #include<stdio.h> //skip comment int pr1_23() { int ch; while((ch = getchar()) != EOF) { if(ch == '/') { ch = getchar(); if(ch == '*') { while((ch = getchar()) != '*' && ch != EOF) ; while((ch = getchar()) != '/' && ch != EOF) ; if(ch == '/') { ch = getchar(); } else { return 1; } } if(ch == '/') { while((ch = getchar()) != '\n' && ch != EOF) ; if(ch == EOF) { return 1; } } } putchar(ch); } return 0; } }} &bold(){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.) #highlight(linenumber,c){{ #include<stdio.h> int pr1_24() { int ch; int parenthese_flag; int brackets_flag; int braces_flag; parenthese_flag = 0; brackets_flag = 0; braces_flag = 0; while((ch = getchar()) != EOF) { //check parenthese if(ch == '(') { parenthese_flag++; } if(ch == ')') { parenthese_flag--; } //check brachets if(ch == '{') { brackets_flag++; } if(ch == '}') { brackets_flag--; } //check braces if(ch == '[') { braces_flag++; } if(ch == ']') { braces_flag--; } //check comment if(ch == '/') { ch = getchar(); if(ch == '*') { while((ch = getchar()) != '*' && ch != EOF) ; while((ch = getchar()) != '/' && ch != EOF) ; if(ch == '/') { ch = getchar(); } else { return 1; } } if(ch == '/') { while((ch = getchar()) != '\n' && ch != EOF) ; if(ch == EOF) { return 1; } } } //output //putchar(ch); } if(parenthese_flag != 0) { printf("unmatch parenthese\n"); return 1; } if(brackets_flag != 0) { printf("unmatch brackets\n"); return 1; } if(braces_flag != 0) { printf("unmatch braces\n"); return 1; } return 0; } }} - test -- test (2009-11-03 10:50:44) #comment()

表示オプション

横に並べて表示:
変化行の前後のみ表示: