diff options
| author | Carlos Maiolino <[email protected]> | 2025-09-06 09:26:21 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-09-06 09:26:21 +0200 |
| commit | 973e27b243ea7f12b6743894465c67a4a6a87eb2 (patch) | |
| tree | 006a2d9fc8f86b4914499302325fbcaa3941e17c | |
| parent | 736967952470e740781c95cf5afb7e705ec59030 (diff) | |
Move some other code here
Signed-off-by: Carlos Maiolino <[email protected]>
34 files changed, 495 insertions, 0 deletions
diff --git a/C/HF/array.c b/C/HF/array.c new file mode 100644 index 0000000..8410be0 --- /dev/null +++ b/C/HF/array.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +int main(void) +{ + char s[] = "Howdy horny!\n"; + char *t = s; + + puts((t + 6)); + return 0; +} diff --git a/C/HF/bermuda.c b/C/HF/bermuda.c new file mode 100644 index 0000000..d1657c5 --- /dev/null +++ b/C/HF/bermuda.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +int +main(void) +{ + float latitude; + float longitude; + char info[80]; + int started = 0; + + while (scanf("%f,%f,%79[^\n]",&longitude, &latitude, info) == 3) { + + if ((longitude > -76 && longitude < -64) && + (latitude > 26 && latitude < 34)) + printf("%f,%f,%s\n",longitude, latitude, info); + } + + return 0; +} + + diff --git a/C/HF/categorize.c b/C/HF/categorize.c new file mode 100644 index 0000000..a455224 --- /dev/null +++ b/C/HF/categorize.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void +usage(char *program) +{ + printf("Usage:\n"); + printf("\t%s: <filter1> <output_file_1> <filter2> <output_file_2> <everything>\n", + program); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + char line[80]; + FILE *in; + FILE *file1; + FILE *file2; + FILE *file3; + + if (argc != 6) + usage(argv[0]); + + in = fopen("spooky.csv", "r"); + file1 = fopen(argv[2], "w"); + file2 = fopen(argv[4], "w"); + file3 = fopen(argv[5], "w"); + + while (fscanf(in, "%79[^\n]\n", line) == 1) { + if (strstr(line, argv[1])) + fprintf(file1, "%s\n", line); + else if (strstr(line, argv[3])) + fprintf(file2, "%s\n", line); + else + fprintf(file3, "%s\n", line); + } + + fclose(in); + fclose(file1); + fclose(file2); + fclose(file3); + return 0; +} diff --git a/C/HF/chap4/cryptic/Makefile b/C/HF/chap4/cryptic/Makefile new file mode 100644 index 0000000..64312d5 --- /dev/null +++ b/C/HF/chap4/cryptic/Makefile @@ -0,0 +1,14 @@ +ecat: ecat.o encrypt.o + cc -o ecat ecat.o encrypt.o + +ecat.o: encrypt.h ecat.c + cc -c ecat.c + +message: message.o encrypt.o + cc -o message message.o encrypt.o + +message.o: encrypt.h message.c + cc -c message.c + +encrypt.o: encrypt.h encrypt.c + cc -c encrypt.c diff --git a/C/HF/chap4/cryptic/ecat.c b/C/HF/chap4/cryptic/ecat.c new file mode 100644 index 0000000..5266116 --- /dev/null +++ b/C/HF/chap4/cryptic/ecat.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include "encrypt.h" + +int main(int argc, char **argv) +{ + FILE *fd; + char buf[80]; + + if (argc != 2) + return 1; + + fd = fopen(argv[1], "r"); + + while (fgets(buf, 80, fd)) { + encrypt(buf); + printf("%s", buf); + } + + printf("\n"); + fclose(fd); + + return 0; +} diff --git a/C/HF/chap4/cryptic/encrypt.c b/C/HF/chap4/cryptic/encrypt.c new file mode 100644 index 0000000..128b1a4 --- /dev/null +++ b/C/HF/chap4/cryptic/encrypt.c @@ -0,0 +1,9 @@ +#include "encrypt.h" + +void encrypt(char *message) +{ + while (*message) { + *message = *message ^ 42; + message++; + } +} diff --git a/C/HF/chap4/cryptic/encrypt.h b/C/HF/chap4/cryptic/encrypt.h new file mode 100644 index 0000000..7d5cf9d --- /dev/null +++ b/C/HF/chap4/cryptic/encrypt.h @@ -0,0 +1,6 @@ +#ifndef ENCRYPT_H +#define ENCRYPT_H + +void encrypt(char *m); + +#endif diff --git a/C/HF/chap4/cryptic/message.c b/C/HF/chap4/cryptic/message.c new file mode 100644 index 0000000..345774c --- /dev/null +++ b/C/HF/chap4/cryptic/message.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include "encrypt.h" + +int main(void) +{ + char msg[80]; + while (fgets(msg, 80, stdin)) { + encrypt(msg); + printf("%s\n", msg); + } + + return 0; +} diff --git a/C/HF/chap4/func_maze.c b/C/HF/chap4/func_maze.c new file mode 100644 index 0000000..a65a3d1 --- /dev/null +++ b/C/HF/chap4/func_maze.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +float amuze_me(void) { + return 3.9; +} + +int main(void) +{ + int foo = amuze_me(); + printf("%i\n", foo); + return 0; +} diff --git a/C/HF/chap4/lab1/moisture.c b/C/HF/chap4/lab1/moisture.c new file mode 100644 index 0000000..349050f --- /dev/null +++ b/C/HF/chap4/lab1/moisture.c @@ -0,0 +1,45 @@ +/* This works - tested on Tinkercad */ +#define THRESHOLD 800 +#define DRY 500 + +#define LED 13 + +setup(){ + Serial.begin(9600); + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); +} + +void blink_led(long interval) { + digitalWrite(LED, LOW); + delay(interval); + digitalWrite(LED, HIGH); +} + +void led_on(void) +{ + if (digitalRead(LED) == LOW) + digitalWrite(LED, HIGH); +} + +void led_off(void) +{ + if (digitalRead(LED) == HIGH) { + digitalWrite(LED, LOW); + Serial.println("Thank you, Seymour"); + } +} + +void loop(){ + int moist = analogRead(1); + if (moist > THRESHOLD) { + led_off(); + continue; + } + + if (moist < DRY) + blink_led(500); + else + led_on(); + Serial.println("Feed me!"); +} diff --git a/C/HF/chap4/type.c b/C/HF/chap4/type.c new file mode 100644 index 0000000..f63e134 --- /dev/null +++ b/C/HF/chap4/type.c @@ -0,0 +1,12 @@ +#include <stdio.h> +#include <limits.h> +#include <float.h> + +int main() { + printf("INT_MAX: %i\n", INT_MAX); + printf("INT_MIN: %i\n", INT_MIN); + printf("FLT_MAX: %f\n", DBL_MAX); + printf("FLT_MIN: %.600f\n", DBL_MIN); + printf("Int: %i\n", sizeof(size_t)); + return 0; +} diff --git a/C/HF/chap5/data.c b/C/HF/chap5/data.c new file mode 100644 index 0000000..4034480 --- /dev/null +++ b/C/HF/chap5/data.c @@ -0,0 +1,46 @@ +#include <stdio.h> + +enum q { + COUNT, + WEIGHT, + VOLUME, +}; + +/* Using anonymous data structures is awesome */ +struct item { + char *desc; + union { + long int count; + float weight; + float volume; + }; + enum q type; +}__attribute((packed))__; + +int main(void) { + + struct item fruit = + { + .desc = "apple", + }; + + fruit.type = WEIGHT; + + if (fruit.type == COUNT) { + fruit.weight = 3.87; + printf("Fruit = %s is %ld\n", fruit.desc, fruit.count); + } + if (fruit.type == WEIGHT) { + fruit.weight = 69.5; + printf("Fruit = %s is %2.2f\n", fruit.desc, fruit.weight); + } + if (fruit.type == VOLUME) { + fruit.volume = 63.5; + printf("Fruit = %s is %2.2f\n", fruit.desc, fruit.volume); + } + + printf("struct item: %lu - count: %lu - weight: %lu - volume: %lu - type: %lu\n", + sizeof(struct item), sizeof(fruit.count), sizeof(fruit.weight), + sizeof(fruit.volume), sizeof(fruit.type)); + return 0; +} diff --git a/C/HF/chap7/Makefile b/C/HF/chap7/Makefile new file mode 100644 index 0000000..64312d5 --- /dev/null +++ b/C/HF/chap7/Makefile @@ -0,0 +1,14 @@ +ecat: ecat.o encrypt.o + cc -o ecat ecat.o encrypt.o + +ecat.o: encrypt.h ecat.c + cc -c ecat.c + +message: message.o encrypt.o + cc -o message message.o encrypt.o + +message.o: encrypt.h message.c + cc -c message.c + +encrypt.o: encrypt.h encrypt.c + cc -c encrypt.c diff --git a/C/HF/chap7/ecat.c b/C/HF/chap7/ecat.c new file mode 100644 index 0000000..ec034e4 --- /dev/null +++ b/C/HF/chap7/ecat.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include "xor.h" +#include "encrypt.h" + +int main(int argc, char **argv) +{ + FILE *fd; + char buf[80]; + + if (argc != 2) + return 1; + + fd = fopen(argv[1], "r"); + + while (fgets(buf, 80, fd)) { + encrypt(buf, xor64); + printf("%s", buf); + } + + printf("\n"); + fclose(fd); + return 0; +} diff --git a/C/HF/chap7/ecat.core b/C/HF/chap7/ecat.core Binary files differnew file mode 100644 index 0000000..2b80a6f --- /dev/null +++ b/C/HF/chap7/ecat.core diff --git a/C/HF/chap7/encrypt.c b/C/HF/chap7/encrypt.c new file mode 100644 index 0000000..1b5ce25 --- /dev/null +++ b/C/HF/chap7/encrypt.c @@ -0,0 +1,6 @@ +#include "encrypt.h" +typedef void (*encrypt_fn_t)(char *message); + +void encrypt(char *message, encrypt_fn_t encrypt_fn) { + encrypt_fn(message); +} diff --git a/C/HF/chap7/encrypt.h b/C/HF/chap7/encrypt.h new file mode 100644 index 0000000..4e130e9 --- /dev/null +++ b/C/HF/chap7/encrypt.h @@ -0,0 +1,7 @@ +#ifndef ENCRYPT_H +#define ENCRYPT_H + +typedef void (*encrypt_fn_t)(char *message); +void encrypt(char *message, encrypt_fn_t); + +#endif /* ENCRYPT_H */ diff --git a/C/HF/chap7/ls.txt b/C/HF/chap7/ls.txt new file mode 100644 index 0000000..928beb3 --- /dev/null +++ b/C/HF/chap7/ls.txt @@ -0,0 +1 @@ +J diff --git a/C/HF/chap7/message.c b/C/HF/chap7/message.c new file mode 100644 index 0000000..345774c --- /dev/null +++ b/C/HF/chap7/message.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include "encrypt.h" + +int main(void) +{ + char msg[80]; + while (fgets(msg, 80, stdin)) { + encrypt(msg); + printf("%s\n", msg); + } + + return 0; +} diff --git a/C/HF/chap7/text.txt b/C/HF/chap7/text.txt new file mode 100644 index 0000000..c87bea7 --- /dev/null +++ b/C/HF/chap7/text.txt @@ -0,0 +1 @@ +RONALDO diff --git a/C/HF/chap7/xor.c b/C/HF/chap7/xor.c new file mode 100644 index 0000000..0efa3a4 --- /dev/null +++ b/C/HF/chap7/xor.c @@ -0,0 +1,20 @@ +#include <stdio.h> +#include "xor.h" + +void xor42(char *message) +{ + printf("Orig: %s\n", message); + while (*message) { + *message = *message ^ 42; + message++; + } +} + +void xor64(char *message) +{ + printf("Orig: %s\n", message); + while (*message) { + *message = *message ^ 64; + message++; + } +} diff --git a/C/HF/chap7/xor.h b/C/HF/chap7/xor.h new file mode 100644 index 0000000..6f3de42 --- /dev/null +++ b/C/HF/chap7/xor.h @@ -0,0 +1,7 @@ +#ifndef XOR_H +#define XOR_H + +void xor42(char *m); +void xor64(char *m); + +#endif diff --git a/C/HF/disappearance.csv b/C/HF/disappearance.csv new file mode 100644 index 0000000..2edfd07 --- /dev/null +++ b/C/HF/disappearance.csv @@ -0,0 +1 @@ +scully Disappearance diff --git a/C/HF/geo2json.c b/C/HF/geo2json.c new file mode 100644 index 0000000..f1d3f8a --- /dev/null +++ b/C/HF/geo2json.c @@ -0,0 +1,31 @@ +#include <stdio.h> + +int +main(void) +{ + float latitude; + float longitude; + char info[80]; + int started = 0; + + puts("data=["); + while (scanf("%f,%f,%79[^\n]",&longitude, &latitude, info) == 3) { + if (started) + printf(",\n"); + else + started = 1; + + if (longitude < -180 || longitude > 180 || + latitude < -90 || latitude > 90) { + fprintf(stderr, "Latitude must be between -90,90 and longitude -180,180\n"); + return 2; + } + + printf("{latitude: %f, longitude: %f, info: '%s'}", + latitude, longitude, info); + } + puts("\n]"); + return 0; +} + + diff --git a/C/HF/in.txt b/C/HF/in.txt new file mode 100644 index 0000000..0a5e5b9 --- /dev/null +++ b/C/HF/in.txt @@ -0,0 +1,6 @@ +45.99,-23.11,speed = 3 +69.32,93.91,speed = 95 +-68.345,28.243,speed = 69 +196.19,59.90,speed = 33 +77.29,-12.34,speed = 1 +89.69,56.78,speed = 9 diff --git a/C/HF/order_pizza.c b/C/HF/order_pizza.c new file mode 100644 index 0000000..aec19ac --- /dev/null +++ b/C/HF/order_pizza.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <unistd.h> + +int +main(int argc, char **argv) +{ + char *delivery = ""; + int thick = 0; + int count = 0; + char ch; + + while ((ch = getopt(argc, argv, "d:t")) != EOF) + switch (ch) { + case 'd': + delivery = optarg; + break; + case 't': + thick = 1; + break; + default: + fprintf(stderr, "Unknown option: '%s'\n", optarg); + return 1; + } + + argc -= optind; + argv += optind; + + if (thick) + puts("Thick crust."); + + if (delivery[0]) + printf("To be delivered %s\n", delivery); + + puts("Ingredients:"); + + for (count = 0; count < argc; count++) + puts(argv[count]); + + return 0; +} diff --git a/C/HF/others.csv b/C/HF/others.csv new file mode 100644 index 0000000..7f5a4f5 --- /dev/null +++ b/C/HF/others.csv @@ -0,0 +1,3 @@ +ronaldo curintia +mulder UFO +mandinga da braba diff --git a/C/HF/out.json b/C/HF/out.json new file mode 100644 index 0000000..f0b6cde --- /dev/null +++ b/C/HF/out.json @@ -0,0 +1,2 @@ +data=[ +{latitude: -23.110001, longitude: 45.990002, info: 'speed = 3'}, diff --git a/C/HF/output.json b/C/HF/output.json new file mode 100644 index 0000000..94fb2ff --- /dev/null +++ b/C/HF/output.json @@ -0,0 +1,7 @@ +data=[ +{latitude: -23.110001, longitude: 45.990002, info: 'speed = 3'}, +{latitude: 33.910000, longitude: 69.320000, info: 'speed = 95'}, +{latitude: 59.900002, longitude: 66.190002, info: 'speed = 33'}, +{latitude: -12.340000, longitude: 77.290001, info: 'speed = 1'}, +{latitude: 56.779999, longitude: 89.690002, info: 'speed = 9'} +] diff --git a/C/HF/rocks.c b/C/HF/rocks.c new file mode 100644 index 0000000..b244659 --- /dev/null +++ b/C/HF/rocks.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +int main() { + puts("Corno"); + return 0; +} diff --git a/C/HF/shuffle.c b/C/HF/shuffle.c new file mode 100644 index 0000000..eb11bf9 --- /dev/null +++ b/C/HF/shuffle.c @@ -0,0 +1,14 @@ +#include <stdio.h> + +int main() +{ + char cards[] = "JQK"; + char a_card = cards[2]; + cards[2] = cards[1]; + cards[1] = cards[0]; + cards[0] = cards[2]; + cards[2] = cards[1]; + cards[1] = a_card; + puts(cards); + return 0; +} diff --git a/C/HF/spooky.csv b/C/HF/spooky.csv new file mode 100644 index 0000000..4846f85 --- /dev/null +++ b/C/HF/spooky.csv @@ -0,0 +1,4 @@ +ronaldo curintia +mulder UFO +scully Disappearance +mandinga da braba diff --git a/C/HF/str.c b/C/HF/str.c new file mode 100644 index 0000000..f3e635a --- /dev/null +++ b/C/HF/str.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <string.h> + +char tracks[][80] = { + "I left my heart in MIT", + "Newark, Newark", + "Dancing with a Doll", + "From here to eternity", + "The girl from Iwo Jima", +}; + +void find_track(char s[]) +{ + int i = 5; + + for (i = 0; i < 5; i++) { + if (strstr(tracks[i], s)) { + printf("Found track: %i - %s\n", i, tracks[i]); + return; + } + } + printf("Track not found: %s\n", s); +} + +int main(void) +{ + char i_str[80]; + + printf("Type track: "); + gets_s(i_str, 80); + find_track(i_str); + return 0; +} diff --git a/C/HF/ufos.csv b/C/HF/ufos.csv new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/C/HF/ufos.csv |
