From 563cea897121b33cc5e98dc6ed3a36eb39d52766 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sat, 13 May 2023 14:52:00 +0530 Subject: 7.5: initial commit, 4.10 with scanf instead of getchar --- 7.5/Makefile | 13 +++++++++++++ 7.5/main.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 7.5/op.c | 32 ++++++++++++++++++++++++++++++++ 7.5/op.h | 10 ++++++++++ 7.5/stk.c | 29 +++++++++++++++++++++++++++++ 7.5/stk.h | 7 +++++++ 6 files changed, 135 insertions(+) create mode 100644 7.5/Makefile create mode 100644 7.5/main.c create mode 100644 7.5/op.c create mode 100644 7.5/op.h create mode 100644 7.5/stk.c create mode 100644 7.5/stk.h 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 +#include +#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 +#include +#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 + +#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 +#include +#include +#include +#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); -- cgit v1.2.3