From d465d6e672eba42da6e2dd729e02e6b78b0bf22a Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 3 Nov 2021 23:31:16 +0100 Subject: initial commit --- main.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ meson.build | 3 +++ state.c | 6 ++++++ util.c | 32 ++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 main.c create mode 100644 meson.build create mode 100644 state.c create mode 100644 util.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..f72d7aa --- /dev/null +++ b/main.c @@ -0,0 +1,65 @@ +#include +#include "state.h" +#include "util.h" +#include +#include + +#define BUF_SIZE 50 + +/* rpncalc: a simple reverse polish notation calculator */ +int main() { + + /* initialize state struct and its values */ + state s; + /* default input buffer is stdin */ + s.defbuf = stdin; + /* default output buffer is stdout */ + s.defout = stdout; + s.last_op = 0; + /* set default interactive prompt */ + s.prompt = "[%d]> "; + stack stack = {{0}, 0}; + /* initialize value stack */ + s.stack = stack; + s.command_count = 0; + + /* start reading from defbuf */ + char buf[BUF_SIZE]; + while(1){ + fprintf(s.defout, s.prompt, s.command_count, s.last_op); + s.last_op = 0; + if (s.stack.count == STACK_SIZE) err(1, "exceeded stack size"); + fgets(buf, BUF_SIZE, s.defbuf); + TYPE t = discriminate(buf); + if (t == OPERATOR ) { + if (s.stack.count == 1) { + fprintf(s.defout, "not enough arguments\n"); + continue; + } + char operator = buf[0]; + if (operator == '+') { + s.stack.val[s.stack.count-2] = s.stack.val[s.stack.count-1]+s.stack.val[s.stack.count-2]; + s.stack.val[s.stack.count--] = 0; /* lower count by 1 */ + s.last_op = '+'; + } else if (operator == '-') { + s.stack.val[s.stack.count-2] = s.stack.val[s.stack.count-2]-s.stack.val[s.stack.count-1]; + s.stack.val[s.stack.count--] = 0; /* lower count by 1 */ + s.last_op = '-'; + } else if (operator == '*') { + s.stack.val[s.stack.count-2] = s.stack.val[s.stack.count-2]*s.stack.val[s.stack.count-1]; + s.stack.val[s.stack.count--] = 0; /* lower count by 1 */ + s.last_op = '*'; + } else if (operator == '/') { + s.stack.val[s.stack.count-2] = s.stack.val[s.stack.count-2]/s.stack.val[s.stack.count-1]; + s.stack.val[s.stack.count--] = 0; /* lower count by 1 */ + s.last_op = '/'; + } + } else { + s.stack.val[s.stack.count++]=strtod(buf, NULL); + } + s.command_count++; + for (int i = 0; i < s.stack.count; i++) { + fprintf(s.defout,"%d → %f\n",i, s.stack.val[i]); + } + } +} diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..e6012bf --- /dev/null +++ b/meson.build @@ -0,0 +1,3 @@ +project('rpncalc', 'c') +src = ['main.c', 'state.c', 'util.c'] +executable('rpncalc', src) diff --git a/state.c b/state.c new file mode 100644 index 0000000..2c504e8 --- /dev/null +++ b/state.c @@ -0,0 +1,6 @@ +#include "state.h" + +void push(state *s, double d) { + s->stack.val[s->stack.count+1] = d; + s->stack.count += 1; +} diff --git a/util.c b/util.c new file mode 100644 index 0000000..555c445 --- /dev/null +++ b/util.c @@ -0,0 +1,32 @@ +#include "util.h" +#include + +char OPERATOR_LIST[] = { + '+', + '-', + '*', + '/', + 0 +}; + +int is_decimal(char *s) { + for (int i = 0; s[i] != 0; i++) { + if (s[i] == ',' || s[i] == '.') return 1; + } + return 0; +} + +int is_operator(char *s) { + for (int i = 0; OPERATOR_LIST[i] != 0; i++) { + if (s[0] == OPERATOR_LIST[i]) return 1; + } + return 0; +} + +TYPE discriminate(char *s) { + if (is_decimal(s)) { + return DOUBLE; + } else if (is_operator(s)) { + return OPERATOR; + } else return INT; +} -- cgit 1.4.1-2-gfad0