From f11eef6a7a9179f6936e35a04453c263de836475 Mon Sep 17 00:00:00 2001 From: Tibor Bizjak Date: Fri, 14 Jul 2023 12:50:17 +0200 Subject: [PATCH] solutions to excercises --- k&r/04-funcs-and-prog-struct/04-07.c | 48 +++++++++++++++++++++++++ k&r/04-funcs-and-prog-struct/04-08.c | 22 ++++++++++++ k&r/04-funcs-and-prog-struct/04-12.c | 37 +++++++++++++++++++ k&r/04-funcs-and-prog-struct/04-13.c | 28 +++++++++++++++ k&r/04-funcs-and-prog-struct/04-qsort.c | 43 ++++++++++++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 k&r/04-funcs-and-prog-struct/04-07.c create mode 100644 k&r/04-funcs-and-prog-struct/04-08.c create mode 100644 k&r/04-funcs-and-prog-struct/04-12.c create mode 100644 k&r/04-funcs-and-prog-struct/04-13.c create mode 100644 k&r/04-funcs-and-prog-struct/04-qsort.c diff --git a/k&r/04-funcs-and-prog-struct/04-07.c b/k&r/04-funcs-and-prog-struct/04-07.c new file mode 100644 index 0000000..ca681d9 --- /dev/null +++ b/k&r/04-funcs-and-prog-struct/04-07.c @@ -0,0 +1,48 @@ +#include +#include + +#define MIN(A, B) ((A < B) ? (A) : (B)) + +/* -------- INPUT WITH PUSHBACK */ +int _getchar() +{ + extern int buffi; + extern char buffer[]; + return buffi ? buffer[--buffi] : getchar(); +} +#undef getchar +#define getchar() _getchar() + + +/* --------- MAIN PROGRAM */ +#define SLEN 10 +void ungets(char *s); + +/* this program copies its input to its output, + * if ungets wokrks as expected */ +int main() +{ + int i, c; + char s[SLEN+1]; + + for (i = 0; i < SLEN && (c = getchar()) != EOF; i++) + s[i] = c; + s[i] = '\0'; + + ungets(s); + while ((c = getchar()) != EOF) + putchar(c); +} + + +#define BUFF_SIZE 100 +int buffi; +char buffer[BUFF_SIZE]; + +/* ungets: pushes string s back to input */ +void ungets(char *s) +{ + int i = strlen(s); + for (i = MIN(i, BUFF_SIZE-buffi)-1; i >= 0; i--, buffi++) + buffer[buffi] = s[i]; +} diff --git a/k&r/04-funcs-and-prog-struct/04-08.c b/k&r/04-funcs-and-prog-struct/04-08.c new file mode 100644 index 0000000..6b2781d --- /dev/null +++ b/k&r/04-funcs-and-prog-struct/04-08.c @@ -0,0 +1,22 @@ +#include + +int main() +{ + ; +} + +/* this handles EOF as wall */ +int buffc = -1; + +char getch() +{ + char temp; + return (buffc < 0) ? getchar() : (temp = buffc, buffc = -1, temp); +} + +void ungetch(int c) +{ + if (buffc >= 0) + printf("warning: overriding character buffer\n"); + buffc = c; +} diff --git a/k&r/04-funcs-and-prog-struct/04-12.c b/k&r/04-funcs-and-prog-struct/04-12.c new file mode 100644 index 0000000..86667a3 --- /dev/null +++ b/k&r/04-funcs-and-prog-struct/04-12.c @@ -0,0 +1,37 @@ +#include +#include +#include + +void itoar(char *s, int x); + +/* converts first arg to int, puts it to output with itoar */ +int main(int argc, char *argv[]) +{ + if (argc < 2) + return 1; + int x = atoi(argv[1]); + char s[strlen(argv[1])+1]; + itoar(s, x); + printf("%s\n", s); + return 0; +} + +char * utoar(char *s, unsigned x); + +/* itoa: writes int x to string s in decimal + * recursive implementation */ +void itoar(char *s, int x) +{ + unsigned y = (x < 0) ? *(s++) = '-', -x : x; + s = utoar(s, y); + *s = '\0'; +} + +/* utoar: helper for itoa */ +char * utoar(char *s, unsigned x) +{ + if (x / 10) + s = utoar(s, x / 10); + *s = x % 10 + '0'; + return s+1; +} diff --git a/k&r/04-funcs-and-prog-struct/04-13.c b/k&r/04-funcs-and-prog-struct/04-13.c new file mode 100644 index 0000000..256c0e2 --- /dev/null +++ b/k&r/04-funcs-and-prog-struct/04-13.c @@ -0,0 +1,28 @@ +#include + +int reverse(char string[], int i); + +/* reverses command line argument */ +int main(int argc, char *argv[]) +{ + if (argc < 2) + return 1; + printf("length : %d\n", reverse(argv[1], 0)); + printf("reversed : %s\n", argv[1]); + return 0; +} + +/* reverse: reverses string from index i, returns length + * recursive implementation */ +int reverse(char s[], int i) +{ + if (s[i] == '\0') { /* base case */ + return 0; + } + int j = reverse(s, i+1); + if (j > i) { /* swap elements */ + char c; + c = s[i], s[i] = s[j], s[j] = c; + } + return j+1; +} diff --git a/k&r/04-funcs-and-prog-struct/04-qsort.c b/k&r/04-funcs-and-prog-struct/04-qsort.c new file mode 100644 index 0000000..9382fd5 --- /dev/null +++ b/k&r/04-funcs-and-prog-struct/04-qsort.c @@ -0,0 +1,43 @@ +#include +#include + +void qsort_book(int v[], int left, int right); + +/* sorts command line arguments */ +int main(int argc, char *argv[]) +{ + int i, v[argc-1]; + for (i = 1; i < argc; i++) + v[i-1] = atoi(argv[i]); + qsort_book(v, 0, argc-2); + for (i = 0; i < argc-1; i++) + printf("%d ", v[i]); + putchar('\n'); + return 0; +} + +/* swap: swap v[i] and v[j] */ +void swap(int *v, int i, int j) +{ + int temp = v[i]; + v[i] = v[j], v[j] = temp; +} + +/* qsort: sort v[left]...v[right] into increasing order */ +void qsort_book(int v[], int left, int right) +{ + int i, last; + + if (left >= right) /* do nothing if array contains */ + return; /* fewer than two elements */ + swap(v, left, (left + right)/2); /* move partition elem */ + last = left; /* to v[0] */ + + for (i = left+1; i <= right; i++) /* partition */ + if (v[i] < v[left]) + swap(v, ++last, i); + swap(v, left, last); /* restore partition elem */ + + qsort_book(v, left, last-1); + qsort_book(v, last+1, right); +}