summaryrefslogtreecommitdiff
path: root/C/HF/chap4
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-09-06 09:26:21 +0200
committerCarlos Maiolino <[email protected]>2025-09-06 09:26:21 +0200
commit973e27b243ea7f12b6743894465c67a4a6a87eb2 (patch)
tree006a2d9fc8f86b4914499302325fbcaa3941e17c /C/HF/chap4
parent736967952470e740781c95cf5afb7e705ec59030 (diff)
Move some other code here
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'C/HF/chap4')
-rw-r--r--C/HF/chap4/cryptic/Makefile14
-rw-r--r--C/HF/chap4/cryptic/ecat.c23
-rw-r--r--C/HF/chap4/cryptic/encrypt.c9
-rw-r--r--C/HF/chap4/cryptic/encrypt.h6
-rw-r--r--C/HF/chap4/cryptic/message.c13
-rw-r--r--C/HF/chap4/func_maze.c12
-rw-r--r--C/HF/chap4/lab1/moisture.c45
-rw-r--r--C/HF/chap4/type.c12
8 files changed, 134 insertions, 0 deletions
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;
+}