From 2c345e0909a082b77be959a4e6166cdbdc79e067 Mon Sep 17 00:00:00 2001 From: Adam Carpenter <53hornet@gmail.com> Date: Fri, 10 May 2019 16:00:12 -0400 Subject: Init. --- copier.c | 29 +++++++++++++++++++++++++++++ counter.c | 29 +++++++++++++++++++++++++++++ ctof.c | 24 ++++++++++++++++++++++++ ftoc.c | 32 ++++++++++++++++++++++++++++++++ hello.c | 8 ++++++++ line_counter.c | 16 ++++++++++++++++ whitespace_counter.c | 22 ++++++++++++++++++++++ 7 files changed, 160 insertions(+) create mode 100644 copier.c create mode 100644 counter.c create mode 100644 ctof.c create mode 100644 ftoc.c create mode 100644 hello.c create mode 100644 line_counter.c create mode 100644 whitespace_counter.c diff --git a/copier.c b/copier.c new file mode 100644 index 0000000..eeade8e --- /dev/null +++ b/copier.c @@ -0,0 +1,29 @@ +#include + +/* Copy input to output; 1st version */ +//main() { +// int c; +// +// c = getchar(); +// +// while (c != EOF) { +// putchar(c); +// c = getchar(); +// } +//} + +/* Copy input to output; 2nd version */ +main() { + int c; + + while ((c = getchar()) != EOF) { + putchar(c); + } +} + +//main() { +// printf("%d", EOF); +// getchar(); +// printf("%d", getchar()); +// printf("%d", getchar()); +//} diff --git a/counter.c b/counter.c new file mode 100644 index 0000000..a1b9927 --- /dev/null +++ b/counter.c @@ -0,0 +1,29 @@ +#include + +/* + * Count characters in input; 1st version + */ +//main() { +// long nc; +// +// nc = 0; +// +// while (getchar() != EOF) +// ++nc; +// +// +// printf("%ld\n", nc); +//} + +/* + * Count characters in input; 2nd version + */ +main() { + long nc; + + + for (nc = 0; getchar() != EOF; ++nc) + ; + + printf("%ld\n", nc); +} diff --git a/ctof.c b/ctof.c new file mode 100644 index 0000000..7e2cbef --- /dev/null +++ b/ctof.c @@ -0,0 +1,24 @@ +#include + +/* + * Print Celcius-Fahrenheit table for celcius = 0, 20, ... 300. + */ +main() { + float fahr, celcius; + int lower, upper, step; + + lower = 0; // lower limit of temperature table + upper = 300; // upper limit of temperature table + step = 20; // step size + + celcius = lower; + + printf("%3s %6s\n", "C", "F"); + + while (celcius <= upper) { + fahr = (9.0 / 5.0) * celcius + 32.0; + printf("%3.0f %6.1f\n", celcius, fahr); + celcius = celcius + step; + } +} + diff --git a/ftoc.c b/ftoc.c new file mode 100644 index 0000000..2efa6f7 --- /dev/null +++ b/ftoc.c @@ -0,0 +1,32 @@ +#include + +#define LOWER 0 // lower limit of table +#define UPPER 300 // upper limit of table +#define STEP 20 // step size + +/* + * Print Fahrenheit-Celcius table for fahr = 0, 20, ... 300. + */ +main() { + float fahr, celcius; + int lower, upper, step; + + lower = 0; // lower limit of temperature table + upper = 300; // upper limit of temperature table + step = 20; // step size + + fahr = lower; + + printf("%3s %6s\n", "F", "C"); + + // while (fahr <= upper) { + // celcius = 5.0 / 9.0 * (fahr - 32.0); + // printf("%3.0f %6.1f\n", fahr, celcius); + // fahr = fahr + step; + // } + + for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) { + printf("%3.0f %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32)); + } +} + diff --git a/hello.c b/hello.c new file mode 100644 index 0000000..b1ad928 --- /dev/null +++ b/hello.c @@ -0,0 +1,8 @@ +#include + +main() { + printf("hello, "); + printf("world."); + printf("\n"); +} + diff --git a/line_counter.c b/line_counter.c new file mode 100644 index 0000000..0447aa7 --- /dev/null +++ b/line_counter.c @@ -0,0 +1,16 @@ +#include + +/* + * Count lines in input + */ +main() { + int c, nl; + + nl = 0; + + while ((c = getchar()) != EOF) + if (c == '\n') + ++nl; + + printf("%d\n", nl); +} diff --git a/whitespace_counter.c b/whitespace_counter.c new file mode 100644 index 0000000..ea441e2 --- /dev/null +++ b/whitespace_counter.c @@ -0,0 +1,22 @@ +#include + +/* + * Count whitespace in input + */ +main() { + int c, n_whitespace; + + n_whitespace = 0; + + while ((c = getchar()) != EOF) { + if (c == '\n') + ++n_whitespace; + if (c == '\t') + ++n_whitespace; + if (c == ' ') + ++n_whitespace; + } + + + printf("%d\n", n_whitespace); +} -- cgit v1.2.3