From d7ad1d229b876ee5f144ca0c169435e0e13c9152 Mon Sep 17 00:00:00 2001 From: caskd Date: Fri, 18 Oct 2019 18:25:31 +0200 Subject: [PATCH] Split the codebase into a managable tree --- .kdev4/LearningC.kdev4 | 218 ++++++++++++ CMakeLists.txt | 5 +- chapters/1/exercises/1.c | 5 + chapters/1/exercises/10.c | 17 + chapters/1/exercises/12.c | 13 + chapters/1/exercises/13.c | 13 + chapters/1/exercises/14.c | 33 ++ chapters/1/exercises/15.c | 14 + chapters/1/exercises/16.c | 24 ++ chapters/1/exercises/17.c | 13 + chapters/1/exercises/18.c | 20 ++ chapters/1/exercises/19.c | 15 + chapters/1/exercises/20.c | 12 + chapters/1/exercises/21.c | 12 + chapters/1/exercises/3.c | 15 + chapters/1/exercises/4.c | 17 + chapters/1/exercises/5.c | 12 + chapters/1/exercises/6.c | 9 + chapters/1/exercises/7.c | 6 + chapters/1/exercises/8.c | 18 + chapters/1/exercises/shared/functions.h | 8 + .../exercises/shared/functions/convert2cels.c | 3 + chapters/1/exercises/shared/functions/copy.c | 6 + chapters/1/exercises/shared/functions/detab.c | 30 ++ chapters/1/exercises/shared/functions/entab.c | 37 ++ .../1/exercises/shared/functions/getlines.c | 13 + .../1/exercises/shared/functions/reverse.c | 10 + chapters/1/exercises/shared/limits.h | 1 + chapters/1/exercises/shared/tempsteps.h | 3 + main.c | 326 ------------------ 30 files changed, 601 insertions(+), 327 deletions(-) create mode 100644 chapters/1/exercises/1.c create mode 100644 chapters/1/exercises/10.c create mode 100644 chapters/1/exercises/12.c create mode 100644 chapters/1/exercises/13.c create mode 100644 chapters/1/exercises/14.c create mode 100644 chapters/1/exercises/15.c create mode 100644 chapters/1/exercises/16.c create mode 100644 chapters/1/exercises/17.c create mode 100644 chapters/1/exercises/18.c create mode 100644 chapters/1/exercises/19.c create mode 100644 chapters/1/exercises/20.c create mode 100644 chapters/1/exercises/21.c create mode 100644 chapters/1/exercises/3.c create mode 100644 chapters/1/exercises/4.c create mode 100644 chapters/1/exercises/5.c create mode 100644 chapters/1/exercises/6.c create mode 100644 chapters/1/exercises/7.c create mode 100644 chapters/1/exercises/8.c create mode 100644 chapters/1/exercises/shared/functions.h create mode 100644 chapters/1/exercises/shared/functions/convert2cels.c create mode 100644 chapters/1/exercises/shared/functions/copy.c create mode 100644 chapters/1/exercises/shared/functions/detab.c create mode 100644 chapters/1/exercises/shared/functions/entab.c create mode 100644 chapters/1/exercises/shared/functions/getlines.c create mode 100644 chapters/1/exercises/shared/functions/reverse.c create mode 100644 chapters/1/exercises/shared/limits.h create mode 100644 chapters/1/exercises/shared/tempsteps.h delete mode 100644 main.c diff --git a/.kdev4/LearningC.kdev4 b/.kdev4/LearningC.kdev4 index b836f1d..a78e9c4 100644 --- a/.kdev4/LearningC.kdev4 +++ b/.kdev4/LearningC.kdev4 @@ -23,6 +23,224 @@ checkPerformance=true checkPortability=true checkStyle=true +[Filters] +size=43 + +[Filters][0] +inclusive=0 +pattern=.* +targets=3 + +[Filters][1] +inclusive=1 +pattern=.gitignore +targets=1 + +[Filters][10] +inclusive=1 +pattern=.clang-format +targets=1 + +[Filters][11] +inclusive=1 +pattern=.circleci +targets=2 + +[Filters][12] +inclusive=0 +pattern=.git +targets=2 + +[Filters][13] +inclusive=0 +pattern=CVS +targets=2 + +[Filters][14] +inclusive=0 +pattern=.svn +targets=2 + +[Filters][15] +inclusive=0 +pattern=_svn +targets=2 + +[Filters][16] +inclusive=0 +pattern=SCCS +targets=2 + +[Filters][17] +inclusive=0 +pattern=_darcs +targets=2 + +[Filters][18] +inclusive=0 +pattern=.hg +targets=2 + +[Filters][19] +inclusive=0 +pattern=.bzr +targets=2 + +[Filters][2] +inclusive=1 +pattern=.gitmodules +targets=1 + +[Filters][20] +inclusive=0 +pattern=__pycache__ +targets=2 + +[Filters][21] +inclusive=0 +pattern=*.o +targets=1 + +[Filters][22] +inclusive=0 +pattern=*.a +targets=1 + +[Filters][23] +inclusive=0 +pattern=*.so +targets=1 + +[Filters][24] +inclusive=0 +pattern=*.so.* +targets=1 + +[Filters][25] +inclusive=0 +pattern=*.obj +targets=1 + +[Filters][26] +inclusive=0 +pattern=*.lib +targets=1 + +[Filters][27] +inclusive=0 +pattern=*.dll +targets=1 + +[Filters][28] +inclusive=0 +pattern=*.exp +targets=1 + +[Filters][29] +inclusive=0 +pattern=*.pdb +targets=1 + +[Filters][3] +inclusive=1 +pattern=.pre-commit-config.yaml +targets=1 + +[Filters][30] +inclusive=0 +pattern=moc_*.cpp +targets=1 + +[Filters][31] +inclusive=0 +pattern=*.moc +targets=1 + +[Filters][32] +inclusive=0 +pattern=ui_*.h +targets=1 + +[Filters][33] +inclusive=0 +pattern=*.qmlc +targets=1 + +[Filters][34] +inclusive=0 +pattern=qrc_*.cpp +targets=1 + +[Filters][35] +inclusive=0 +pattern=*~ +targets=1 + +[Filters][36] +inclusive=0 +pattern=*.orig +targets=1 + +[Filters][37] +inclusive=0 +pattern=.*.kate-swp +targets=1 + +[Filters][38] +inclusive=0 +pattern=.*.swp +targets=1 + +[Filters][39] +inclusive=0 +pattern=*.pyc +targets=1 + +[Filters][4] +inclusive=1 +pattern=.gitlab-ci.yml +targets=1 + +[Filters][40] +inclusive=0 +pattern=*.pyo +targets=1 + +[Filters][41] +inclusive=0 +pattern=/learningc +targets=1 + +[Filters][42] +inclusive=0 +pattern=/build/learningc +targets=1 + +[Filters][5] +inclusive=1 +pattern=.travis.yml +targets=1 + +[Filters][6] +inclusive=1 +pattern=.editorconfig +targets=1 + +[Filters][7] +inclusive=1 +pattern=.pep8 +targets=1 + +[Filters][8] +inclusive=1 +pattern=.prettierignore +targets=1 + +[Filters][9] +inclusive=1 +pattern=.prettierrc* +targets=1 + [Launch] Launch Configurations=Launch Configuration 0 diff --git a/CMakeLists.txt b/CMakeLists.txt index 71193c5..5cd50dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.0) project(learningc LANGUAGES C) -add_executable(learningc main.c) +set(CHAPTER 1) +set(EXERCISE 21) + +add_executable(learningc chapters/${CHAPTER}/exercises/${EXERCISE}.c) install(TARGETS learningc RUNTIME DESTINATION bin) diff --git a/chapters/1/exercises/1.c b/chapters/1/exercises/1.c new file mode 100644 index 0000000..b769aab --- /dev/null +++ b/chapters/1/exercises/1.c @@ -0,0 +1,5 @@ +#include +int main() { + printf("Hello, World!\n"); + return 0; +} diff --git a/chapters/1/exercises/10.c b/chapters/1/exercises/10.c new file mode 100644 index 0000000..3d7062c --- /dev/null +++ b/chapters/1/exercises/10.c @@ -0,0 +1,17 @@ +#include + +int main() { + int c; + while ((c = getchar()) != EOF) { + if (c == '\t') { + printf("\\t"); + } else if (c == '\b') { + printf("\\b"); // Needs literal backspace due to line buffer + } else if (c == '\\') { + printf("\\\\"); + } else { + putchar(c); + } + } + return 0; +} diff --git a/chapters/1/exercises/12.c b/chapters/1/exercises/12.c new file mode 100644 index 0000000..1a01e74 --- /dev/null +++ b/chapters/1/exercises/12.c @@ -0,0 +1,13 @@ +#include + +int main() { + int c; + while ((c = getchar()) != EOF) { + if (c == ' ' || c == '\n' || c == '\t') { + printf("\n"); + } else { + putchar(c); + } + } + return 0; +} diff --git a/chapters/1/exercises/13.c b/chapters/1/exercises/13.c new file mode 100644 index 0000000..3f33588 --- /dev/null +++ b/chapters/1/exercises/13.c @@ -0,0 +1,13 @@ +#include + +int main() { + int c; + while ((c = getchar()) != EOF) { + if (c == ' ' || c == '\n' || c == '\t') { + printf("\n"); + } else { + printf("#"); + } + } + return 0; +} diff --git a/chapters/1/exercises/14.c b/chapters/1/exercises/14.c new file mode 100644 index 0000000..b85a2c7 --- /dev/null +++ b/chapters/1/exercises/14.c @@ -0,0 +1,33 @@ +#include +#include "shared/limits.h" + +int main() { + int c; + int count[MAXLINE]; + for (int i = 0; i < MAXLINE; i++) { + count[i] = 0; + } + while ((c = getchar()) != EOF) { + count[c]++; + } + printf("\n"); + for (int i = 0; i < MAXLINE; i++) { + if (count[i] != 0) { + if (i == '\n') { + printf("\\n"); + } else if (i == '\t') { + printf("\\t"); + } else if (i == '\"') { + printf("\\\""); + } else { + putchar(i); + } + printf("\t: "); + for (int z = 0; z < count[i]; z++) { + printf("#"); + } + printf("\n"); + } + } + return 0; +} diff --git a/chapters/1/exercises/15.c b/chapters/1/exercises/15.c new file mode 100644 index 0000000..6d23e14 --- /dev/null +++ b/chapters/1/exercises/15.c @@ -0,0 +1,14 @@ +#include +#include "shared/tempsteps.h" +#include "shared/functions.h" +#include "shared/functions/convert2cels.c" + +int main() { + float celsius = LOWER; + printf("%s", "Temperature conversion table (F to C):\n"); + while (celsius <= UPPER) { + printf("%3.0f %6.1f\n", celsius, convert2cels(celsius)); + celsius = celsius + STEP; + } + return 0; +} diff --git a/chapters/1/exercises/16.c b/chapters/1/exercises/16.c new file mode 100644 index 0000000..0a16aec --- /dev/null +++ b/chapters/1/exercises/16.c @@ -0,0 +1,24 @@ +#include +#include "shared/functions.h" +#include "shared/limits.h" +#include "shared/functions/getlines.c" + +int main() { + int len; + /* current line length */ + int max; + /* maximum length seen so far */ + char line[MAXLINE]; + /* current input line */ + char longest[MAXLINE]; + /* longest line saved here */ + max = 0; + while ((len = getlines(line, MAXLINE)) > 0) + if (len > max) { + max = len; + copy(longest, line); + } + if (max > 0) /* there was a line */ + printf("Character count: %d\n%s", max, longest); + return 0; +} diff --git a/chapters/1/exercises/17.c b/chapters/1/exercises/17.c new file mode 100644 index 0000000..29788c1 --- /dev/null +++ b/chapters/1/exercises/17.c @@ -0,0 +1,13 @@ +#include +#include "shared/limits.h" +#include "shared/functions.h" +#include "shared/functions/getlines.c" + +int main() { + char line[MAXLINE]; + int len; + while ((len = getlines(line,MAXLINE)) > 0) { + if (len > 80) + printf("%s", line); + } +} diff --git a/chapters/1/exercises/18.c b/chapters/1/exercises/18.c new file mode 100644 index 0000000..611552a --- /dev/null +++ b/chapters/1/exercises/18.c @@ -0,0 +1,20 @@ +#include +#include "shared/limits.h" +#include "shared/functions.h" +#include "shared/functions/getlines.c" + +int main() { + char line[MAXLINE], prostr[MAXLINE]; + int len; + int skip = 0; + while ((len = getlines(line, MAXLINE)) > 0) { + for (int i=len; !(i < 0); --i) { + if (skip != 0 || !(line[i] == ' ' || line[i] == '\n' || line[i] == '\t' || line[i] == '\0')) { + prostr[i] = line[i]; + skip = 1; + } + } + } + printf("\"%s\"\n", prostr); + return 0; +} diff --git a/chapters/1/exercises/19.c b/chapters/1/exercises/19.c new file mode 100644 index 0000000..45a4c4e --- /dev/null +++ b/chapters/1/exercises/19.c @@ -0,0 +1,15 @@ +#include +#include "shared/limits.h" +#include "shared/functions.h" +#include "shared/functions/getlines.c" +#include "shared/functions/reverse.c" + +int main() { + char d[MAXLINE]; + char line[MAXLINE]; + int len; + while ((len = getlines(line,MAXLINE)) > 0) { + reverse(line, d); + printf("\nOriginal: %s\nReversed: %s\n", line, d); + } +} diff --git a/chapters/1/exercises/20.c b/chapters/1/exercises/20.c new file mode 100644 index 0000000..c34f3b6 --- /dev/null +++ b/chapters/1/exercises/20.c @@ -0,0 +1,12 @@ +#include +#include "shared/limits.h" +#include "shared/functions.h" +#include "shared/functions/detab.c" + +int main() { + int len; + char line[MAXLINE]; + while ((len = detab(line,MAXLINE,8)) > 0) { + printf("%s", line); + } +} diff --git a/chapters/1/exercises/21.c b/chapters/1/exercises/21.c new file mode 100644 index 0000000..d7b141b --- /dev/null +++ b/chapters/1/exercises/21.c @@ -0,0 +1,12 @@ +#include +#include "shared/limits.h" +#include "shared/functions.h" +#include "shared/functions/entab.c" + +int main() { + int len; + char line[MAXLINE]; + while ((len = entab(line,MAXLINE,8)) > 0) { + printf("%s", line); + } +} diff --git a/chapters/1/exercises/3.c b/chapters/1/exercises/3.c new file mode 100644 index 0000000..b9d93eb --- /dev/null +++ b/chapters/1/exercises/3.c @@ -0,0 +1,15 @@ +#include +#include "shared/tempsteps.h" + +int main() { + float fahr, celsius; + + fahr = LOWER; + printf("%s", "Temperature conversion table (F to C):\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; +} diff --git a/chapters/1/exercises/4.c b/chapters/1/exercises/4.c new file mode 100644 index 0000000..a0c1bcb --- /dev/null +++ b/chapters/1/exercises/4.c @@ -0,0 +1,17 @@ +#include +int main() { + float fahr, celsius, lower, upper, step; + + lower = -40; + upper = 300; + step = 20; + + celsius = lower; + printf("%s", "Temperature conversion table (C to F):\n"); + while (celsius <= upper) { + fahr = celsius * (9.0/5.0) + 32.0; + printf("%3.0f %6.1f\n", celsius, fahr); + celsius = celsius + step; + } + return 0; +} diff --git a/chapters/1/exercises/5.c b/chapters/1/exercises/5.c new file mode 100644 index 0000000..d830055 --- /dev/null +++ b/chapters/1/exercises/5.c @@ -0,0 +1,12 @@ +#include +#include "shared/tempsteps.h" + +int main() { + float fahr = UPPER; + printf("%s", "Temperature conversion table (F to C):\n"); + while (fahr >= LOWER) { + printf("%3.0f %6.1f\n", fahr, (5.0/9.0) * (fahr-32.0)); + fahr = fahr - STEP; + } + return 0; +} diff --git a/chapters/1/exercises/6.c b/chapters/1/exercises/6.c new file mode 100644 index 0000000..6d839bd --- /dev/null +++ b/chapters/1/exercises/6.c @@ -0,0 +1,9 @@ +#include + +int main() { + int c; + while ((c = getchar()) != EOF) { + putchar(c); + } + return 0; +} diff --git a/chapters/1/exercises/7.c b/chapters/1/exercises/7.c new file mode 100644 index 0000000..43e5327 --- /dev/null +++ b/chapters/1/exercises/7.c @@ -0,0 +1,6 @@ +#include + +int main() { + printf("%d\n",EOF); + return 0; +} diff --git a/chapters/1/exercises/8.c b/chapters/1/exercises/8.c new file mode 100644 index 0000000..5fda812 --- /dev/null +++ b/chapters/1/exercises/8.c @@ -0,0 +1,18 @@ +#include + +int main() { + int c, tabc, blankc, nl; + tabc = blankc = nl = 0; + while ((c = getchar()) != EOF) { + if (c == '\t') { + tabc++; + } else if (c == ' ') { + blankc++; + } else if (c == '\n') { + nl++; + } + } + printf ("\nSpecial character count:\n"); + printf ("Newlines: %d\nBlanks: %d\nTabs: %d\n", nl, blankc, tabc); + return 0; +} diff --git a/chapters/1/exercises/shared/functions.h b/chapters/1/exercises/shared/functions.h new file mode 100644 index 0000000..bc956bb --- /dev/null +++ b/chapters/1/exercises/shared/functions.h @@ -0,0 +1,8 @@ +#include +#include "limits.h" +float convert2cels(int fahr); +void reverse(char s[MAXLINE], char d[strlen(s)]); +void copy(char to[], char from[]); +int getlines(char line[], int maxline); +int detab(char s[],int lim, int col); +int entab(char s[], int lim, int col); diff --git a/chapters/1/exercises/shared/functions/convert2cels.c b/chapters/1/exercises/shared/functions/convert2cels.c new file mode 100644 index 0000000..1e60a42 --- /dev/null +++ b/chapters/1/exercises/shared/functions/convert2cels.c @@ -0,0 +1,3 @@ +float convert2cels(int fahr) { + return (5.0/9.0) * (fahr-32.0); +} diff --git a/chapters/1/exercises/shared/functions/copy.c b/chapters/1/exercises/shared/functions/copy.c new file mode 100644 index 0000000..57d4fec --- /dev/null +++ b/chapters/1/exercises/shared/functions/copy.c @@ -0,0 +1,6 @@ +void copy(char to[], char from[]) { + int i; + i = 0; + while ((to[i] = from[i]) != '\0') + ++i; +} diff --git a/chapters/1/exercises/shared/functions/detab.c b/chapters/1/exercises/shared/functions/detab.c new file mode 100644 index 0000000..ddb2262 --- /dev/null +++ b/chapters/1/exercises/shared/functions/detab.c @@ -0,0 +1,30 @@ +#include + +int detab(char s[],int lim, int col) +/*s is the string we need to replace, lim is the character limit + * and col is the size of a tab (8 characters usually so we use that)*/ +{ + int c, i; /*c is the character i0nteger representation and i is the column we are on*/ + for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) + if (c == '\t') { + int sp=col-i%col; /*Amount of spaces to replace the tab with*/ + int st=i; /*Starting point*/ + for (int co = 0; co<=sp && i < lim-1; co++ && i++) { + /*For each space we need to add, we increase the column count and add it + *to the array at position (starting point + spaces added so far) + * and we increase the spaces added so far*/ + s[st+co] = ' '; + } + } else { + s[i] = c; + ++i; + } + if (c == '\n') + /*If it is the end of the string, represented by a new line, + * add that new line and increase it for the \0 addition*/ { + s[i] = c; + } + /*Add the string end and return it to the previous context*/ + s[i+1] = '\0'; + return i; +} diff --git a/chapters/1/exercises/shared/functions/entab.c b/chapters/1/exercises/shared/functions/entab.c new file mode 100644 index 0000000..0128d14 --- /dev/null +++ b/chapters/1/exercises/shared/functions/entab.c @@ -0,0 +1,37 @@ +#include + +int entab(char s[], int lim, int col) { + int c, i, t; + for (i=0,t=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';i++,t++) { + /* + * c is the character representation as integer + * i is the current column on the copied string + * t is the current column on the original string + * + * t is used to count tab stops because i goes back when spaces are replaced by tabs. + * i and t are both modified, however only i changes in contexts other than the routinal increase. + * c is set to i's position and t is only used in tab stop counting + */ + if (t >= col && t%col == 0 && s[i-1] == ' ' && s[i-2] == ' ') { + int x; + for (x = i; s[x-1] == ' ' && i-col < x; x--); + s[x] = '\t'; + i = ++x; + } + s[i] = c; + } + if (c == '\n') + /* + * If it is the end of the string, represented by a new line, + * add that new line and increase it for the \0 addition + */ { + s[i] = c; + } + /*Add the string end and return it to the previous context*/ + if (lim-1 > i) { + s[i+1] = '\0'; + } else { + s[lim-1] = '\0'; + } + return i; +} diff --git a/chapters/1/exercises/shared/functions/getlines.c b/chapters/1/exercises/shared/functions/getlines.c new file mode 100644 index 0000000..7dad0f7 --- /dev/null +++ b/chapters/1/exercises/shared/functions/getlines.c @@ -0,0 +1,13 @@ +#include + +int getlines(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; +} diff --git a/chapters/1/exercises/shared/functions/reverse.c b/chapters/1/exercises/shared/functions/reverse.c new file mode 100644 index 0000000..a28db05 --- /dev/null +++ b/chapters/1/exercises/shared/functions/reverse.c @@ -0,0 +1,10 @@ +#include +#include "../limits.h" + +void reverse(char s[MAXLINE], char d[strlen(s)+1]) { + int len = strlen(s); + for (int i=--len; i >= 0; --i) { + d[len-i] = s[i]; + d[len+1] = '\0'; + } +} diff --git a/chapters/1/exercises/shared/limits.h b/chapters/1/exercises/shared/limits.h new file mode 100644 index 0000000..489ad69 --- /dev/null +++ b/chapters/1/exercises/shared/limits.h @@ -0,0 +1 @@ +#define MAXLINE 1000 diff --git a/chapters/1/exercises/shared/tempsteps.h b/chapters/1/exercises/shared/tempsteps.h new file mode 100644 index 0000000..b0324dc --- /dev/null +++ b/chapters/1/exercises/shared/tempsteps.h @@ -0,0 +1,3 @@ +#define LOWER 0 +#define UPPER 300 +#define STEP 20 diff --git a/main.c b/main.c deleted file mode 100644 index a3422ff..0000000 --- a/main.c +++ /dev/null @@ -1,326 +0,0 @@ -#include -#include -#include - -// // PART OF 1-5, 1-15 -// #define LOWER 0 -// #define UPPER 300 -// #define STEP 20 - -// // PART OF 1-15 -// float convert2cels(int fahr); - -// // PART OF 1-16, 1-17, 1-18, 1-19, 1-20,1-21 -#define MAXLINE 1000 -// int getlines(char line[], int maxline); -// int detab(char s[],int lim, int col); -int entab(char s[], int lim, int col); -// void reverse(char s[MAXLINE], char d[strlen(s)]); -// void copy(char to[], char from[]); - -int main() { -// // 1-1 -// printf("Hello, World!\n"); - -// // 1-3 -// float fahr, celsius, lower, upper, step; -// -// lower = 0; -// upper = 300; -// step = 20; -// -// fahr = lower; -// printf("%s", "Temperature conversion table (F to C):\n"); -// while (fahr <= upper) { -// celsius = (5.0/9.0) * (fahr-32.0); -// printf("%3.0f %6.1f\n", fahr, celsius); -// fahr = fahr + step; -// } - -// // 1-4 -// float fahr, celsius, lower, upper, step; -// -// lower = -40; -// upper = 300; -// step = 20; -// -// celsius = lower; -// printf("%s", "Temperature conversion table (C to F):\n"); -// while (celsius <= upper) { -// fahr = celsius * (9.0/5.0) + 32.0; -// printf("%3.0f %6.1f\n", celsius, fahr); -// celsius = celsius + step; -// } - -// // 1-5 -// float fahr = UPPER; -// printf("%s", "Temperature conversion table (F to C):\n"); -// while (fahr >= LOWER) { -// printf("%3.0f %6.1f\n", fahr, (5.0/9.0) * (fahr-32.0)); -// fahr = fahr - STEP; -// } - -// // 1-6 -// int c; -// while ((c = getchar()) != EOF) { -// putchar(c); -// } - -// // 1-7 -// printf("%d\n",EOF); - -// // 1-8 -// int c, tabc, blankc, nl; -// tabc = blankc = nl = 0; -// while ((c = getchar()) != EOF ) { -// if (c == '\t') { -// tabc++; -// } else if (c == ' ') { -// blankc++; -// } else if (c == '\n') { -// nl++; -// } -// } -// printf ("\nSpecial character count:\n"); -// printf ("Newlines: %d\nBlanks: %d\nTabs: %d\n", nl, blankc, tabc); - -// // 1-9 TODO -// - -// // 1-10 -// int c; -// while ((c = getchar()) != EOF) { -// if (c == '\t') { -// printf("\\t"); -// } else if (c == '\b') { -// printf("\\b"); // Needs literal backspace due to line buffer -// } else if (c == '\\') { -// printf("\\\\"); -// } else { -// putchar(c); -// } -// } - -// // 1-12 -// int c; -// while ((c = getchar()) != EOF) { -// if (c == ' ' || c == '\n' || c == '\t') { -// printf("\n"); -// } else { -// putchar(c); -// } -// } - -// // 1-13 -// int c; -// while ((c = getchar()) != EOF) { -// if (c == ' ' || c == '\n' || c == '\t') { -// printf("\n"); -// } else { -// printf("#"); -// } -// } - -// // 1-14 -// int c; -// int count[1024]; -// for (int i = 0; i < 1024; i++) { -// count[i] = 0; -// } -// while ((c = getchar()) != EOF) { -// count[c]++; -// } -// printf("\n"); -// for (int i = 0; i < 1024; i++) { -// if (count[i] != 0) { -// if (i == '\n') { -// printf("\\n"); -// } else if (i == '\t') { -// printf("\\t"); -// } else if (i == '\"') { -// printf("\\\""); -// } else { -// putchar(i); -// } -// printf("\t: "); -// for (int z = 0; z < count[i]; z++) { -// printf("#"); -// } -// printf("\n"); -// } -// } - -// // 1-15 -// float celsius = LOWER; -// printf("%s", "Temperature conversion table (F to C):\n"); -// while (celsius <= UPPER) { -// printf("%3.0f %6.1f\n", celsius, convert2cels(celsius)); -// celsius = celsius + STEP; -// } - -// // 1-16 -// int len; -// /* current line length */ -// int max; -// /* maximum length seen so far */ -// char line[MAXLINE]; -// /* current input line */ -// char longest[MAXLINE]; -// /* longest line saved here */ -// max = 0; -// while ((len = getlines(line, MAXLINE)) > 0) -// if (len > max) { -// max = len; -// copy(longest, line); -// } -// if (max > 0) /* there was a line */ -// printf("Character count: %d\n%s", max, longest); - -// // 1-17 -// char line[MAXLINE]; -// int len; -// while ((len = getlines(line,MAXLINE)) > 0) { -// if (len > 80) -// printf("%s", line); -// } - -// // 1-18 -// char line[MAXLINE], prostr[MAXLINE]; -// int len; -// int skip = 0; -// while ((len = getlines(line, MAXLINE)) > 0) { -// for (int i=len; !(i < 0); --i) { -// if (skip != 0 || !(line[i] == ' ' || line[i] == '\n' || line[i] == '\t' || line[i] == '\0')) { -// prostr[i] = line[i]; -// skip = 1; -// } -// } -// } -// printf("\"%s\"\n", prostr);\ - -// // 1-19 -// char d[MAXLINE]; -// char line[MAXLINE]; -// int len; -// while ((len = getlines(line,MAXLINE)) > 0) { -// reverse(line, d); -// printf("\nOriginal: %s\nReversed: %s\n", line, d); -// } - -// // 1-20 -// int len; -// char line[MAXLINE]; -// while ((len = detab(line,MAXLINE,8)) > 0) { -// printf("%s", line); -// } - -// 1-21 - int len; - char line[MAXLINE]; - while ((len = entab(line,MAXLINE,8)) > 0) { - printf("%s", line); - } - return 0; -} - -// // PART OF 1-15 -// float convert2cels(int fahr) { -// return (5.0/9.0) * (fahr-32.0); -// } - -// // PART OF 1-16, 1-17, 1-18, 1-19 -// int getlines(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; -// } - -// // PART OF 1-20 -// int detab(char s[],int lim, int col) -// /*s is the string we need to replace, lim is the character limit -// * and col is the size of a tab (8 characters usually so we use that)*/ -// { -// int c, i; /*c is the character integer representation and i is the column we are on*/ -// for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) -// if (c == '\t') { -// int sp=col-i%col; /*Amount of spaces to replace the tab with*/ -// int st=i; /*Starting point*/ -// for (int co = 0; co<=sp && i < lim-1; co++ && i++) { -// /*For each space we need to add, we increase the column count and add it -// *to the array at position (starting point + spaces added so far) -// * and we increase the spaces added so far*/ -// s[st+co] = ' '; -// } -// } else { -// s[i] = c; -// ++i; -// } -// if (c == '\n') -// /*If it is the end of the string, represented by a new line, -// * add that new line and increase it for the \0 addition*/ { -// s[i] = c; -// } -// /*Add the string end and return it to the previous context*/ -// s[i+1] = '\0'; -// return i; -// } - -// PART OF 1-21 -int entab(char s[], int lim, int col) { - int c, i, t; - for (i=0,t=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';i++,t++) { - /* - * c is the character representation as integer - * i is the current column on the copied string - * t is the current column on the original string - * - * t is used to count tab stops because i goes back when spaces are replaced by tabs. - * i and t are both modified, however only i changes in contexts other than the routinal increase. - * c is set to i's position and t is only used in tab stop counting - */ - if (t >= col && t%col == 0 && s[i-1] == ' ' && s[i-2] == ' ') { - int x; - for (x = i; s[x-1] == ' ' && i-col < x; x--); - s[x] = '\t'; - i = ++x; - } - s[i] = c; - } - if (c == '\n') - /* - * If it is the end of the string, represented by a new line, - * add that new line and increase it for the \0 addition - */ { - s[i] = c; - } - /*Add the string end and return it to the previous context*/ - if (lim-1 > i) { - s[i+1] = '\0'; - } else { - s[lim-1] = '\0'; - } - return i; -} - -// /* copy: copy 'from' into 'to'; assume to is big enough */ -// void copy(char to[], char from[]) { -// int i; -// i = 0; -// while ((to[i] = from[i]) != '\0') -// ++i; -// } - -// // PART OF 1-19 -// void reverse(char s[MAXLINE], char d[strlen(s)+1]) { -// int len = strlen(s); -// for (int i=--len; i >= 0; --i) { -// d[len-i] = s[i]; -// d[len+1] = '\0'; -// } -// }