From 49891f6bce29843f503b437c84d83cb787ecfa87 Mon Sep 17 00:00:00 2001 From: Zhineng Li Date: Sat, 10 Jan 2026 16:37:39 +0800 Subject: first commit --- ch01/ex01-hello.c | 6 ++++++ ch01/ex02-exp.c | 6 ++++++ ch01/ex03-temp.c | 21 +++++++++++++++++++++ ch01/ex04-temp.c | 21 +++++++++++++++++++++ ch01/ex05-temp.c | 11 +++++++++++ ch01/ex06-eof.c | 6 ++++++ ch01/ex07-eof.c | 6 ++++++ ch01/ex08-count.c | 22 ++++++++++++++++++++++ ch01/ex09-copy.c | 21 +++++++++++++++++++++ ch01/ex10-replace.c | 26 ++++++++++++++++++++++++++ ch01/ex11-wc.c | 35 ++++++++++++++++++++++++++++++++++ ch01/ex12-print.c | 23 +++++++++++++++++++++++ ch01/ex13-histogram.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 256 insertions(+) create mode 100644 ch01/ex01-hello.c create mode 100644 ch01/ex02-exp.c create mode 100644 ch01/ex03-temp.c create mode 100644 ch01/ex04-temp.c create mode 100644 ch01/ex05-temp.c create mode 100644 ch01/ex06-eof.c create mode 100644 ch01/ex07-eof.c create mode 100644 ch01/ex08-count.c create mode 100644 ch01/ex09-copy.c create mode 100644 ch01/ex10-replace.c create mode 100644 ch01/ex11-wc.c create mode 100644 ch01/ex12-print.c create mode 100644 ch01/ex13-histogram.c (limited to 'ch01') diff --git a/ch01/ex01-hello.c b/ch01/ex01-hello.c new file mode 100644 index 0000000..53f4080 --- /dev/null +++ b/ch01/ex01-hello.c @@ -0,0 +1,6 @@ +#include + +int main() +{ + printf("hello, world\n"); +} diff --git a/ch01/ex02-exp.c b/ch01/ex02-exp.c new file mode 100644 index 0000000..dcc46dd --- /dev/null +++ b/ch01/ex02-exp.c @@ -0,0 +1,6 @@ +#include + +int main() +{ + printf("hello, world\c"); +} diff --git a/ch01/ex03-temp.c b/ch01/ex03-temp.c new file mode 100644 index 0000000..b52e241 --- /dev/null +++ b/ch01/ex03-temp.c @@ -0,0 +1,21 @@ +#include + +/* print Fahrenheit-Celsius table */ +int main() +{ + float fahr, celsius; + int lower, upper, step; + + lower = 0; + upper = 300; + step = 20; + + printf("%3s %6s\n", "F", "C"); + + fahr = lower; + while (fahr <= upper) { + celsius = (5.0/9.0) * (fahr-32.0); + printf("%3.0f %6.1f\n", fahr, celsius); + fahr = fahr + step; + } +} diff --git a/ch01/ex04-temp.c b/ch01/ex04-temp.c new file mode 100644 index 0000000..f78a191 --- /dev/null +++ b/ch01/ex04-temp.c @@ -0,0 +1,21 @@ +#include + +/* print Celsius-Fahrenheit table */ +int main() +{ + float celsius, fahr; + int lower, upper, step; + + lower = 0; + upper = 300; + step = 20; + + printf("%3s %6s\n", "C", "F"); + + celsius = lower; + while (celsius <= upper) { + fahr = celsius / (5.0/9.0) + 32.0; + printf("%3.0f %6.0f\n", celsius, fahr); + celsius = celsius + step; + } +} diff --git a/ch01/ex05-temp.c b/ch01/ex05-temp.c new file mode 100644 index 0000000..6af87c6 --- /dev/null +++ b/ch01/ex05-temp.c @@ -0,0 +1,11 @@ +#include + +/* print Fahrenheit-Celsius table, in reverse order */ +int main() +{ + int fahr; + + for (fahr = 300; fahr >= 0; fahr = fahr - 20) { + printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); + } +} diff --git a/ch01/ex06-eof.c b/ch01/ex06-eof.c new file mode 100644 index 0000000..bd8fae5 --- /dev/null +++ b/ch01/ex06-eof.c @@ -0,0 +1,6 @@ +#include + +int main() +{ + printf("%d\n", getchar() != EOF); +} diff --git a/ch01/ex07-eof.c b/ch01/ex07-eof.c new file mode 100644 index 0000000..f91bb75 --- /dev/null +++ b/ch01/ex07-eof.c @@ -0,0 +1,6 @@ +#include + +int main() +{ + printf("%d\n", EOF); +} diff --git a/ch01/ex08-count.c b/ch01/ex08-count.c new file mode 100644 index 0000000..c5052e6 --- /dev/null +++ b/ch01/ex08-count.c @@ -0,0 +1,22 @@ +#include + +/* count blanks, tabs, and newlines in input */ +int main() +{ + int c; + int ns, nt, nl; + + ns = 0; + nt = 0; + nl = 0; + + while ((c=getchar()) != EOF) { + if (c == ' ') + ++ns; + if (c == '\t') + ++nt; + if (c == '\n') + ++nl; + } + printf("%d %d %d\n", ns, nt, nl); +} diff --git a/ch01/ex09-copy.c b/ch01/ex09-copy.c new file mode 100644 index 0000000..2303b83 --- /dev/null +++ b/ch01/ex09-copy.c @@ -0,0 +1,21 @@ +#include + +/* merge consecutive blanks */ +int main() +{ + int c, b; + + b = 0; + while ((c = getchar()) != EOF) { + if (c != ' ') { + putchar(c); + b = 0; + } + + if (c == ' ') { + if (b == 0) + putchar(c); + b = 1; + } + } +} diff --git a/ch01/ex10-replace.c b/ch01/ex10-replace.c new file mode 100644 index 0000000..b7ec4a5 --- /dev/null +++ b/ch01/ex10-replace.c @@ -0,0 +1,26 @@ +#include + +/* replace each tab by \t, each backspace by \b, and each backslash by \\ */ +int main() +{ + int c; + + while ((c = getchar()) != EOF) { + if (c == '\t') { + putchar('\\'); + putchar('t'); + } + if (c == '\b') { + putchar('\\'); + putchar('b'); + } + if (c == '\\') { + putchar('\\'); + putchar('\\'); + } + if (c != '\t') + if (c != '\b') + if (c != '\\') + putchar(c); + } +} diff --git a/ch01/ex11-wc.c b/ch01/ex11-wc.c new file mode 100644 index 0000000..e2ec1d8 --- /dev/null +++ b/ch01/ex11-wc.c @@ -0,0 +1,35 @@ +#include + +#define IN 1 /* inside a word */ +#define OUT 0 /* outside a word */ + +/* count lines, words, and characters in input + * + * I would focus on testing the state transitions, since character and + * line counting are straightforward. The character count increments for + * every character that is not EOF, and the line count increments whenever + * a newline is encountered. Word counting is more complex because it + * depends on correctly detecting boundaries. I would include test cases + * with varying numbers of spaces, tabs, and newlines to verify that word + * boundaries are handled correctly, since those inputs are the most likely + * to reveal bugs. + */ +int main() +{ + 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("%d %d %d\n", nl, nw, nc); +} diff --git a/ch01/ex12-print.c b/ch01/ex12-print.c new file mode 100644 index 0000000..91aafa2 --- /dev/null +++ b/ch01/ex12-print.c @@ -0,0 +1,23 @@ +#include + +#define IN 1 /* inside a word */ +#define OUT 0 /* outside a word */ + +/* print one word per line from the input */ +int main() +{ + int c, state; + + state = OUT; + while ((c = getchar()) != EOF) { + if (c == ' ' || c == '\n' || c == '\t') { + if (state == IN) + putchar('\n'); + state = OUT; + } else if (state == OUT) + state = IN; + if (state == IN) + putchar(c); + } + putchar('\n'); +} diff --git a/ch01/ex13-histogram.c b/ch01/ex13-histogram.c new file mode 100644 index 0000000..14002a5 --- /dev/null +++ b/ch01/ex13-histogram.c @@ -0,0 +1,52 @@ +#include + +#define IN 1 /* inside a word */ +#define OUT 0 /* outside a word */ + +/* print a histogram of word lengths */ +int main() +{ + int c, i, j, nc, state; + int ndigit[10]; + + for (i = 0; i < 10; ++i) + ndigit[i] = 0; + + nc = 0; + state = OUT; + while ((c = getchar()) != EOF) { + if (c == ' ' || c == '\n' || c == '\t') { + if (state == IN) { + i = nc - 1; + if (i > 9) + i = 9; + ++ndigit[i]; + nc = 0; + } + state = OUT; + } else if (state == OUT) { + state = IN; + } + + if (state == IN) + ++nc; + } + + if (state == IN) { + i = nc - 1; + if (i > 9) + i = 9; + ++ndigit[i]; + nc = 0; + } + + for (i = 0; i < 10; ++i) { + if (i == 9) + printf(" >9: "); + else + printf(" %2d: ", i + 1); + for (j = 0; j < ndigit[i]; ++j) + putchar('#'); + putchar('\n'); + } +} -- cgit v1.2.3