RPN Analyzer

Reverse Polish Notation is a mathematical notation that, unlike traditional math notation, does not require parenthesis. In it, the 2 operands precede each operator. It is also considered to be computer-friendly in a way that it is easy for applications to read and parse the equation. It works like this; each number read gets pushed to a stack. Each math operator pops 2 values off the stack, executes the math operation between those 2 operands, and pushes the result back to the stack.

So 3 + 3 would become 3, 3, +.
Take this example: 5 + ((1 + 2) * 4) − 3
In RPN that’s: 5, 1, 2, +, 4, *, +, 3, −

5 -- Push 5                                 (Stack: 5)
1 -- Push 1                                 (Stack: 5, 1)
2 -- Push 2                                 (Stack: 5, 1, 2)
+ -- Pop 1 & 2, add them, push result       (Stack: 5, 3)
4 -- Push 4                                 (Stack: 5, 3, 4)
* -- Pop 4 & 3, multiply them, push result  (Stack: 5, 12)
+ -- Pop 12 & 5, add them, push result      (Stack: 17)
3 -- Push 3                                 (Stack: 17, 3)
- -- Pop 3 & 17, subtract them, push result (Stack: 14)
Result = 14

I made an RPN Analyzer in cpp. The argument for the equation is a comma delmited (between each operand and operator) and semicolon terminated equation.
Example argument: 3,5,+;
Source:

#include <cstdio>
#include <string>
#include <stack>
#include <cstdlib>
using namespace std;
void help();

int main(int argc,char *argv[]) {
   if (argc<2) { help(); return 1; }
   char *expr=argv[1];  
   int ep=0;            
   char c;              
   stack<int> s;       
   char op[5];
   int opi=0;
   int t=0;
   while (c!=';') {
      c=*(expr+ep);
      if (!(c==','||c=='+'||c=='-'||c=='*'||c=='/')) {
         if (t!=0) {
            t=0;
            strcpy(op,"\0\0\0\0\0");
            opi=0;
         }
         op[opi]=c;
         opi++;
         op[opi]=0;
      } else if (c==',') {
         if (t==0) {
            int num=atoi(op);
            s.push(num);
         }
         t=1;
      } else {
         t=2;
         int a;
         int b;
         int ci;
         if (c=='+') {
            a=s.top();
            s.pop();
            b=s.top();
            s.pop();
            ci=b+a;
            s.push(ci);
         } else if (c=='-') {
            a=s.top();
            s.pop();
            b=s.top();
            s.pop();
            ci=b-a;
            s.push(ci);
         } else if (c=='*') {
            a=s.top();
            s.pop();
            b=s.top();
            s.pop();
            ci=b*a;
            s.push(ci);
         } else if (c=='/') {
            a=s.top();
            s.pop();
            b=s.top();
            s.pop();
            ci=b/a;
            s.push(ci);
         } else if (c==';') {
           ;
         }
      }
      ep++;
   }
   int ans=s.top();
   printf("%d\n",ans);
   return 0;
}

void help() {
   printf(
   "Reverse Polish Notation Analyzer\n"
   "by Jakash3\n"
   "Usage: rpn EQUATION\n"
   "Equation is comma delimited equation terminated by semicolon.\n"
   "Decimal numbers can be a maximum of 4 digits\n"
   "Operators available: +, -, *, /\n"
   );
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: