diff options
author | Daniel <steew0x8@protonmail.com> | 2021-11-07 00:35:07 +0100 |
---|---|---|
committer | Daniel <steew0x8@protonmail.com> | 2021-11-07 00:35:07 +0100 |
commit | bd5f44ae610514e159f3320b3353faec19247769 (patch) | |
tree | 0a98964ad558e893d0039d84aabedd2aa3eb1299 | |
parent | e300a7d8ee1125aaec92c84b2cb29e54dab6e2ff (diff) | |
download | rpncalc-bd5f44ae610514e159f3320b3353faec19247769.tar.gz |
made commands maybe more readable, added ^
Signed-off-by: Daniel <steew0x8@protonmail.com>
-rw-r--r-- | cmds.c | 30 | ||||
-rw-r--r-- | main.c | 4 | ||||
-rw-r--r-- | util.c | 1 |
3 files changed, 28 insertions, 7 deletions
diff --git a/cmds.c b/cmds.c index 07707df..4884ec8 100644 --- a/cmds.c +++ b/cmds.c @@ -6,29 +6,43 @@ #include <err.h> #include <string.h> +#define LAST stack.val[stack.count-1] +#define PREV stack.val[stack.count-2] +#define REDUCE stack.val[--stack.count] = 0 + stack fceil(stack stack) { - stack.val[stack.count-1] = ceil(stack.val[stack.count-1]); + LAST = ceil(PREV); return stack; } stack ffloor(stack stack) { - stack.val[stack.count-1] = floor(stack.val[stack.count-1]); + LAST = floor(LAST); return stack; } stack flogB(stack stack) { - stack.val[stack.count-2] = log2(stack.val[stack.count-2])/log2(stack.val[stack.count-1]); - stack.val[--stack.count] = 0; + PREV = log2(PREV)/log2(LAST); + REDUCE; return stack; } stack fnegate(stack stack) { - stack.val[stack.count-1] *= -1; + LAST *= -1; return stack; } stack fpop(stack stack) { - stack.val[--stack.count] = 0; + REDUCE; + return stack; +} + +stack fsqrt(stack stack) { + LAST = sqrt(LAST); + return stack; +} + +stack finv(stack stack) { + LAST = 1/LAST; return stack; } @@ -36,8 +50,10 @@ command CMD_LIST[] = { {"p", &fpop, "pop last element", 1}, {"ceil", &fceil, "truncate to the next integer", 1}, {"floor", &ffloor, "truncate to the previous integer", 1}, - {"log", &flogB, "calculate logarithm in base LAST_NUM of LAST_NUM-1", 2}, + {"log", &flogB, "calculate logarithm using last element as a base and the previous as the argument", 2}, {"neg", &fnegate, "change last element's sign", 1}, + {"sqrt", &fsqrt, "calculate the square root", 1}, + {"inv", &finv, "invert last number", 1}, }; int compare(const void *s1, const void *s2) { diff --git a/main.c b/main.c index 4f9dd2e..b4c092f 100644 --- a/main.c +++ b/main.c @@ -68,6 +68,10 @@ int main() { s.stk.val[s.stk.count-2] = s.stk.val[s.stk.count-2]/s.stk.val[s.stk.count-1]; s.stk.val[s.stk.count--] = 0; /* lower count by 1 */ s.last_op = '/'; + } else if (operator == '^') { + s.stk.val[s.stk.count-2] = pow(s.stk.val[s.stk.count-2],s.stk.val[s.stk.count-1]); + s.stk.val[s.stk.count--] = 0; /* lower count by 1 */ + s.last_op = '^'; } else; } else if (t == FUNCTION) { /* check for special commands, else pass to exec() */ diff --git a/util.c b/util.c index a6c831f..7d13e3b 100644 --- a/util.c +++ b/util.c @@ -7,6 +7,7 @@ const char OPERATOR_LIST[] = { '-', '*', '/', + '^', 0 }; |