diff options
author | thing1 <thing1@seacrossedlovers.xyz> | 2025-01-30 12:49:01 +0000 |
---|---|---|
committer | thing1 <thing1@seacrossedlovers.xyz> | 2025-01-30 12:49:01 +0000 |
commit | 02653ab40d93fb7e6d07edb747fe0e07c5d60c74 (patch) | |
tree | 12e62d9e66737597e2d7ddc71eefa8a08264cffa /comp/work/42 | |
parent | 87c1e35223b0fd9002a6efca2a208fb8ae1993e2 (diff) |
did many new things
Diffstat (limited to 'comp/work/42')
-rw-r--r-- | comp/work/42/rpn/Makefile | 21 | ||||
-rwxr-xr-x | comp/work/42/rpn/rpn | bin | 0 -> 20864 bytes | |||
-rw-r--r-- | comp/work/42/rpn/rpn.c | 56 | ||||
-rw-r--r-- | comp/work/42/rpn/rpn.o | bin | 0 -> 8656 bytes | |||
-rw-r--r-- | comp/work/42/rpn/stack.c | 35 | ||||
-rw-r--r-- | comp/work/42/rpn/stack.h | 10 | ||||
-rw-r--r-- | comp/work/42/rpn/stack.o | bin | 0 -> 4960 bytes | |||
-rw-r--r-- | comp/work/42/rpn/test.rpn | 1 | ||||
-rw-r--r-- | comp/work/42/test/test.py | 21 | ||||
-rw-r--r-- | comp/work/42/torpn/torpn.c | 29 |
10 files changed, 173 insertions, 0 deletions
diff --git a/comp/work/42/rpn/Makefile b/comp/work/42/rpn/Makefile new file mode 100644 index 0000000..6718c5b --- /dev/null +++ b/comp/work/42/rpn/Makefile @@ -0,0 +1,21 @@ +CC=cc +CFLAGS=-ggdb -Wextra -Wall -Werror +LIBS=-lm + +SRC = rpn.c stack.c +OBJ = ${SRC:.c=.o} + +all: rpn + +.c.o: + ${CC} -c ${CFLAGS} $< +rpn: ${OBJ} + ${CC} -o $@ ${OBJ} ${LIBS} +install: all + cp rpn /usr/local/bin/rpn +clean: + rm -rf rpn *.o +uninstall: + rm /usr/local/bin/rpn + +.PHONY: all clean install uninstall diff --git a/comp/work/42/rpn/rpn b/comp/work/42/rpn/rpn Binary files differnew file mode 100755 index 0000000..424fbc5 --- /dev/null +++ b/comp/work/42/rpn/rpn diff --git a/comp/work/42/rpn/rpn.c b/comp/work/42/rpn/rpn.c new file mode 100644 index 0000000..f5cd6b7 --- /dev/null +++ b/comp/work/42/rpn/rpn.c @@ -0,0 +1,56 @@ +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include "stack.h" + +char *readword(FILE *f) { + if (getc(f) == EOF) return NULL; + fseek(f, -1, SEEK_CUR); + + char *word = malloc(10); + char c; + int i = 0; + while ((c = getc(f)) != ' ') { + if (c == EOF || c == '\n') break; + word[i] = c; + i++; + } + word[i] = 0; + return word; +} + +int main() { + FILE *f = fopen("test.rpn", "r"); + stack *s = initstack(0 ,100); + + char *word; + int a, b; + while ((word = readword(f)) != NULL) { + if (!isdigit(word[0])) { + b = pop(s); + a = pop(s); + switch (word[0]) { + case '+': push(s, a+b); break; + case '-': push(s, a-b); break; + case '*': push(s, a*b); break; + case '/': push(s, a/b); break; + case '^': push(s, pow(a, b)); break; + case '~': + push(s, a); + push(s, -b); + break; + default: + printf("unknown symbol %c\n",word[0]); + exit(1); + break; + } + } + else push(s, atoi(word)); + free(word); + } + printf("%d\n", pop(s)); + + deinitstack(s); +} diff --git a/comp/work/42/rpn/rpn.o b/comp/work/42/rpn/rpn.o Binary files differnew file mode 100644 index 0000000..169c928 --- /dev/null +++ b/comp/work/42/rpn/rpn.o diff --git a/comp/work/42/rpn/stack.c b/comp/work/42/rpn/stack.c new file mode 100644 index 0000000..0311404 --- /dev/null +++ b/comp/work/42/rpn/stack.c @@ -0,0 +1,35 @@ +#include <stddef.h> +#include <stdlib.h> + +typedef struct stack { + int *sp; + int value; + int *stack; +} stack; + +int pop(stack *stack){ + stack->sp -= sizeof(int); + int i = *stack->sp; + *stack->sp = stack->value; + return i; +} + +void push(stack *stack, int value){ + *stack->sp = value; + stack->sp += sizeof(int); +} + +void deinitstack(stack *stack){ + free(stack->stack); + free(stack); + stack = NULL; +} + +stack *initstack(int value, size_t size){ + stack *s = malloc(sizeof(stack)); + s->stack = calloc(0, size*sizeof(int)); + s->sp = s->stack; + s->value = value; + return s; +} + diff --git a/comp/work/42/rpn/stack.h b/comp/work/42/rpn/stack.h new file mode 100644 index 0000000..0c78a10 --- /dev/null +++ b/comp/work/42/rpn/stack.h @@ -0,0 +1,10 @@ +typedef struct stack { + int *sp; + int value; + int *stack; +} stack; + +int pop(stack *stack); +void push(stack *stack, int value); +void deinitstack(stack *stack); +stack *initstack(int value, int size); diff --git a/comp/work/42/rpn/stack.o b/comp/work/42/rpn/stack.o Binary files differnew file mode 100644 index 0000000..05aa2e6 --- /dev/null +++ b/comp/work/42/rpn/stack.o diff --git a/comp/work/42/rpn/test.rpn b/comp/work/42/rpn/test.rpn new file mode 100644 index 0000000..02f9f8a --- /dev/null +++ b/comp/work/42/rpn/test.rpn @@ -0,0 +1 @@ +2 ~ 4 + diff --git a/comp/work/42/test/test.py b/comp/work/42/test/test.py new file mode 100644 index 0000000..4e70d92 --- /dev/null +++ b/comp/work/42/test/test.py @@ -0,0 +1,21 @@ +def tostring(list): + string = "" + for i in list: + string += str(i) + print(string) + +def getbin(n): + out = [] + while (n >= 1): + out.append(n % 2) + n = int(n / 2) + + out.reverse() + return out + +def tobin(n): + raw = getbin(n) + tostring(raw) + +n = int(input("what number would you like to convert: ")) +tobin(n) diff --git a/comp/work/42/torpn/torpn.c b/comp/work/42/torpn/torpn.c new file mode 100644 index 0000000..58b939a --- /dev/null +++ b/comp/work/42/torpn/torpn.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +typedef enum op { + ADD = '+', + SUB = '-', + MUL = '*', + DIV = '/', +} op; + +typedef struct ast { + op o; + int val1; + struct ast *child1; + + int val2; + struct ast *child2; +} ast; + +int main(){ + FILE *f = fopen("test.in", "r"); + char *expr = malloc(256); + fgets(expr, 256, f); + + for (int i = 0; i < strlen(expr); i++){ + + } +} |