diff options
author | sinanmohd <sinan@firemail.cc> | 2023-05-13 14:52:00 +0530 |
---|---|---|
committer | sinanmohd <sinan@firemail.cc> | 2023-05-13 22:06:17 +0530 |
commit | 563cea897121b33cc5e98dc6ed3a36eb39d52766 (patch) | |
tree | 8da51d24daf9796c3d5351f13a068fc03fe997aa /7.5 | |
parent | e0b9da43b6311aa4413d030b27c6cdab000aeafd (diff) |
7.5: initial commit, 4.10 with scanf instead of getchar
Diffstat (limited to '7.5')
-rw-r--r-- | 7.5/Makefile | 13 | ||||
-rw-r--r-- | 7.5/main.c | 44 | ||||
-rw-r--r-- | 7.5/op.c | 32 | ||||
-rw-r--r-- | 7.5/op.h | 10 | ||||
-rw-r--r-- | 7.5/stk.c | 29 | ||||
-rw-r--r-- | 7.5/stk.h | 7 |
6 files changed, 135 insertions, 0 deletions
diff --git a/7.5/Makefile b/7.5/Makefile new file mode 100644 index 0000000..43c5a6f --- /dev/null +++ b/7.5/Makefile @@ -0,0 +1,13 @@ +OBJECTS = main.o stk.o op.o +CC = clang +CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address + +calc : $(OBJECTS) + $(CC) $(CFLAGS) -o calc $(OBJECTS) + +main.o : stk.h op.h + +.PHONY : clean + +clean : + rm -f calc $(OBJECTS) diff --git a/7.5/main.c b/7.5/main.c new file mode 100644 index 0000000..cf5194e --- /dev/null +++ b/7.5/main.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include "stk.h" +#include "op.h" + +#define MAXOP 100 + +int main(void) +{ + char op[MAXOP]; + int opd2; + int c; + + while ((c = getop(op, MAXOP))) { + switch (c) { + case NUM: + push(atof(op)); + break; + case '+': + push(pop() + pop()); + break; + case '-': + opd2 = pop(); + push(pop() + opd2); + break; + case '*': + push(pop() * pop()); + break; + case '/': + opd2 = pop(); + push(pop() / opd2); + break; + case '\n': + printf("\tans: %.2lf\n", pop()); + clear(); + break; + default: + printf("err: unkown operator: %c\n", c); + break; + } + } + + return 0; +} diff --git a/7.5/op.c b/7.5/op.c new file mode 100644 index 0000000..e7640e6 --- /dev/null +++ b/7.5/op.c @@ -0,0 +1,32 @@ +#include <stdio.h> +#include <ctype.h> +#include "op.h" + +char getop(char *op, size_t len) +{ + size_t i; + + *op = '\0'; // if scanf fails + do { + scanf("%c", op); + } while (isblank(*op)); + + if (!isdigit(*op)) + return *op; + + i = 1; + do { + scanf("%c", op + i); + } while (isdigit(op[i]) && ++i < len); + + if (op[i] == '.') { + i++; + do { + scanf("%c", op + i); + } while (isdigit(op[i]) && ++i < len); + } + + ungetc(op[i], stdin); + op[i] = '\0'; + return NUM; +} diff --git a/7.5/op.h b/7.5/op.h new file mode 100644 index 0000000..bfe24b5 --- /dev/null +++ b/7.5/op.h @@ -0,0 +1,10 @@ +#include <stddef.h> + +#ifndef OP_H + +#define NUM '0' + +#define OP_H +#endif + +char getop(char *op, size_t len); diff --git a/7.5/stk.c b/7.5/stk.c new file mode 100644 index 0000000..ea10221 --- /dev/null +++ b/7.5/stk.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <stdlib.h> +#include <error.h> +#include <errno.h> +#include "stk.h" + +static unsigned top; +double stk[MAXBUFF]; + +void push(double n) +{ + if (top < MAXBUFF) + stk[top++] = n; + else + error(EXIT_FAILURE, ENOBUFS, "push"); +} + +double pop(void) +{ + if (top <= 0) + error(EXIT_FAILURE, ENOBUFS, "pop"); + + return stk[--top]; +} + +void clear(void) +{ + top = 0; +} diff --git a/7.5/stk.h b/7.5/stk.h new file mode 100644 index 0000000..cd4cf2c --- /dev/null +++ b/7.5/stk.h @@ -0,0 +1,7 @@ +#define MAXBUFF 100 + +extern double stk[MAXBUFF]; + +void clear(void); +void push(double n); +double pop(void); |