summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2022-11-21 20:39:02 +0000
committerMartin Ashby <martin@ashbysoft.com>2022-11-21 20:39:02 +0000
commit50534766e09667a9c23c1f167dca92f5d4fd0f27 (patch)
treee9b0bcfb282d67ec9f89433467cd988dde9976f4
parent5a72e2df2d68116241cf053b78a0f78135b02e7b (diff)
downloadlearn-c-50534766e09667a9c23c1f167dca92f5d4fd0f27.tar.gz
learn-c-50534766e09667a9c23c1f167dca92f5d4fd0f27.tar.bz2
learn-c-50534766e09667a9c23c1f167dca92f5d4fd0f27.tar.xz
learn-c-50534766e09667a9c23c1f167dca92f5d4fd0f27.zip
Actually implement ex4-3
-rw-r--r--ex4-3.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/ex4-3.c b/ex4-3.c
index 47689f4..7de79fb 100644
--- a/ex4-3.c
+++ b/ex4-3.c
@@ -10,6 +10,11 @@ int getop(char[]);
void push(double);
double pop(void);
+// Naïve implementation of remainder operation
+double frem(double f1, double f2) {
+ return f1 - (f2 * (int)(f1 / f2));
+}
+
/**
* Reverse polish notation calculator
*/
@@ -39,6 +44,13 @@ int main(void) {
else
printf("error: zero division attempted!\n");
break;
+ case '%':
+ op2 = pop();
+ if (op2 != 0.0)
+ push(frem(pop(), op2));
+ else
+ printf("error: zero division attempted!\n");
+ break;
case '\n':
printf("\t%.8g\n", pop());
break;