From 2dcca73fc5c62a9837daedb3e943fb9487ff2b17 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 6 Sep 2020 14:45:41 -0400 Subject: [PATCH] Left off on exercise 1.23 --- ch_01/exercise_01-15/main.c | 32 ++++++++++ ch_01/exercise_01-17/main.c | 48 +++++++++++++++ ch_01/exercise_01-19/main.c | 66 +++++++++++++++++++++ ch_01/exercise_01-20/main.c | 88 ++++++++++++++++++++++++++++ ch_01/exercise_01-23/main_template.c | 6 ++ ch_01/main_template.c | 6 ++ 6 files changed, 246 insertions(+) create mode 100644 ch_01/exercise_01-15/main.c create mode 100644 ch_01/exercise_01-17/main.c create mode 100644 ch_01/exercise_01-19/main.c create mode 100644 ch_01/exercise_01-20/main.c create mode 100644 ch_01/exercise_01-23/main_template.c create mode 100644 ch_01/main_template.c diff --git a/ch_01/exercise_01-15/main.c b/ch_01/exercise_01-15/main.c new file mode 100644 index 0000000..2b13b0c --- /dev/null +++ b/ch_01/exercise_01-15/main.c @@ -0,0 +1,32 @@ +/* + * + * Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to + * use a function for conversion. + * + * Author: Kun Deng + */ + +#include + +#define UPPER 200 +#define LOWER 0 +#define STEP 10 + +double convert_to_celsius(double); + +int main(int argc, char **argv) +{ + printf("%13s %13s\n", "fahrenheit", "celsius"); + + for (double fahrenheit = LOWER; fahrenheit <= UPPER; fahrenheit = fahrenheit + STEP) + { + printf("%13.2f %13.2f\n", fahrenheit, convert_to_celsius(fahrenheit)); + } + + return 0; +} + +double convert_to_celsius(double fahrenheit) +{ + return ((fahrenheit - 32) * 5.0 / 9.0); +} diff --git a/ch_01/exercise_01-17/main.c b/ch_01/exercise_01-17/main.c new file mode 100644 index 0000000..65fc863 --- /dev/null +++ b/ch_01/exercise_01-17/main.c @@ -0,0 +1,48 @@ +/* + * + * Exercise 1-17. Write a program to print all input lines that are longer than 80 + * characters + * + * Author: Kun Deng + */ + +#include + +#define LINE_LIMIT_COUNT 80 +#define LINE_MAX 1000 + +int get_line(char line[]); + + +int main(int argc, char **argv) +{ + char line[LINE_MAX]; + int length = 0; + + printf("Enter a sequence of characters.\n"); + printf("Lines that are greater than %d will be printed to the output\n", LINE_LIMIT_COUNT); + + while(length = get_line(line)) + { + if (length > LINE_LIMIT_COUNT) + { + printf("%s\n", line); + } + } +} + +int get_line(char line[]) +{ + int i = 0; + int c; + + for (; ((c = getchar()) != EOF) && c != '\n'; ++i) + { + line[i] = c; + } + + line[i] = '\0'; + + + return i; +} diff --git a/ch_01/exercise_01-19/main.c b/ch_01/exercise_01-19/main.c new file mode 100644 index 0000000..067799b --- /dev/null +++ b/ch_01/exercise_01-19/main.c @@ -0,0 +1,66 @@ +/* + * + * 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. + * + * Author: Kun Deng + */ + +#include + +#define UPPER 200 +#define LOWER 0 +#define STEP 10 +#define LINE_MAX_LIMIT 1000 + +int get_line(char line[]); + +void reverse(char whole_string[], int length); + +int main(int argc, char **argv) +{ + char line[LINE_MAX_LIMIT]; + int length; + + while ((length = get_line(line))) + { + printf("before: %s\n", line); + reverse(line, length); + printf("after: %s\n", line); + } + + return 0; +} + + + +int get_line(char line[]) +{ + int i = 0; + int c; + + for (; ((c = getchar()) != EOF) && c != '\n'; ++i) + { + line[i] = c; + } + + line[i] = '\0'; + + + return i; +} + + +void reverse(char whole_string[], int length) +{ + int mirrored_index = length - 1; + + for (int i = 0; i != (length / 2); ++i) + { + char mirrored_char = whole_string[mirrored_index]; + whole_string[mirrored_index] = whole_string[i]; + whole_string[i] = mirrored_char; + + --mirrored_index; + } +} diff --git a/ch_01/exercise_01-20/main.c b/ch_01/exercise_01-20/main.c new file mode 100644 index 0000000..17ba6c1 --- /dev/null +++ b/ch_01/exercise_01-20/main.c @@ -0,0 +1,88 @@ +/* + * + * Exercise 1-20. Write a program detab that replaces the 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? + * + * Author: Kun Deng + */ + +#include + +#define TAB_REPRESENTER 2 // Represents how many spaces a tab would equal +#define LINE_MAX_LIMIT 1000 + +char line[LINE_MAX_LIMIT]; +int line_length; + +int get_line(void); + +void replace_tabs(void); + + +int main() +{ + extern int line_length; + + printf("Enter a sequence of characters\n"); + printf("Any tabs will be replaced with %d white space characters\n", LINE_MAX_LIMIT); + + while (line_length = get_line()) + { + printf("before: %s\n", line); + replace_tabs(); + printf("after: %s\n", line); + } + + return 0; +} + + +int get_line(void) +{ + extern char line[]; + int i = 0; + int c; + + for (; (c = getchar()) != EOF && c != '\n'; ++i) + { + line[i] = c; + } + + line[i] = '\0'; + + return i; +} + + +void replace_tabs(void) +{ + extern char line[]; + extern int line_length; + + for (int i = 0; i != line_length; ++i) + { + char some_char = line[i]; + + if (some_char == '\t') + { + int new_line_length = line_length + TAB_REPRESENTER; + + line[new_line_length] = '\0'; + + for (int j = (line_length - 1); j > i; --j) + { + line[j + TAB_REPRESENTER] = line[j]; + } + + for (int k = i; k < (i + TAB_REPRESENTER); ++k) + { + line[k] = ' '; + } + + line_length = new_line_length; + + line[i + TAB_REPRESENTER] = 127; + } + } +} diff --git a/ch_01/exercise_01-23/main_template.c b/ch_01/exercise_01-23/main_template.c new file mode 100644 index 0000000..de09a81 --- /dev/null +++ b/ch_01/exercise_01-23/main_template.c @@ -0,0 +1,6 @@ +/* + * + * Exercise + * + * Author: Kun Deng + */ diff --git a/ch_01/main_template.c b/ch_01/main_template.c new file mode 100644 index 0000000..de09a81 --- /dev/null +++ b/ch_01/main_template.c @@ -0,0 +1,6 @@ +/* + * + * Exercise + * + * Author: Kun Deng + */