aboutsummaryrefslogtreecommitdiff
path: root/5.10.c
diff options
context:
space:
mode:
authorsinanmohd <pcmsinan@gmail.com>2022-06-20 22:41:06 +0530
committersinanmohd <pcmsinan@gmail.com>2022-06-20 22:41:06 +0530
commit119ebb67517669e87a41d0af4da1424bb3fec43e (patch)
tree9c22d9b3009ac24af8e8eb3d446359af7a14ebd8 /5.10.c
parente2d7e348da862126f28194a09812002a41c013fc (diff)
5.10
Diffstat (limited to '5.10.c')
-rw-r--r--5.10.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/5.10.c b/5.10.c
new file mode 100644
index 0000000..26a9f84
--- /dev/null
+++ b/5.10.c
@@ -0,0 +1,92 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <math.h>
+
+#define MAXOP 1000
+#define NUMBER_SIG '0'
+
+double ops[MAXOP];
+int opsl = 0; /* ops location */
+
+char getop(char str[]);
+double pop(void);
+void push(double num);
+
+int
+main(int argc, char *argv[])
+{
+ int opnum;
+ double op2;
+ char input;
+
+ for (opnum = 1; --argc; opnum++)
+ switch ((input = getop(argv[opnum]))) {
+ case NUMBER_SIG :
+ push(atof(argv[opnum]));
+ break;
+ case '*' :
+ push(pop() * pop());
+ break;
+ case '+' :
+ push(pop() + pop());
+ break;
+ case '-' :
+ op2 = pop();
+ push(pop() - op2);
+ break;
+ case '/' :
+ if ((op2 = pop()) == 0.0)
+ printf("Err: deviser cant be zero\n");
+ else
+ push(pop() / op2);
+ break;
+ case '%' :
+ op2 = pop();
+ if (op2 != 0)
+ push(fmod(pop(), op2));
+ else
+ printf("Err: deviser cant be zero\n");
+ break;
+ default :
+ printf("Err: unknown command %d-'%c'\n", input, input);
+ break;
+ }
+
+ printf("Result: %.8g\n", pop());
+
+ return 0;
+}
+
+char
+getop(char str[])
+{
+ /* skip blanks */
+ while (isblank(*str))
+ str++;
+
+ /* return operators */
+ if (!isdigit(*str) && *str != '.' && !isdigit(*(str+1)))
+ return *str;
+
+ /* collect numbers */
+ else if (isdigit(*str) || *str == '.')
+ return NUMBER_SIG;
+
+ return -1;
+}
+
+double
+pop(void)
+{
+ return (opsl > 0) ? ops[--opsl] : 0 ;
+}
+
+void
+push(double num)
+{
+ if (opsl <= MAXOP)
+ ops[opsl++] = num;
+ else
+ printf("Err: stack full\n");
+}