Please help me this C program For this assignment you are to

Please help me this C++ program
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 (array implementations of the stack will not receive full credit).

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

make sure it work

Solution

Please execute the following code. Stack has been implemented using linked list.

#include <iostream>
#include <iostream>
#include <string.h>
#include <sstream>
#include <stdlib.h>
using namespace std;

struct node {
    float data;
    struct node *next;
};

class stack {
    struct node *top;
    public : stack() {
        top = NULL;
    }
    void push(float element);
    float pop();
    float peek();
    bool isEmpty();
    int size();
  
};

bool stack::isEmpty() {
    if (top == NULL) {
        return true;
    }
    return false;
}

float stack::peek() {
    if (top == NULL) {
        cout << \"Error : empty stack\";      
    }
    return top->data;
}

void stack::push(float element) {
    struct node *newNode;
    newNode = new node;
    newNode->data = element;
    newNode->next = NULL;
    if (top != NULL)
        newNode->next=top;
    top=newNode;
}

float stack::pop() {
    struct node *temp;
    if(top==NULL) {
        cout<<\"Error : empty stack\";
    }
    temp=top;
    top=top->next;
    float value = temp->data;
    delete temp;
    return value;
}

int stack::size() {
    struct node *temp;
    temp=top;
    int count = 0;
    while (temp != NULL) {
        count = count + 1;      
        temp=temp->next;
    }
    return count;
}

int main() {
    while (1) {
        stack s;
        float first, second;
        string input, str1;
      
        getline(cin,input);
        istringstream is(input);
      
        if (input == \"0\") {
            return 0;
        }
      
        bool flag = true;
        for(;is>>str1;) {

            if (str1.compare(\"+\")==0) {
                if (s.size() < 2) {
                    cout << \"Error: Too many operators\ \";
                    flag = false;
                    break;
                }
                second=s.pop();          
                first=s.pop();
                s.push(first+second);
            }
            else if (str1.compare(\"-\")==0) {
                if (s.size() < 2) {
                    cout << \"Error: Too many operators\ \";
                    flag = false;
                    break;
                }
                second=s.pop();          
                first=s.pop();
                s.push(first - second);
            }
            else if(str1.compare(\"*\")==0) {
                if (s.size() < 2) {
                    cout << \"Error: Too many operators\ \";
                    flag = false;
                    break;
                }
                second=s.pop();          
                first=s.pop();
                s.push(first * second);
            } else if(str1.compare(\"/\")==0){
                if (s.size() < 2) {
                    cout << \"Error: Too many operators\ \";
                    flag = false;
                    break;
                }
                second=s.pop();          
                first=s.pop();
                if (second == 0) {
                    cout << \"Error: Division by zero\ \";
                    flag = false;
                    break;
                }
                s.push(first / second);
            } else {
                s.push(strtof(str1.c_str(),NULL));
            }
        }
      
        if (s.size() == 1 && flag) {
            cout << s.pop(); cout << \"\ \";
        }
        else if (s.size() > 1) {
            cout << \"Error: Too many operands\ \";      
        }
    }
}  

Please help me this C++ program For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provide
Please help me this C++ program For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provide
Please help me this C++ program For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provide

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site