REVERSE POLISH CALCULATOR C ONLY For this assignment you are
REVERSE POLISH CALCULATOR
C++ ONLY.
For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user.
 
 You must use a linked list to maintain the stack for this program (NO array implementations of the stack).
 
 You must handle the following situations (errors):
 Too many operators (+ - / *)
 Too many operands (doubles)
 Division by zero
 
 The program will take in a Polish expression that separates the operators and operands by a single space, and terminates the expression with an equals sign.
 
 The program will continue to take and evaluate expressions until the user enters a zero (0) on a line by itself followed by a new line.
 
 Your sample output should show the handling of all the error conditions as well as make use of all of the operators.
 
 Sample IO: (note: formatting of output isn’t a critical issue)
 Input Output
 10 15 + = 25
 10 15 - = -5
 2.5 3.5 + = 6 (or 6.0)
 10 0 / = Error: Division by zero
 10 20 * / = Error: Too many operators
 12 20 30 / = Error: Too many operands
 -10 -30 - = 20
 100 10 50 25 / * - -2 / = -40
Thanks. Will rate if solution is correct.
Solution
#include <iostream>
 #include <iomanip>
 #include <vector>
 #include <string>
 #include <algorithm>
 #include <iterator>
 #include <sstream>
 #include <stdlib.h>
 #include <ctype.h>
 #include<string>
 using namespace std;
 struct stack//linked list for stack
 {
    double data;
    struct stack *next;
 };
 struct stack *head,*top;
void push(double d)//pushing to stack...//operand stack
 {
    if(head==NULL)
    {
        head = new stack();  
        head->data = d;
        head->next =NULL;
        top = head;
    }
    else
    {
        top->next = new stack();
        top->next->data =d;
        top=top->next;  
    }
   
 }
 int stack_length()//finding number of elements in stack
 {
    struct stack *temp =head;
    int i=0;
    while(temp!=NULL)
    {
        i++;
        temp = temp->next;  
    }
    return i;
   
 }
 double pop()//poping element from stack
 {
    double d;
    struct stack *temp=head;
    if(head==NULL)
    {
        cout<<\"\ stack is empty\ \";
        exit(0);
    }
    if(head==top)
    {
        d=head->data;
        head =NULL;
        top = NULL;  
    }
    else
    {
    while(temp->next != top)
    {
        temp=temp->next;  
    }
    d=top->data;
    temp->next =NULL;
    top=temp;
    }
    return d;
 }
//spliting given string at spaces
 void split(const std::string& str, std::vector<std::string>& v) {
 std::stringstream ss(str);
 ss >> std::noskipws;
 std::string field;
 char ws_delim;
 while(1) {
     if( ss >> field )
       v.push_back(field);
     else if (ss.eof())
       break;
     else
       v.push_back(std::string());
     ss.clear();
     ss >> ws_delim;
 }
 }
bool is_digit(string a)//finding whether digit or not..
 {
   
    bool isNumber = true;
    int i;
    for(i=0;a[i]!=\'\\0\';i++)
    if(a[i]!=\'.\')if(!isdigit(a[i])){
    isNumber=false;
    break;
    }
   
     return isNumber;
 }
 bool evaluate(string e)//evaluating expression
 {
    if(stack_length()>=2)
    {
        double p1,p2;
        if(e==\"+\")
        {
            p1=pop();
            p2=pop();
            push(p1+p2);
           
        }
        else if(e==\"-\")
        {
            p1=pop();
            p2=pop();
            push(p2-p1);
        }
        else if(e==\"/\")
        {
            p1=pop();
            p2=pop();
            if(p1==0)
            {
                cout<<\"Error:Division by zero\ \";
                exit(0);
                return true;
            }
            push(p2/p1);  
        }
        else
        {
            p1=pop();
            p2=pop();
            push(p1*p2);
           
        }
       
       
           
    }
    else
    {
        if(e==\"=\")return false;
        cout<<\"Error:Too many operators\";
        exit(0);
        return true;  
    }
    return false;
   
 }
 int main()
 {
    string s;
    cout<<\"Enter:\";
    getline(cin,s);
    vector<string> e;
    split(s,e);
   
   
    int i=0;
    int d;
   
    while(i<e.size())
    {
        //cout<<e[i]<<\"\ \";
       
        if(is_digit(e[i]))
        {
               push(atof(e[i].c_str()));
        }
        else
        {
            if(evaluate(e[i]))
            {
                break;  
            }
               
        }  
        i++;
    }
   
    if(stack_length()==1)cout<<pop()<<endl;
    else cout<<\"Error: Too many operands\ \";
   
    return 0;
 }




