Question

In: Computer Science

REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will...

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.

Solutions

Expert 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<<"\nstack is empty\n";
       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\n";
               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]<<"\n";
      
       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\n";
   main();
   return 0;
}

ouput


Related Solutions

Reverse Polish (HP) Style Calculator - Part 2 The purpose of this assignment is to incorporate...
Reverse Polish (HP) Style Calculator - Part 2 The purpose of this assignment is to incorporate an abstract class and inheritance and add it to the interface of the business calculator created in Topic 4. • Implement a pure abstract stack class named AbstractStack that has no implementation. It will not have a private array of double, because that is an implementation detail of ArrayStack that would not be found in other implementations such as LinkedStack. Nor will it have...
For this assignment you will develop pseudocode and write a C++ program for a simple calculator....
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below. PSEUDOCODE FILE NAME: Calculator.txt C++ SOURCE CODE FILE NAME : Calculator.cpp DESCRIPTION: Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt...
Write a Reverse Polish Calculator, RPN We discussed in class a general implementation of RPN using...
Write a Reverse Polish Calculator, RPN We discussed in class a general implementation of RPN using a stack to keep track of the numbers. Each time an operand is encountered, two values are popped from this stack, the given operation is performed, and the result is pushed back onto the stack. Utilize Java generic Stack Class to implement this algorithm with four arithmetic operations: +, *, -, /. It is suggested that you implement a class RPN with a single...
Write a Reverse Polish Calculator, RPN in JAVA Each time an operand is encountered, two values...
Write a Reverse Polish Calculator, RPN in JAVA Each time an operand is encountered, two values are popped from this stack, the given operation is performed, and the result is pushed back onto the stack. Utilize Java generic Stack Class to implement this algorithm with four arithmetic operations: +, *, -, /. Implement a class RPN with a single static method evaluate. This method should have the following signature:             public static String evaluate(String expression) It should split the passed...
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called...
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called Operand Give it a virtual destructor to avoid any weird problems later on! Derive a class called Number from Operand Maintain a double member variable in class Number For simplicity, you may make the member variable public if you would like Derive a class called Operator from Operand Derive a class called Add from Operator (2 + 3 = 5) Derive a class called...
This code needs to run a working Reverse Polish Calculator, but when I input my commands...
This code needs to run a working Reverse Polish Calculator, but when I input my commands it only pops my inputs never push them. So my answer is always zero. Here is my code. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> struct Node { int element; struct Node *next; }; //Global variable struct Node *top = NULL; void push(int ele) { struct Node *newNode; newNode = (struct Node *) malloc(sizeof(struct Node)); newNode->element = ele; newNode->next = top; top = newNode; }...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack (or your own implementation) for this problem. Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which...
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT