learning-cc4e

Solution to https://cc4e.com/. If you copy these you're not right in the head.
Log | Files | Refs

commit b4a1b9263cb20d3a5f2c96d18c742c2d5efd40dd
parent 2ada2f036f28109a5b717d992dd72c13ae87b76e
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Tue, 31 Oct 2023 18:47:30 +0200

Add everything I could from what I did thusfar.

Diffstat:
A01/k-n-r-1-1/output.txt | 1+
R01/k-n-r-1-1.c -> 01/k-n-r-1-1/solution.c | 0
A01/k-n-r-1-10/input.txt | 3+++
A01/k-n-r-1-10/output.txt | 8++++++++
A01/k-n-r-1-10/solution.c | 11+++++++++++
A01/k-n-r-1-17/main.c | 28++++++++++++++++++++++++++++
A01/k-n-r-1-17/output.txt | 6++++++
A01/k-n-r-1-17/solution.c | 18++++++++++++++++++
A01/k-n-r-1-3/output.txt | 10++++++++++
A01/k-n-r-1-3/solution.c | 9+++++++++
A01/k-n-r-1-4/output.txt | 6++++++
A01/k-n-r-1-4/solution.c | 10++++++++++
A01/k-n-r-1-5/output.txt | 16++++++++++++++++
A01/k-n-r-1-5/solution.c | 8++++++++
A01/k-n-r-1-6/input.txt | 4++++
A01/k-n-r-1-6/output.txt | 1+
A01/k-n-r-1-6/solution.c | 13+++++++++++++
A01/k-n-r-1-7/input.txt | 4++++
A01/k-n-r-1-7/output.txt | 4++++
A01/k-n-r-1-7/solution.c | 18++++++++++++++++++
A01/lbs290-4/output.txt | 3+++
A01/lbs290-4/solution.c | 20++++++++++++++++++++
A02/k-n-r-2-10/main.c | 8++++++++
A02/k-n-r-2-10/output.txt | 4++++
A02/k-n-r-2-10/solution.c | 7+++++++
A02/k-n-r-2-2/main.c | 8++++++++
A02/k-n-r-2-2/output.txt | 4++++
A02/k-n-r-2-2/solution.c | 34++++++++++++++++++++++++++++++++++
A02/lbs290-8/input.txt | 5+++++
A02/lbs290-8/output.txt | 2++
A02/lbs290-8/solution.c | 13+++++++++++++
A03/k-n-r-3-1/main.c | 13+++++++++++++
A03/k-n-r-3-1/output.txt | 4++++
A03/k-n-r-3-1/solution.c | 20++++++++++++++++++++
A03/k-n-r-3-4/main.c | 46++++++++++++++++++++++++++++++++++++++++++++++
A03/k-n-r-3-4/output.txt | 10++++++++++
A03/k-n-r-3-4/solution.c | 39+++++++++++++++++++++++++++++++++++++++
A03/k-n-r-3-6/input.txt | 10++++++++++
A03/k-n-r-3-6/output.txt | 6++++++
A03/k-n-r-3-6/solution.c | 21+++++++++++++++++++++
A03/lbs290-16/input.txt | 7+++++++
A03/lbs290-16/output.txt | 7+++++++
A03/lbs290-16/solution.c | 31+++++++++++++++++++++++++++++++
A04/4-a/main.c | 10++++++++++
A04/4-a/output.txt | 6++++++
A04/4-a/solution.c | 5+++++
A04/4-b/main.c | 9+++++++++
A04/4-b/output.txt | 5+++++
A04/4-b/solution.c | 5+++++
A04/4-c/main.c | 11+++++++++++
A04/4-c/output.txt | 5+++++
A04/4-c/solution.c | 11+++++++++++
A04/lbs290-18/input.txt | 16++++++++++++++++
A04/lbs290-18/output.txt | 3+++
A04/lbs290-18/solution.c | 34++++++++++++++++++++++++++++++++++
A05/lbs290-13/input.txt | 3+++
A05/lbs290-13/main.c | 12++++++++++++
A05/lbs290-13/output.txt | 3+++
A05/lbs290-13/solution.c | 9+++++++++
A05/lbs290-14/input.txt | 10++++++++++
A05/lbs290-14/output.txt | 17+++++++++++++++++
A05/lbs290-14/solution.c | 24++++++++++++++++++++++++
A05/lbs290-15/main.c | 10++++++++++
A05/lbs290-15/output.txt | 8++++++++
A05/lbs290-15/solution.c | 27+++++++++++++++++++++++++++
A05/lbs290-98/input.txt | 2++
A05/lbs290-98/output.txt | 2++
A05/lbs290-98/solution.c | 19+++++++++++++++++++
A05/rs-10/input.txt | 2++
A05/rs-10/output.txt | 2++
A05/rs-10/solution.c | 20++++++++++++++++++++
A05/rs-11/main.c | 8++++++++
A05/rs-11/output.txt | 1+
A05/rs-11/solution.c | 32++++++++++++++++++++++++++++++++
A05/rs-12/main.c | 9+++++++++
A05/rs-12/output.txt | 1+
A05/rs-12/solution.c | 19+++++++++++++++++++
A06/6-a/main.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A06/6-a/output.txt | 12++++++++++++
A06/6-a/solution.c | 36++++++++++++++++++++++++++++++++++++
A06/6-b/main.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
A06/6-b/output.txt | 15+++++++++++++++
A06/6-b/solution.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A06/6-o/main.c | 38++++++++++++++++++++++++++++++++++++++
A06/6-o/output.txt | 7+++++++
A06/6-o/solution.c | 30++++++++++++++++++++++++++++++
A07/01-str/main.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A07/01-str/output.txt | 5+++++
A07/01-str/solution.c | 25+++++++++++++++++++++++++
A07/02-list/main.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
A07/02-list/output.txt | 6++++++
A07/02-list/solution.c | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A07/03-dict/main.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A07/03-dict/output.txt | 12++++++++++++
A07/03-dict/solution.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A08/01-encapsulation/main.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A08/01-encapsulation/output.txt | 9+++++++++
A08/01-encapsulation/solution.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A08/02-iteration/main.c | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A08/02-iteration/output.txt | 15+++++++++++++++
A08/02-iteration/solution.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
101 files changed, 1934 insertions(+), 0 deletions(-)

diff --git a/01/k-n-r-1-1/output.txt b/01/k-n-r-1-1/output.txt @@ -0,0 +1 @@ +Hello world diff --git a/01/k-n-r-1-1.c b/01/k-n-r-1-1/solution.c diff --git a/01/k-n-r-1-10/input.txt b/01/k-n-r-1-10/input.txt @@ -0,0 +1,3 @@ +But soft what light +through yonder window +breaks diff --git a/01/k-n-r-1-10/output.txt b/01/k-n-r-1-10/output.txt @@ -0,0 +1,8 @@ +But +soft +what +light +through +yonder +window +breaks diff --git a/01/k-n-r-1-10/solution.c b/01/k-n-r-1-10/solution.c @@ -0,0 +1,11 @@ +#include <stdio.h> +main() { + int c; + while ((c = getchar()) != EOF) { + if (c == ' ') { + putchar('\n'); + } else { + putchar(c); + } + } +} diff --git a/01/k-n-r-1-17/main.c b/01/k-n-r-1-17/main.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <string.h> +int main() { + char t[1000]; + char *copy(); + void reverse(); + copy("Hello world", t); + printf("%s\n", t); + reverse(t); + printf("%s\n", t); + reverse(copy("XY", t)); + printf("%s\n", t); + reverse(copy("Even", t)); + printf("%s\n", t); + reverse(copy("Odd", t)); + printf("%s\n", t); + reverse(copy("civic", t)); + printf("%s\n", t); +} + +/* copy s1 to s2; assume s2 big enough */ +char *copy(s1, s2) +char s1[], s2[]; +{ + int i; + for(i=0;(s2[i] = s1[i]);i++); + return s2; +} diff --git a/01/k-n-r-1-17/output.txt b/01/k-n-r-1-17/output.txt @@ -0,0 +1,6 @@ +Hello world +dlrow olleH +YX +nevE +ddO +civic diff --git a/01/k-n-r-1-17/solution.c b/01/k-n-r-1-17/solution.c @@ -0,0 +1,18 @@ +void reverse(t) +char t[]; +{ + char *start = t; + char *end; + char temp; + + for (end = start; *end != '\0'; ++end) {}; + + --end; + + for (; start < end; ++start, --end) { + temp = *start; + *start = *end; + *end = temp; + } + return; +} diff --git a/01/k-n-r-1-3/output.txt b/01/k-n-r-1-3/output.txt @@ -0,0 +1,10 @@ +FAHR CELSIUS +--------------- + 0 -17.8 + 40 4.4 + 80 26.7 + 120 48.9 + 160 71.1 + 200 93.3 + 240 115.6 + 280 137.8 diff --git a/01/k-n-r-1-3/solution.c b/01/k-n-r-1-3/solution.c @@ -0,0 +1,8 @@ +#include <stdio.h> +main() /* Fahrenheit-Celsius table */ +{ + int fahr; + printf("FAHR CELSIUS\n---------------\n"); + for (fahr = 0; fahr <= 300; fahr = fahr + 40) + printf("%4d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); +} +\ No newline at end of file diff --git a/01/k-n-r-1-4/output.txt b/01/k-n-r-1-4/output.txt @@ -0,0 +1,6 @@ + 0 32.0 + 20 68.0 + 40 104.0 + 60 140.0 + 80 176.0 + 100 212.0 diff --git a/01/k-n-r-1-4/solution.c b/01/k-n-r-1-4/solution.c @@ -0,0 +1,9 @@ +#include <stdio.h> +main() /* Fix this to be Celsius-Fahrenheit table */ +{ + int cel; + for (cel = 0; + cel <= 100; + cel = cel + 20) + printf("%4d %6.1f\n", cel, (1.8*cel+32.0)); +} +\ No newline at end of file diff --git a/01/k-n-r-1-5/output.txt b/01/k-n-r-1-5/output.txt @@ -0,0 +1,16 @@ + 300 148.9 + 280 137.8 + 260 126.7 + 240 115.6 + 220 104.4 + 200 93.3 + 180 82.2 + 160 71.1 + 140 60.0 + 120 48.9 + 100 37.8 + 80 26.7 + 60 15.6 + 40 4.4 + 20 -6.7 + 0 -17.8 diff --git a/01/k-n-r-1-5/solution.c b/01/k-n-r-1-5/solution.c @@ -0,0 +1,7 @@ +#include <stdio.h> +main() /* Fahrenheit-Celsius table */ +{ + int fahr; + for (fahr = 300; fahr >= 0; fahr -= 20) + printf("%4d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); +} +\ No newline at end of file diff --git a/01/k-n-r-1-6/input.txt b/01/k-n-r-1-6/input.txt @@ -0,0 +1,4 @@ +But soft what light through yonder window breaks +It is the east and Juliet is the sun +Arise fair sun and kill the envious moon +Who is already sick and pale with grief diff --git a/01/k-n-r-1-6/output.txt b/01/k-n-r-1-6/output.txt @@ -0,0 +1 @@ +29 4 diff --git a/01/k-n-r-1-6/solution.c b/01/k-n-r-1-6/solution.c @@ -0,0 +1,12 @@ +#include <stdio.h> +main() /* count new lines in input */ +{ + int c, nl, nblanks; + nblanks = nl = 0; + while ((c = getchar()) != EOF) { + if (c == '\n') + ++nl; + if (c == ' ') ++nblanks; +} + printf("%d %d\n", nblanks, nl); +} +\ No newline at end of file diff --git a/01/k-n-r-1-7/input.txt b/01/k-n-r-1-7/input.txt @@ -0,0 +1,4 @@ +But soft what light through yonder window breaks +It is the east and Juliet is the sun +Arise fair sun and kill the envious moon +Who is already sick and pale with grief diff --git a/01/k-n-r-1-7/output.txt b/01/k-n-r-1-7/output.txt @@ -0,0 +1,4 @@ +But soft what light through yonder window breaks +It is the east and Juliet is the sun +Arise fair sun and kill the envious moon +Who is already sick and pale with grief diff --git a/01/k-n-r-1-7/solution.c b/01/k-n-r-1-7/solution.c @@ -0,0 +1,18 @@ +#include <stdio.h> +main() { + int c; + char last_white = 0; + while ((c = getchar()) != EOF) { + if (last_white) { + if (c != ' ') { + last_white = 0; + putchar(c); + } + } else if (c == ' ') { + putchar(c); + last_white = 1; + } else { + putchar(c); + } + } +} diff --git a/01/lbs290-4/output.txt b/01/lbs290-4/output.txt @@ -0,0 +1,3 @@ +Hello there and welcome to the program +This statement should print out, why doesn't it? +This statement does print out, yay! diff --git a/01/lbs290-4/solution.c b/01/lbs290-4/solution.c @@ -0,0 +1,20 @@ +/* Assignment 4 LBS 290 - Fall 1991 - syntax errors */ + +#include "stdio.h" + +main () { + char c; + int value; + int i; + float x; + + printf("Hello there and welcome to the program\n"); + i = 10; + x = 2.50; + x = x + 1.; + value = 15; +/* This is a comment */ + printf("This statement should print out, why doesn't it?\n"); +/* And another comment */ + printf("This statement does print out, yay!\n"); +} diff --git a/02/k-n-r-2-10/main.c b/02/k-n-r-2-10/main.c @@ -0,0 +1,8 @@ +#include <stdio.h> +int main() { + int lower(); + printf("Lower M is %c\n", lower('M')); + printf("Lower x is %c\n", lower('x')); + printf("Lower @ is %c\n", lower('@')); + printf("Lower M is %c\n", lower('M')); +} diff --git a/02/k-n-r-2-10/output.txt b/02/k-n-r-2-10/output.txt @@ -0,0 +1,4 @@ +Lower M is m +Lower x is x +Lower @ is @ +Lower M is m diff --git a/02/k-n-r-2-10/solution.c b/02/k-n-r-2-10/solution.c @@ -0,0 +1,7 @@ +int lower(c) /* convert c to lower case; ASCII only */ +int c; +{ + return((c >= 'A' && c <= 'Z') + ? (c + 'a' - 'A') + : (c)); +} diff --git a/02/k-n-r-2-2/main.c b/02/k-n-r-2-2/main.c @@ -0,0 +1,8 @@ +#include <stdio.h> +int main() { + int htoi(); + printf("htoi('c077b') = %d\n", htoi("c077b")); + printf("htoi('f') = %d\n", htoi("f")); + printf("htoi('F0') = %d\n", htoi("F0")); + printf("htoi('12fab') = %d\n", htoi("12fab")); +} diff --git a/02/k-n-r-2-2/output.txt b/02/k-n-r-2-2/output.txt @@ -0,0 +1,4 @@ +htoi('c077b') = 788347 +htoi('f') = 15 +htoi('F0') = 240 +htoi('12fab') = 77739 diff --git a/02/k-n-r-2-2/solution.c b/02/k-n-r-2-2/solution.c @@ -0,0 +1,34 @@ +int atoi(s) /* convert s to integer */ +char s[]; +{ + int i, n; + + n = 0; + for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i) + n = 10 * n + s[i] - '0'; + return(n); +} + +int htoi(s) + char s[]; +{ + int n; + char *p; + + n = 0; + for (p = s; + (*p >= '0' && *p <= '9') + || (*p >= 'a' && *p <= 'f') + || (*p >= 'A' && *p <= 'F'); + ++p) { + n *= 16; + if (*p >= '0' && *p <= '9') { + n += *p - '0'; + } else if (*p >= 'a' && *p <= 'f') { + n += *p - 'a' + 10; + } else { + n += *p - 'A' + 10; + } + } + return(n); +} diff --git a/02/lbs290-8/input.txt b/02/lbs290-8/input.txt @@ -0,0 +1,5 @@ +10.0 +5.0 +15.0 +20.0 +10.0 diff --git a/02/lbs290-8/output.txt b/02/lbs290-8/output.txt @@ -0,0 +1,2 @@ +The total is: 60.0 +The average is: 12.0 diff --git a/02/lbs290-8/solution.c b/02/lbs290-8/solution.c @@ -0,0 +1,13 @@ +#include "stdio.h" + +main () { + int scanned, number_of_numbers = 0; + double input_number, total, average; + while ((scanned = scanf("%lf", &input_number)) != EOF) { + total += input_number; + ++number_of_numbers; + } + average = total / number_of_numbers; + printf("The total is: %.1f\n", total); + printf("The average is: %.1f\n", average); +} diff --git a/03/k-n-r-3-1/main.c b/03/k-n-r-3-1/main.c @@ -0,0 +1,13 @@ +#include <stdio.h> +int main() { + char t[1000]; + void expand(); + expand("Hello world", t); + printf("%s\n", t); + expand("Hello world\n", t); + printf("%s\n", t); + expand("Hello\tworld\n", t); + printf("%s\n", t); + expand("Hello\tworld\nHave a nice\tday\n", t); + printf("%s\n", t); +} diff --git a/03/k-n-r-3-1/output.txt b/03/k-n-r-3-1/output.txt @@ -0,0 +1,4 @@ +Hello world +Hello world\n +Hello\tworld\n +Hello\tworld\nHave a nice\tday\n diff --git a/03/k-n-r-3-1/solution.c b/03/k-n-r-3-1/solution.c @@ -0,0 +1,20 @@ +void expand(s, t) +char s[], t[]; +{ + for(; *s; s++) { + switch (*s) { + case '\t': + *t++ = '\\'; + *t++ = 't'; + break; + case '\n': + *t++ = '\\'; + *t++ = 'n'; + break; + default: + *t++ = *s; + break; + } + } + *t = '\0'; +} diff --git a/03/k-n-r-3-4/main.c b/03/k-n-r-3-4/main.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <string.h> +int main() { + char s[1000]; + void itob(), itoh(), reverse(); + + itob(42,s); + printf("42 in base-2 is %s\n", s); + itoh(42,s); + printf("42 in base-16 is %s\n", s); + + itob(16,s); + printf("16 in base-2 is %s\n", s); + itoh(16,s); + printf("16 in base-16 is %s\n", s); + + itob(59,s); + printf("59 in base-2 is %s\n", s); + itoh(59,s); + printf("59 in base-16 is %s\n", s); + + itob(100,s); + printf("100 in base-2 is %s\n", s); + itoh(100,s); + printf("100 in base-16 is %s\n", s); + + itob(254,s); + printf("254 in base-2 is %s\n", s); + itoh(254,s); + printf("254 in base-16 is %s\n", s); +} + +void reverse(t) +char t[]; +{ + int i, j, len; + char tmp; + len = strlen(t); + for(i=0, j=len-1;;i++,j--) { + if (j<i) break; + tmp = t[i]; + t[i] = t[j]; + t[j] = tmp; + } + return; +} diff --git a/03/k-n-r-3-4/output.txt b/03/k-n-r-3-4/output.txt @@ -0,0 +1,10 @@ +42 in base-2 is 101010 +42 in base-16 is 2a +16 in base-2 is 10000 +16 in base-16 is 10 +59 in base-2 is 111011 +59 in base-16 is 3b +100 in base-2 is 1100100 +100 in base-16 is 64 +254 in base-2 is 11111110 +254 in base-16 is fe diff --git a/03/k-n-r-3-4/solution.c b/03/k-n-r-3-4/solution.c @@ -0,0 +1,39 @@ +void reverse(); + +void itob(n, s) +int n; +char s[]; +{ + char *s_ptr; + s_ptr = s; + + do { + *s_ptr++ = (n % 2) + '0'; + n /= 2; + } while (n != 0); + + *s_ptr = '\0'; + + reverse(s); +} +void itoh(n, s) +int n; +char s[]; +{ + char *s_ptr; + s_ptr = s; + + do { + *s_ptr++ = (n % 16) + + ( + ((n % 16) < 10) + ? '0' + : ('a' - 10) + ); + n /= 16; + } while (n != 0); + + *s_ptr = '\0'; + + reverse(s); +} diff --git a/03/k-n-r-3-6/input.txt b/03/k-n-r-3-6/input.txt @@ -0,0 +1,10 @@ +One line +Two Line +Red Line +Blue line +Blue line +Blue line +Yada +Yada +Yada +Last line diff --git a/03/k-n-r-3-6/output.txt b/03/k-n-r-3-6/output.txt @@ -0,0 +1,6 @@ +One line +Two Line +Red Line +Blue line +Yada +Last line diff --git a/03/k-n-r-3-6/solution.c b/03/k-n-r-3-6/solution.c @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <string.h> +int main() { + char line[1000]; + char keep[1000]; + + if (gets(line) != NULL) { + printf("%s\n", line); + strcpy(keep, line); + } else { + return; + } + + while (gets(line) != NULL) { + if (strcmp(keep, line) == 0) { + continue; + } + strcpy(keep, line); + printf("%s\n", line); + } +} diff --git a/03/lbs290-16/input.txt b/03/lbs290-16/input.txt @@ -0,0 +1,7 @@ += 6.0 +* 7.0 += 1.23 ++ 5.0 +/ 2.0 +* 6.3 +- 4.0 diff --git a/03/lbs290-16/output.txt b/03/lbs290-16/output.txt @@ -0,0 +1,7 @@ +Display: 6.00 +Display: 42.00 +Display: 1.23 +Display: 6.23 +Display: 3.12 +Display: 19.62 +Display: 15.62 diff --git a/03/lbs290-16/solution.c b/03/lbs290-16/solution.c @@ -0,0 +1,31 @@ +#include <stdio.h> + +int main() +{ + char line[256]; + char opcode; + float value, display = 0.0; + + while(fgets(line, 256, stdin) != NULL) { + /* Use sscanf to parse data from a string. */ + sscanf(line, "%c %f", &opcode, &value); + if ( opcode == 'S' ) { + break; + } else if ( opcode == '=' ) { + display = value; + } else if ( opcode == '+' ) { + display += value; + } else if ( opcode == '-' ) { + display -= value; + } else if ( opcode == '*' ) { + display *= value; + } else if ( opcode == '/' ) { + display /= value; + } else { + printf("Bad operation: %c\n", opcode); + return(-1); + } + + printf("Display: %.2f\n", display); + } +} diff --git a/04/4-a/main.c b/04/4-a/main.c @@ -0,0 +1,10 @@ +#include <stdio.h> +int main() { + double faren(); + printf("faren(42) is %.1f\n", faren(42.0)); + printf("faren(0) is %.1f\n", faren(0.0)); + printf("faren(-10) is %.1f\n", faren(-10.0)); + printf("faren(32) is %.1f\n", faren(32.0)); + printf("faren(100) is %.1f\n", faren(100.0)); + printf("faren(212) is %.1f\n", faren(212.0)); +} diff --git a/04/4-a/output.txt b/04/4-a/output.txt @@ -0,0 +1,6 @@ +faren(42) is 107.6 +faren(0) is 32.0 +faren(-10) is 14.0 +faren(32) is 89.6 +faren(100) is 212.0 +faren(212) is 413.6 diff --git a/04/4-a/solution.c b/04/4-a/solution.c @@ -0,0 +1,5 @@ +double faren(celsius) +double celsius; +{ + return(1.8 * celsius + 32.0); +} diff --git a/04/4-b/main.c b/04/4-b/main.c @@ -0,0 +1,9 @@ +#include <stdio.h> +int main() { + int bump(); + printf("bump() returns %d\n", bump()); + printf("bump() returns %d\n", bump()); + printf("bump() returns %d\n", bump()); + printf("bump() returns %d\n", bump()); + printf("bump() returns %d\n", bump()); +} diff --git a/04/4-b/output.txt b/04/4-b/output.txt @@ -0,0 +1,5 @@ +bump() returns 0 +bump() returns 1 +bump() returns 2 +bump() returns 3 +bump() returns 4 diff --git a/04/4-b/solution.c b/04/4-b/solution.c @@ -0,0 +1,5 @@ +extern int counter = 0; +int bump() +{ + return(counter++); +} diff --git a/04/4-c/main.c b/04/4-c/main.c @@ -0,0 +1,11 @@ +#include <stdio.h> +int main() { + int bump(); + void start(); + printf("bump() returns %d\n", bump()); + printf("bump() returns %d\n", bump()); + printf("bump() returns %d\n", bump()); + start(42); + printf("bump() returns %d\n", bump()); + printf("bump() returns %d\n", bump()); +} diff --git a/04/4-c/output.txt b/04/4-c/output.txt @@ -0,0 +1,5 @@ +bump() returns 0 +bump() returns 1 +bump() returns 2 +bump() returns 42 +bump() returns 43 diff --git a/04/4-c/solution.c b/04/4-c/solution.c @@ -0,0 +1,11 @@ +static int counter = 0; + +int bump() +{ + return(counter++); +} + +void start(value) +{ + counter = value; +} diff --git a/04/lbs290-18/input.txt b/04/lbs290-18/input.txt @@ -0,0 +1,16 @@ +* Beginning +0 = 72 +1 = 101 +2 = 108 +3 = 108 +4 = 108 +4 + 3 +5 = 10 +6 = 100 +6 + 19 +7 = 111 +8 = 114 +9 = 108 +10 = 101 +10 - 1 +X diff --git a/04/lbs290-18/output.txt b/04/lbs290-18/output.txt @@ -0,0 +1,3 @@ +Memory: +Hello +world diff --git a/04/lbs290-18/solution.c b/04/lbs290-18/solution.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +int main() +{ + char line[256]; + char memory[256]; + char opcode; + int count,address,value; + + while(fgets(line, 256, stdin) != NULL) { + /* printf("\nLine: %s\n",line); */ + if ( line[0] == 'X' ) break; + if ( line[0] == '*' ) { + continue; + } + count = sscanf(line, "%d %c %d", &address, &opcode, &value); + if ( count != 3 ) continue; + /* printf("address: %d opcode: %c value: %d\n", address, opcode, value); */ + + if ( opcode == '=' ) { + memory[address] = value; + } else if ( opcode == '+' ) { + memory[address] += value; + } else if ( opcode == '-' ) { + memory[address] -= value; + } else { + printf("Bad opcode: %c\n", opcode); + return -1; + } + + /* printf("Memory:\n%s\n", memory); */ + } + printf("Memory:\n%s\n", memory); +} diff --git a/05/lbs290-13/input.txt b/05/lbs290-13/input.txt @@ -0,0 +1,3 @@ +123 5.00 40 +100 4.00 45 +199 5.25 10 diff --git a/05/lbs290-13/main.c b/05/lbs290-13/main.c @@ -0,0 +1,12 @@ +#include <stdio.h> +main() { + int empno; + float rate, hours, pay; + void calcpay(); + + while(1) { + if ( scanf("%d %f %f",&empno,&rate,&hours) < 3 ) break; + calcpay(&pay, rate, hours); + printf("Employee=%d Rate=%.2f Hours=%.2f Pay=%.2f\n",empno, rate, hours, pay); + } +} diff --git a/05/lbs290-13/output.txt b/05/lbs290-13/output.txt @@ -0,0 +1,3 @@ +Employee=123 Rate=5.00 Hours=40.00 Pay=200.00 +Employee=100 Rate=4.00 Hours=45.00 Pay=190.00 +Employee=199 Rate=5.25 Hours=10.00 Pay=52.50 diff --git a/05/lbs290-13/solution.c b/05/lbs290-13/solution.c @@ -0,0 +1,9 @@ +void calcpay(p,r,h) + float *p,r,h; +{ + if (h <= 40) { + *p = r * h; + } else { + *p = 1.5 * r * (h - 40 ) + (r * 40); + } +} diff --git a/05/lbs290-14/input.txt b/05/lbs290-14/input.txt @@ -0,0 +1,10 @@ +9 +5 +100 +16 +18 +20 +6 +100 +1 +77 diff --git a/05/lbs290-14/output.txt b/05/lbs290-14/output.txt @@ -0,0 +1,17 @@ +numb[9] = 77 +numb[8] = 1 +numb[7] = 100 +numb[6] = 6 +numb[5] = 20 +numb[4] = 18 +numb[3] = 16 +numb[2] = 100 +numb[1] = 5 +numb[0] = 9 + +Searching for entries equal to 100 + +Found 100 at 2 +Found 100 at 7 + +Found 2 entries with 100 diff --git a/05/lbs290-14/solution.c b/05/lbs290-14/solution.c @@ -0,0 +1,24 @@ +#include <stdio.h> +int main() { + int i, v, arr[10], count_100 = 0, hundreds_indices[10]; + + for (i=0;i<10;i++) { + scanf("%d", &v); + arr[i] = v; + } + + for (--i; i>=0; --i) { + printf("numb[%d] = %d\n", i, arr[i]); + if (arr[i] == 100) { + hundreds_indices[count_100++] = i; + } + } + + printf("\nSearching for entries equal to 100\n\n"); + + for (i = count_100 - 1; i >= 0; --i) { + printf("Found 100 at %d\n", hundreds_indices[i]); + } + + printf("\nFound %d entries with 100\n", count_100); +} diff --git a/05/lbs290-15/main.c b/05/lbs290-15/main.c @@ -0,0 +1,10 @@ +#include <stdio.h> +#include <string.h> +int main() { + char line[1000]; + void process(); + strcpy(line,"Hi there and welcome to LBS290"); + process(line); + strcpy(line,"I love C"); + process(line); +} diff --git a/05/lbs290-15/output.txt b/05/lbs290-15/output.txt @@ -0,0 +1,8 @@ +String: Hi there and welcome to LBS290 +Count=30 +The ninth character is: a +String: Hi-there-and-welcome-to-LBS290 + +String: I love C +Count=8 +String: I-love-C diff --git a/05/lbs290-15/solution.c b/05/lbs290-15/solution.c @@ -0,0 +1,27 @@ +void process(line) + char line[]; +{ + char *line_ptr, ninth_char; + int char_count = 0; + + printf("\nString: "); + + for (line_ptr = line; *line_ptr != '\0'; ++line_ptr) { + ++char_count; + if (char_count == 10) { + ninth_char = *line_ptr; + } + putchar(*line_ptr); + if (*line_ptr == ' ') { + *line_ptr = '-'; + } + } + + printf("\nCount=%d\n", char_count); + + if (char_count > 10) { + printf("The ninth character is: %c\n", ninth_char); + } + + printf("String: %s\n", line); +} diff --git a/05/lbs290-98/input.txt b/05/lbs290-98/input.txt @@ -0,0 +1,2 @@ +42 > 114 > 105 > 97 > +110 < < < < 66 diff --git a/05/lbs290-98/output.txt b/05/lbs290-98/output.txt @@ -0,0 +1,2 @@ +Memory: +Brian diff --git a/05/lbs290-98/solution.c b/05/lbs290-98/solution.c @@ -0,0 +1,19 @@ +#include <stdio.h> + +int main() +{ + char memory[256], token[256]; + int position = 0, value; + + while(scanf("%s", token) == 1 ) { + if (token[0] == '>') { + ++position; + } else if (token[0] == '<') { + --position; + } else { + memory[position] = atoi(token); + } + } + + printf("Memory:\n%s\n", memory); +} diff --git a/05/rs-10/input.txt b/05/rs-10/input.txt @@ -0,0 +1,2 @@ +Kernighan +Ritchie diff --git a/05/rs-10/output.txt b/05/rs-10/output.txt @@ -0,0 +1,2 @@ +Enter two strings +Kernighan & Ritchie diff --git a/05/rs-10/solution.c b/05/rs-10/solution.c @@ -0,0 +1,20 @@ +#include <stdio.h> +int main() { + char a[100], b[100]; + int c; + int i; + + printf("Enter two strings\n"); + + for (i = 0; (c = getchar()) != '\n'; ++i) { + a[i] = c; + } + a[i++] = '\0'; + + for (i = 0; (c = getchar()) != '\n'; ++i) { + b[i] = c; + } + b[i++] = '\0'; + + printf("%s & %s\n", a, b); +} diff --git a/05/rs-11/main.c b/05/rs-11/main.c @@ -0,0 +1,8 @@ +#include <stdio.h> +#include <string.h> +int main() { + char s1[] = " Hello World "; + void py_rstrip(); + py_rstrip(s1); + printf("-%s-\n", s1); +} diff --git a/05/rs-11/output.txt b/05/rs-11/output.txt @@ -0,0 +1 @@ +- Hello World- diff --git a/05/rs-11/solution.c b/05/rs-11/solution.c @@ -0,0 +1,32 @@ +void py_rstrip_quote(inp) +char inp[]; +{ + if (inp[0] == '\0') { + if (inp[-1] == ' ' + || inp[-1] == '\t' + || inp[-1] == '\n') { + printf("%c\n", inp[-1]); + inp[-1] = '\0'; + } + } else { + py_rstrip_quote(inp + 1); + } +} + +void py_rstrip(inp) + char inp[]; +{ + /* + if (inp[0] == '\0') { + return; + } + py_rstrip_quote(inp + 1); + */ + char *last_non_space_location; + for (last_non_space_location = inp; *inp != '\0'; ++inp) { + if (*inp != ' ') { + last_non_space_location = inp; + } + } + last_non_space_location[1] = '\0'; +} diff --git a/05/rs-12/main.c b/05/rs-12/main.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <string.h> +#include <ctype.h> +int main() { + char s1[] = " Hello World "; + void py_lstrip(); + py_lstrip(s1); + printf("-%s-\n", s1); +} diff --git a/05/rs-12/output.txt b/05/rs-12/output.txt @@ -0,0 +1 @@ +-Hello World - diff --git a/05/rs-12/solution.c b/05/rs-12/solution.c @@ -0,0 +1,19 @@ +void py_lstrip(inp) + char inp[]; +{ + char *first_non_space_location; + + for (first_non_space_location = inp; + *first_non_space_location != '\0' + && *first_non_space_location == ' '; + ++first_non_space_location) { + } + + for (; + *first_non_space_location != '\0'; + ++inp, ++first_non_space_location) { + *inp = *first_non_space_location; + } + + *inp = '\0'; +} diff --git a/06/6-a/main.c b/06/6-a/main.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <stdlib.h> + +struct lnode { + int value; + struct lnode *next; +}; + +struct list { + struct lnode *head; + struct lnode *tail; +}; + +int main() +{ + void list_add(); + void list_dump(); + struct lnode * list_find(); + + struct list mylist; + struct lnode * mynode; + + mylist.head = NULL; + mylist.tail = NULL; + + list_add(&mylist, 10); + list_add(&mylist, 20); + list_add(&mylist, 30); + list_dump(&mylist); + + mynode = list_find(&mylist, 42); + if ( mynode == NULL ) { + printf("Did not find 42\n"); + } else { + printf("Looked for 42, found %d\n", mynode->value); + } + + mynode = list_find(&mylist, 30); + if ( mynode == NULL || mynode->value != 30) { + printf("Did not find 30\n"); + } else { + printf("Found 30\n"); + } + + list_add(&mylist, 40); + list_dump(&mylist); + +} + +void list_dump(lst) + struct list *lst; +{ + struct lnode *cur; + printf("\nDump:\n"); + for(cur=lst->head; cur != NULL; cur=cur->next) { + printf(" %d\n", cur->value); + } +} diff --git a/06/6-a/output.txt b/06/6-a/output.txt @@ -0,0 +1,12 @@ +Dump: + 10 + 20 + 30 +Did not find 42 +Found 30 + +Dump: + 10 + 20 + 30 + 40 diff --git a/06/6-a/solution.c b/06/6-a/solution.c @@ -0,0 +1,36 @@ +void list_add(lst, value) + struct list *lst; + int value; +{ + struct lnode *new_node = ( + (struct lnode *) + malloc(sizeof(struct lnode)) + ); + new_node->value = value; + new_node->next = NULL; + + if (lst->head == NULL) { + lst->head = new_node; + } + if (lst->tail == NULL) { + lst->tail = new_node; + } else { + lst->tail->next = new_node; + lst->tail = new_node; + } +} + +struct lnode * list_find(lst, value) + struct list *lst; + int value; +{ + struct lnode *node_walker; + for (node_walker = lst->head; + node_walker != NULL; + node_walker = node_walker->next) { + if (node_walker->value == value) { + return node_walker; + } + } + return NULL; +} diff --git a/06/6-b/main.c b/06/6-b/main.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <stdlib.h> + +struct lnode { + int value; + struct lnode *next; +}; + +struct list { + struct lnode *head; + struct lnode *tail; +}; + +int main() +{ + void list_add(); + void list_dump(); + struct lnode * list_find(); + + struct list mylist; + struct lnode * mynode; + + mylist.head = NULL; + mylist.tail = NULL; + + list_add(&mylist, 10); + list_add(&mylist, 20); + list_add(&mylist, 30); + list_dump(&mylist); + + list_remove(&mylist, 42); + + list_remove(&mylist, 10); + list_dump(&mylist); + + list_remove(&mylist, 30); + list_dump(&mylist); + + list_add(&mylist, 40); + list_dump(&mylist); + +} + +void list_dump(lst) + struct list *lst; +{ + struct lnode *cur; + printf("\nDump:\n"); + for(cur=lst->head; cur != NULL; cur=cur->next) { + printf(" %d\n", cur->value); + } +} diff --git a/06/6-b/output.txt b/06/6-b/output.txt @@ -0,0 +1,15 @@ +Dump: + 10 + 20 + 30 + +Dump: + 20 + 30 + +Dump: + 20 + +Dump: + 20 + 40 diff --git a/06/6-b/solution.c b/06/6-b/solution.c @@ -0,0 +1,57 @@ +void list_add(lst, value) + struct list *lst; + int value; +{ + struct lnode *new_node = ( + (struct lnode *) + malloc(sizeof(struct lnode)) + ); + new_node->value = value; + new_node->next = NULL; + + if (lst->head == NULL) { + lst->head = new_node; + } + + if (lst->tail == NULL) { + lst->tail = new_node; + } else { + lst->tail->next = new_node; + lst->tail = new_node; + } +} + +void list_remove(lst, value) + struct list *lst; + int value; +{ + struct lnode *previous_lnode = NULL, *current_lnode = NULL; + + current_lnode = lst->head; + while (current_lnode != NULL) { + if (current_lnode->value == value) { + if (lst->head == lst->tail) { + free(current_lnode); + current_lnode = NULL; + lst->head = NULL; + lst->tail = NULL; + } else if (lst->head == current_lnode) { + lst->head = current_lnode->next; + free(current_lnode); + current_lnode = lst->head; + } else if (lst->tail == current_lnode) { + lst->tail = previous_lnode; + previous_lnode->next = NULL; + free(current_lnode); + current_lnode = NULL; + } else { + previous_lnode->next = current_lnode->next; + free(current_lnode); + current_lnode = previous_lnode->next; + } + } else { + previous_lnode = current_lnode; + current_lnode = current_lnode->next; + } + } +} diff --git a/06/6-o/main.c b/06/6-o/main.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <stdlib.h> + +struct simpledate { + int day; + int month; + int year; +}; + +static int day_tab[2][13] = { + {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, + {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} +}; + + +main() { + void dump_date(); + printf("Playing with structures\n"); + struct simpledate sd; + + sd.year = 2023; + sd.month = 2; + sd.day = 11; + dump_date(&sd); + printf("Day of year %d\n", day_of_year(&sd)); + + sd.year = 2023; + sd.month = 9; + sd.day = 15; + dump_date(&sd); + printf("Day of year %d\n", day_of_year(&sd)); + + sd.year = 2024; + sd.month = 9; + sd.day = 15; + dump_date(&sd); + printf("Day of year %d\n", day_of_year(&sd)); +} diff --git a/06/6-o/output.txt b/06/6-o/output.txt @@ -0,0 +1,7 @@ +Playing with structures +2023/02/11 +Day of year 42 +2023/09/15 +Day of year 258 +2024/09/15 +Day of year 259 diff --git a/06/6-o/solution.c b/06/6-o/solution.c @@ -0,0 +1,30 @@ +int day_of_year(pd) /* set day of year from month, day */ +struct simpledate *pd; +{ + int i, day_number, leap; + + day_number = pd->day; + + leap = ( + ( + ((pd->year % 4) == 0) + && ((pd->year % 100) != 0) + ) + || ((pd->year % 400) == 0) + ); + + for (i = 1; i < pd->month; ++i) { + day_number += day_tab[leap][i]; + } + + return day_number; +} + +void dump_date(pd) /* print date from year, month, day */ +struct simpledate *pd; +{ + printf("%d/%02d/%02d\n", + pd->year, + pd->month, + pd->day); +} diff --git a/07/01-str/main.c b/07/01-str/main.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> + +struct pystr +{ + int length; + int alloc; /* the length of *data */ + char *data; +}; + +/* Constructor - x = str() */ +struct pystr * pystr_new() { + struct pystr *p = malloc(sizeof(*p)); + p->length = 0; + p->alloc = 10; + p->data = malloc(10); + p->data[0] = '\0'; + return p; +} + +/* Destructor - del(x) */ +void pystr_del(const struct pystr* self) { + free((void *)self->data); /* free string first */ + free((void *)self); +} + +void pystr_dump(const struct pystr* self) +{ + printf("Pystr length=%d alloc=%d data=%s\n", + self->length, self->alloc, self->data); +} + +int pystr_len(const struct pystr* self) +{ + return self->length; +} + +char *pystr_str(const struct pystr* self) +{ + return self->data; +} + + +int main(void) +{ + setvbuf(stdout, NULL, _IONBF, 0); /* Internal */ + + struct pystr * x = pystr_new(); + pystr_dump(x); + + pystr_append(x, 'H'); + pystr_dump(x); + + pystr_appends(x, "ello world"); + pystr_dump(x); + + pystr_assign(x, "A completely new string"); + printf("String = %s\n", pystr_str(x)); + printf("Length = %d\n", pystr_len(x)); + pystr_del(x); +} diff --git a/07/01-str/output.txt b/07/01-str/output.txt @@ -0,0 +1,5 @@ +Pystr length=0 alloc=10 data= +Pystr length=1 alloc=10 data=H +Pystr length=11 alloc=20 data=Hello world +String = A completely new string +Length = 23 diff --git a/07/01-str/solution.c b/07/01-str/solution.c @@ -0,0 +1,25 @@ +/* x = x + 'h'; */ + +void pystr_append(struct pystr* self, char ch) { + if (self->length + 1 >= self->alloc) { + self->alloc += 10; + self->data = realloc(self->data, self->alloc); + } + self->data[self->length] = ch; + self->data[++self->length] = '\0'; +} + +/* x = x + "hello"; */ + +void pystr_appends(struct pystr* self, char *str) { + for (; *str != '\0'; ++str) { + pystr_append(self, *str); + } +} + +/* x = "hello"; */ + +void pystr_assign(struct pystr* self, char *str) { + self->length = 0; + pystr_appends(self, str); +} diff --git a/07/02-list/main.c b/07/02-list/main.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct lnode { + char *text; + struct lnode *next; +}; + +struct pylist { + struct lnode *head; + struct lnode *tail; + int count; +}; + +/* Constructor - lst = list() */ +struct pylist * pylist_new() { + struct pylist *p = malloc(sizeof(*p)); + p->head = NULL; + p->tail = NULL; + p->count = 0; + return p; +} + +/* Destructor - del(lst) */ +void pylist_del(struct pylist* self) { + struct lnode *cur, *next; + cur = self->head; + while(cur) { + free(cur->text); + next = cur->next; + free(cur); + cur = next; + } + free((void *)self); +} + +int main(void) +{ + setvbuf(stdout, NULL, _IONBF, 0); /* Internal */ + + struct pylist * lst = pylist_new(); + pylist_append(lst, "Hello world"); + pylist_print(lst); + pylist_append(lst, "Catch phrase"); + pylist_print(lst); + pylist_append(lst, "Brian"); + pylist_print(lst); + printf("Length = %d\n", pylist_len(lst)); + printf("Brian? %d\n", pylist_index(lst, "Brian")); + printf("Bob? %d\n", pylist_index(lst, "Bob")); + pylist_del(lst); +} diff --git a/07/02-list/output.txt b/07/02-list/output.txt @@ -0,0 +1,6 @@ +['Hello world'] +['Hello world', 'Catch phrase'] +['Hello world', 'Catch phrase', 'Brian'] +Length = 3 +Brian? 2 +Bob? -1 diff --git a/07/02-list/solution.c b/07/02-list/solution.c @@ -0,0 +1,79 @@ +/* print(lst) */ +void pylist_print(struct pylist* self) +{ + /* About 10 lines of code + The output should match Python's + list output + + ['Hello world', 'Catch phrase'] + + Use printf cleverly, *not* string + concatenation since this is C, not Python. + */ + struct lnode *current_node; + printf("["); + for (current_node = self->head; + current_node != NULL; + current_node = current_node->next) { + if (current_node->next != NULL) { + printf("'%s', ", current_node->text); + } else { + printf("'%s'", current_node->text); + } + } + printf("]\n"); +} + +/* len(lst) */ +int pylist_len(const struct pylist* self) +{ + /* One line of code */ + struct lnode *current_node = self->head; + int length = 0; + while (current_node != NULL) { + ++length; + current_node = current_node->next; + } + + return length; +} + +/* lst.append("Hello world") */ +void pylist_append(struct pylist* self, char *str) { + /* Review: Chapter 6 lectures and assignments */ + char *new_str = ( + (char *) + malloc(sizeof(char) * (strlen(str) + 1)) + ); + new_str = strcpy(new_str, str); + struct lnode *new_node = ( + (struct lnode *) + malloc(sizeof(struct lnode)) + ); + new_node->text = new_str; + if (self->head == NULL) { + self->head = new_node; + } + if (self->tail != NULL) { + self->tail->next = new_node; + } + self->tail = new_node; +} +/* lst.index("Hello world") - if not found -1 */ +int pylist_index(struct pylist* self, char *str) +{ + /* Seven or so lines of code */ + struct lnode *current_node = self->head; + int index = 0; + + for (current_node = self->head, + index = 0; + current_node != NULL; + current_node = current_node->next, + ++index) { + if (strcmp(current_node->text, str) == 0) { + return index; + } + } + return -1; +} diff --git a/07/03-dict/main.c b/07/03-dict/main.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct dnode { + char *key; + char *value; + struct dnode *next; +}; + +struct pydict { + struct dnode *head; + struct dnode *tail; + int count; +}; + +/* Constructor - dct = dict() */ +struct pydict * pydict_new() { + struct pydict *p = malloc(sizeof(*p)); + p->head = NULL; + p->tail = NULL; + p->count = 0; + return p; +} + +/* Destructor - del(dct) */ +void pydict_del(struct pydict* self) { + struct dnode *cur, *next; + cur = self->head; + while(cur) { + free(cur->key); + free(cur->value); + next = cur->next; + free(cur); + cur = next; + } + free((void *)self); +} + +int main(void) +{ + struct dnode * cur; + struct pydict * dct = pydict_new(); + + setvbuf(stdout, NULL, _IONBF, 0); /* Internal */ + + pydict_put(dct, "z", "Catch phrase"); + pydict_print(dct); + pydict_put(dct, "z", "W"); + pydict_print(dct); + pydict_put(dct, "y", "B"); + pydict_put(dct, "c", "C"); + pydict_put(dct, "a", "D"); + pydict_print(dct); + printf("Length =%d\n",pydict_len(dct)); + + printf("z=%s\n", pydict_get(dct, "z")); + printf("x=%s\n", pydict_get(dct, "x")); + + printf("\nDump\n"); + for(cur = dct->head; cur != NULL ; cur = cur->next ) { + printf("%s=%s\n", cur->key, cur->value); + } + + pydict_del(dct); +} diff --git a/07/03-dict/output.txt b/07/03-dict/output.txt @@ -0,0 +1,12 @@ +{'z': 'Catch phrase'} +{'z': 'W'} +{'z': 'W', 'y': 'B', 'c': 'C', 'a': 'D'} +Length =4 +z=W +x=(null) + +Dump +z=W +y=B +c=C +a=D diff --git a/07/03-dict/solution.c b/07/03-dict/solution.c @@ -0,0 +1,100 @@ +/* print(dct) */ +/* {'z': 'W', 'y': 'B', 'c': 'C', 'a': 'D'} */ +void pydict_print(struct pydict* self) +{ + struct dnode *curr; + printf("{"); + for (curr = self->head; + curr != NULL; + curr = curr->next) { + printf("'%s': '%s'", + curr->key, + curr->value); + if (curr->next != NULL) { + printf(", "); + } + } + printf("}\n"); +} + +int pydict_len(const struct pydict* self) +{ + struct dnode *curr; + int count = 0; + for (curr = self->head; + curr != NULL; + curr = curr->next) { + ++count; + } + return count; +} + +/* find a node - used in get and put */ +struct dnode* pydict_find(struct pydict* self, char *key) +{ + struct dnode *current_node; + for (current_node = self->head; + current_node != NULL; + current_node = current_node->next) { + if (strcmp(current_node->key, key) == 0) { + return current_node; + } + } + return NULL; +} + +/* x.get(key) - Returns NULL if not found */ +char* pydict_get(struct pydict* self, char *key) +{ + struct dnode *found_node = pydict_find(self, key); + if (found_node == NULL) { + return NULL; + } + + char *new_string = ( + (char *) + malloc( + strlen( + found_node->value + ) + + 1 + ) + ); + + new_string = strcpy(new_string, found_node->value); + + return new_string; +} + +/* x[key] = value; Insert or replace the value associated with a key */ +void pydict_put(struct pydict* self, char *key, char *value) +{ + struct dnode *node = pydict_find(self, key); + char *new_key, *new_value; + + new_value = (char *) malloc(sizeof(char) * (strlen(value) + 1)); + new_value = strcpy(new_value, value); + + if (node != NULL) { + free(node->value); + node->value = new_value; + return; + } + + new_key = (char *) malloc(sizeof(char) * (strlen(key) + 1)); + new_key = strcpy(new_key, key); + + node = (struct dnode *) malloc(sizeof(struct dnode)); + node->key = new_key; + node->value = new_value; + node->next = NULL; + + if (self->head == NULL) { + self->head = node; + self->tail = node; + return; + } + + self->tail->next = node; + self->tail = node; +} diff --git a/08/01-encapsulation/main.c b/08/01-encapsulation/main.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct MapEntry { + char *key; /* public */ + int value; /* public */ + struct MapEntry *__prev; + struct MapEntry *__next; +}; + +struct Map { + /* Attributes */ + struct MapEntry *__head; + struct MapEntry *__tail; + int __count; + + /* Methods */ + void (*put)(struct Map* self, char *key, int value); + int (*get)(struct Map* self, char *key, int def); + int (*size)(struct Map* self); + void (*dump)(struct Map* self); + void (*del)(struct Map* self); +}; + +void __Map_del(struct Map* self) { + struct MapEntry *cur, *next; + cur = self->__head; + while(cur) { + free(cur->key); + /* value is just part of the struct */ + next = cur->__next; + free(cur); + cur = next; + } + free((void *)self); +} + +void __Map_dump(struct Map* self) +{ + struct MapEntry *cur; + printf("Object Map count=%d\n", self->__count); + for(cur = self->__head; cur != NULL ; cur = cur->__next ) { + printf(" %s=%d\n", cur->key, cur->value); + } +} + +struct MapEntry* __Map_find(struct Map* self, char *key) +{ + struct MapEntry *cur; + if ( self == NULL || key == NULL ) return NULL; + for(cur = self->__head; cur != NULL ; cur = cur->__next ) { + if(strcmp(key, cur->key) == 0 ) return cur; + } + return NULL; +} + +/* Student code will be inserted here */ + +int main(void) +{ + struct Map * map = Map_new(); + struct MapEntry *cur; + + /* Make sure we see all output up to an error */ + setvbuf(stdout, NULL, _IONBF, 0); + + printf("Map test\n"); + map->put(map, "z", 8); + map->put(map, "z", 1); + map->put(map, "y", 2); + map->put(map, "b", 3); + map->put(map, "a", 4); + map->dump(map); + + printf("size=%d\n", map->size(map)); + + printf("z=%d\n", map->get(map, "z", 42)); + printf("x=%d\n", map->get(map, "x", 42)); + + map->del(map); +} diff --git a/08/01-encapsulation/output.txt b/08/01-encapsulation/output.txt @@ -0,0 +1,9 @@ +Map test +Object Map count=4 + z=1 + y=2 + b=3 + a=4 +size=4 +z=1 +x=42 diff --git a/08/01-encapsulation/solution.c b/08/01-encapsulation/solution.c @@ -0,0 +1,72 @@ +void __Map_put(struct Map* self, char *key, int value) { + struct MapEntry *old, *new; + char *new_key; + if ( key == NULL ) return; + + old = __Map_find(self, key); + if ( old != NULL ) { + old->value = value; + return; + } + + new = malloc(sizeof(*new)); + + new_key = malloc(sizeof(char) * (strlen(key) + 1)); + new_key = strcpy(new_key, key); + new->key = new_key; + new->value = value; + new->__prev = NULL; + new->__next = NULL; + + if (self->__head == NULL) { + self->__head = new; + self->__tail = new; + } else { + self->__tail->__next = new; + new->__prev = self->__tail; + self->__tail = new; + } + + self->__count++; +} + +int __Map_size(struct Map* self) +{ + struct MapEntry *curr; + int count = 0; + + for (curr = self->__head; + curr != NULL; + curr = curr->__next) { + ++count; + } + + return count; +} + +int __Map_get(struct Map* self, char *key, int def) +{ + struct MapEntry *curr = __Map_find(self, key); + + if (curr == NULL) { + return def; + } else { + return curr->value; + } +} + +struct Map * Map_new() { + struct Map *p = malloc(sizeof(*p)); + + p->__head = NULL; + p->__tail = NULL; + p->__count = 0; + + p->put = &__Map_put; + p->get = &__Map_get; + p->size = &__Map_size; + p->dump = &__Map_dump; + p->del = &__Map_del; + + return p; +} diff --git a/08/02-iteration/main.c b/08/02-iteration/main.c @@ -0,0 +1,116 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct MapEntry { + char *key; /* public */ + int value; /* public */ + struct MapEntry *__prev; + struct MapEntry *__next; +}; + +struct MapIter { + struct MapEntry *__current; + + struct MapEntry* (*next)(struct MapIter* self); + void (*del)(struct MapIter* self); +}; + +struct Map { + /* Attributes */ + struct MapEntry *__head; + struct MapEntry *__tail; + int __count; + + /* Methods */ + void (*put)(struct Map* self, char *key, int value); + int (*get)(struct Map* self, char *key, int def); + int (*size)(struct Map* self); + void (*dump)(struct Map* self); + struct MapIter* (*iter)(struct Map* self); + void (*del)(struct Map* self); +}; + +void __Map_del(struct Map* self) { + struct MapEntry *cur, *next; + cur = self->__head; + while(cur) { + free(cur->key); + /* value is just part of the struct */ + next = cur->__next; + free(cur); + cur = next; + } + free((void *)self); +} + +void __MapIter_del(struct MapIter* self) { + free((void *)self); +} + +void __Map_dump(struct Map* self) +{ + struct MapEntry *cur; + printf("Object Map count=%d\n", self->__count); + for(cur = self->__head; cur != NULL ; cur = cur->__next ) { + printf(" %s=%d\n", cur->key, cur->value); + } +} + +struct MapEntry* __Map_find(struct Map* self, char *key) +{ + struct MapEntry *cur; + if ( self == NULL || key == NULL ) return NULL; + for(cur = self->__head; cur != NULL ; cur = cur->__next ) { + if(strcmp(key, cur->key) == 0 ) return cur; + } + return NULL; +} + +int __Map_get(struct Map* self, char *key, int def) +{ + struct MapEntry *retval = __Map_find(self, key); + if ( retval == NULL ) return def; + return retval->value; +} + +int __Map_size(struct Map* self) +{ + return self->__count; +} + +/* Student code will be inserted here */ + +int main(void) +{ + struct Map * map = Map_new(); + struct MapEntry *cur; + struct MapIter *iter; + + /* Make sure we see all output up to an error */ + setvbuf(stdout, NULL, _IONBF, 0); + + printf("Map test\n"); + map->put(map, "z", 8); + map->put(map, "z", 1); + map->put(map, "y", 2); + map->put(map, "b", 3); + map->put(map, "a", 4); + map->dump(map); + + printf("size=%d\n", map->size(map)); + + printf("z=%d\n", map->get(map, "z", 42)); + printf("x=%d\n", map->get(map, "x", 42)); + + printf("\nIterate\n"); + iter = map->iter(map); + while(1) { + cur = iter->next(iter); + if ( cur == NULL ) break; + printf("%s=%d\n", cur->key, cur->value); + } + iter->del(iter); + + map->del(map); +} diff --git a/08/02-iteration/output.txt b/08/02-iteration/output.txt @@ -0,0 +1,15 @@ +Map test +Object Map count=4 + z=1 + y=2 + b=3 + a=4 +size=4 +z=1 +x=42 + +Iterate +z=1 +y=2 +b=3 +a=4 diff --git a/08/02-iteration/solution.c b/08/02-iteration/solution.c @@ -0,0 +1,68 @@ +void __Map_put(struct Map* self, char *key, int value) { + struct MapEntry *old, *new; + char *new_key; + if ( key == NULL ) return; + + old = __Map_find(self, key); + if ( old != NULL ) { + old->value = value; + return; + } + + new = malloc(sizeof(*new)); + + new_key = malloc(sizeof(*new_key) * (strlen(key) + 1)); + new_key = strcpy(new_key, key); + + new->key = new_key; + new->value = value; + new->__prev = NULL; + new->__next = NULL; + + /* TODO: Link new to the tail of the list */ + if (self->__head == NULL) { + self->__head = new; + self->__tail = new; + } else { + self->__tail->__next = new; + new->__prev = self->__tail; + self->__tail = new; + } + + self->__count++; +} + +struct MapEntry* __MapIter_next(struct MapIter* self) +{ + struct MapEntry *curr = self->__current; + if (curr == NULL) { + return NULL; + } + self->__current = self->__current->__next; + return curr; +} + +struct MapIter* __Map_iter(struct Map* self) +{ + struct MapIter *iter = malloc(sizeof(*iter)); + /* TODO: fill in the new iterator */ + iter->__current = self->__head; + iter->next = &__MapIter_next; + iter->del = &__MapIter_del; + return iter; +} + +struct Map * Map_new() { + struct Map *p = malloc(sizeof(*p)); + + p->__head = NULL; + p->__tail = NULL; + p->__count = 0; + p->put = &__Map_put; + p->get = &__Map_get; + p->size = &__Map_size; + p->dump = &__Map_dump; + p->iter = &__Map_iter; + p->del = &__Map_del; + return p; +}