I NEED THIS CODE FOR TOMORROW PLEASE SOMEONE Implement a RP
I NEED THIS CODE FOR TOMORROW, PLEASE SOMEONE !!!
Implement a RPN calculator: For the second project, you are required to develop a C++ program that implements an interactive calculator for postfix arithmetic. Use the RPN model we went through in the class. Your first step is to create a dynamic stack implementation that can handle evaluation of an arbitrary long expression (you are not allowed to use the collection classes).
To recap, the postfix expressions (often referred to as “Reverse Polish Notation”) are arithmetic expressions built around a user-implemented stack data structure. These expressions have an advantage over ordinary (in-fix) expressions in that every expression can be written without parentheses. In other words, there is no need to use the parentheses to apply the order of operation when evaluating an expression. Each binary operator expression will be defined as a postfix expression N operands and N-1 operators. The unary operators will have the same number of operators and operands. Each operand is pushed onto the stack. Each binary operator causes the top two operands to be popped off the stack; the first popped operand becomes the second argument to the operator, and the second popped operand becomes the first argument. The operation is then performed on these two operands, and the result is pushed onto the stack. The unary operations are performed in a similar pattern but with one operand.
Four error conditions can occur:
SYNTAX ERROR: valid postfix expressions should consist only of blanks, positive decimal integer operands, and the binary integer operators +, -, *, /, and an unary operator sqrt. The operands and operators will be separated by one or more blanks. Zero or more blanks may appear at either the beginning or the end of the expression. Negative operands are not allowed in the input expression (although the evaluation of a given expression may produce a negative result).
STACK UNDERFLOW: postfix expressions are evaluated from left to right. An underflow error will occur if, at any time during expression evaluation, the number of operators
encountered up to that point does not match the number of operands.
TOO MANY OPERANDS: If, after evaluating the entire postfix expression, the stack contains more than one value, then the given postfix expression contained too many operands.
DIVIDE BY ZERO: If the top operand on the stack is a 0 when the / operator is detected, then a divide-by-zero error will occur. This error cannot be readily detected without evaluation of the expression.
Examples of valid postfix expressions include the following:
1 2 (returns 12)
2 16 sqrt (returns 4)
3 100 567 - (returns -467)
4 1 3 + 4 5 * / (returns [1 + 3] / [4 * 5] = 4 / 20 = 0) 11 22 33 44 55 66 77 88 + - * / / * +
The following are examples of invalid postfix expressions:
12 3f -
 1+
 1 2*/ 34-
 11 23 79 *
 8 3+ 46*
 1 33 22 11 + - / (divide by zero error)
Your program will interactively prompt for and read a postfix arithmetic expression. Your program will then check to see whether the given expression contains a syntax error, a stack underflow error, or a “too many operands” error, in that order. If the syntax of the expression is erroneous, your program should display an appropriate error message.
Your program has to use a stack data structure to evaluate each valid postfix arithmetic expression. Each operand must be converted from an input substring to the numerical value represented by that substring, and then pushed onto the operand stack. To evaluate each operator, your program must pop the stack to retrieve the operand(s), perform the specified
operation on the operand(s), and push the result back onto the stack. Note that for subtraction and division, operand order is critical: the first number popped from the stack should be used as the second operand, while the second number popped from the stack should be used as the first operand. Your program must interactively display the value of the given expression.
After processing the first expression, your program should ask the user whether he or she wishes to enter another expression, and should repeat the entire expression evaluation process until the user responds negatively.
Solution
Please find the answer to the above question as follows:-
#include<iostream>
 #include<stdlib.h>
 #include<math.h>
 using namespace std;
struct node{
 int data;
 struct node *next;
 };
class StackLL{
 struct node *top;
 public:
 StackLL(){
 top=NULL;
 }
 void push(int value);
 int pop();
 int isEmpty();
 int size();
 };
int StackLL::size(){
 struct node *ptr;
 ptr = top;
 int count = 0;
 while(ptr!=NULL){
 ptr = ptr->next;
 count++;
 }
 return count;
 }
void StackLL::push(int value)
 {
 struct node *ptr;
 ptr=new node;
 ptr->data=value;
 ptr->next=NULL;
 if(top!=NULL)
 ptr->next=top;
 top=ptr;
 }
int StackLL::pop()
 {
 struct node *temp;
 if(top==NULL)
 {
cout<<\"\ The StackLL is empty!!!\";
 return -1;
 }
 temp=top;
 top=top->next;
 int data = temp->data;
 delete temp;
 return data;
 }
int StackLL::isEmpty(){
 return top == NULL;
 }
int main(){
 cout<<\"Postfix Expression Evaluation App\"<<endl;
 char RPN[100];
 cin.getline(RPN, 100);
 StackLL myStack;
 int i = 0;
 int j = 0;
 char buffer[100];
 bool error = false;
 for(int k = 0; k<100; k++)
 buffer[k] = \' \';
 while(i!=100){
 if(RPN[i]==\'\\0\'){
 cout<<\"Parsing Expression Finished\"<<endl;
 break;
 }
 else if(RPN[i]==\'+\'){
 if(myStack.size()<2){
 cout<<\"STACK UNDERFLOW\"<<endl;
 error = true;
 break;
 }
 int number2 = myStack.pop();
 int number1 = myStack.pop();
 myStack.push(number1+number2);
 }
 else if(RPN[i]==\'-\'){
 if(myStack.size()<2){
 cout<<\"STACK UNDERFLOW\"<<endl;
 error = true;
 break;
 }
 int number2 = myStack.pop();
 int number1 = myStack.pop(); myStack.push(number1-number2);
 }
 else if(RPN[i]==\'*\'){
 if(myStack.size()<2){
 cout<<\"STACK UNDERFLOW\"<<endl;
 error = true;
 break;
 }
 int number2 = myStack.pop();
 int number1 = myStack.pop();
 myStack.push(number1*number2);
 }
 else if(RPN[i]==\'/\'){
 if(myStack.size()<2){
 cout<<\"STACK UNDERFLOW\"<<endl;
 error = true;
 break;
 }
 int number2 = myStack.pop();
 int number1 = myStack.pop();
 if(number1==0){
 cout<<\"DIVIDE BY ZERO\"<<endl;
 error = true;
 break;
 }
 myStack.push(number1/number2);
 }
 else if(RPN[i]==\'s\'){
 if(RPN[i]==\'s\'&&RPN[i+1]==\'q\'
 &&RPN[i+2]==\'r\' &&RPN[i+3]==\'t\'){
 if(myStack.size()<1){
 cout<<\"STACK UNDERFLOW\"<<endl;
 error = true;
 break;
 }
 i = i + 3;
 int number1 = myStack.pop();
 myStack.push(sqrt(number1));
 }else{
 cout<<\"SYNTAX ERROR\";
 error = true;
 break;
 }
 }
 else if(RPN[i]==\' \'){
 j = 0;
 myStack.push(atoi(buffer));
 for(int k = 0; k<100; k++)buffer[k] = \' \';
 }else{
 if(isdigit(RPN[i]))
 buffer[j++] = RPN[i];
 else{
 error = true;
 cout<<\"SYNTAX ERROR\"<<endl;
 break;
 }
 }
 i++;
 }
 if(error == false){
 if(myStack.size()>1)
 cout<<\"TOO MANY OPERANDS\";
 else
 cout<<myStack.pop();
 }
 return 0;
 }




