diff options
Diffstat (limited to 'comp/work/42/rpn')
-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 |
8 files changed, 123 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 + |