It is possible. See an example below
RPN calculator implies in some sort of stack. On identifying an operand you pop
up two operands and does the operation.
I want to let a short example so I will shortcut some things...
our "stack"
I will assume integer operands only, but I think it is not hard to make it generic, by using the usual suspects, void*
This is the stack
in the example
// stack
int pop()
{
int val = 1 + rand()%1000 / (-1)*(rand()%2);
printf("pop(): operand %d from stack\n", val);
return val;
}
It just gets a random number in [-998,999] inclusive :)
the operations
// operations
int divd(int A, int B) { return A / B; };
int mult(int A, int B) { return A * B; };
int sub(int A, int B) { return A - B; };
int sum(int A, int B) { return A + B; };
So we have something to test.
the syntax glue
Assuming the operator is a single char we can build a lookup-table indexed by the operator and put the functions there.
C was created precisely to write this kind of things.
int (*the_fs[256])(int,int) = {0};
the_fs['+'] = sum;
the_fs['-'] = sub;
the_fs['*'] = mult;
the_fs['/'] = divd;
a test
// test for some cases
const char oper[] = "+-*/";
const int N = 8;
for (int i = 0; i < N; i += 1)
{
// do one test
int ix = rand() % 4;
printf("using '%c'\n", oper[ix]);
a = pop();
b = pop();
printf("%d %c %d = %d\n", a, oper[ix], b,
the_fs[oper[ix]](a,b));
}
It is just a matter of calling the_fs[X]
where X
is the operand. No need for if
, no need for switch()
. It can the used alo for unary operators, just PUSHing back the second operator in the function.
output for a random test with random numbers and random operations
using '+'
pop(): operand -624 from stack
pop(): operand -906 from stack
-624 + -906 = -1530
using '*'
pop(): operand -724 from stack
pop(): operand 1 from stack
-724 * 1 = -724
using '*'
pop(): operand -733 from stack
pop(): operand -807 from stack
-733 * -807 = 591531
using '-'
pop(): operand -938 from stack
pop(): operand 1 from stack
-938 - 1 = -939
using '*'
pop(): operand 0 from stack
pop(): operand -121 from stack
0 * -121 = 0
using '*'
pop(): operand 1 from stack
pop(): operand 1 from stack
1 * 1 = 1
using '-'
pop(): operand 1 from stack
pop(): operand -396 from stack
1 - -396 = 397
using '+'
pop(): operand -130 from stack
pop(): operand -372 from stack
-130 + -372 = -502
the complete test program
#include <stdio.h>
#include <stdlib.h>
int pop(); // stack simulator :)
int sum(int,int);
int sub(int,int);
int mult(int,int);
int divd(int,int);
int main(void)
{
int a = 0;
int b = 0; // the operands
srand(210907);
int (*the_fs[256])(int,int) = {0};
the_fs['+'] = sum;
the_fs['-'] = sub;
the_fs['*'] = mult;
the_fs['/'] = divd;
// so you have an operator, and it is RPN
// the stack must have 2 operands
// test for some cases
const char oper[] = "+-*/";
const int N = 8;
for (int i = 0; i < N; i += 1)
{
// do one test
int ix = rand() % 4;
printf("using '%c'\n", oper[ix]);
a = pop();
b = pop();
printf("%d %c %d = %d\n", a, oper[ix], b,
the_fs[oper[ix]](a,b));
}
return 0;
}
// stack
int pop()
{
int val = 1 + rand()%1000 / (-1)*(rand()%2);
printf("pop(): operand %d from stack\n", val);
return val;
}
// operations
int divd(int A, int B) { return A / B; };
int mult(int A, int B) { return A * B; };
int sub(int A, int B) { return A - B; };
int sum(int A, int B) { return A + B; };
/*
https: //stackoverflow.com/questions/69080130/
can-you-implement-arithmetic-operator-as-variables-in-c
*/