Question

In: Computer Science

IN C++ PLS COMMENT CODDEE, SO I CAN UNDERSTAND WHAT YOU DOING Stack application: postfix expression...

IN C++
PLS COMMENT CODDEE, SO I CAN UNDERSTAND WHAT YOU DOING

Stack application: postfix expression evaluation.

Description:
- The stack data structure can be used to evaluate postfix expressions. Please refer to the first 14 pages of this tutorial for postfix expression evaluation: http://www.cs.nthu.edu.tw/~wkhon/ds/ds10/tutorial/tutorial2.pdf

Requirement:
- deal with single digit positive integers only. Operands and operators are fully separated by space. 
- learn and use STL stack: http://www.cplusplus.com/reference/stack/stack/
- learn and use isdigit(): http://www.cplusplus.com/reference/cctype/isdigit/  
- take in a postfix arithmetic expression from cin, and evaluate its value. 
- supported operators: +, -, *, /
- for invalid postfix expression, print out an error message and end the program. 
- output the evaluated value of valid expressions. 

Example1: 
- (valid expr) 4 3 - 5 * 
-- push 4 into stack 
-- push 3 into stack 
-- minus operator (-) detected: pop 3 out of stack, as operand_2
-- pop 4 out of stack, as operand_1
-- perform operand_1 minus operand_2, then push the result (1) into stack. 
-- push 5 into stack 
-- multiply operator (*) detected: pop 5 out of stack, as operand_2
-- pop 1 out of stack, as operand_1
-- perform operand_1 times operand_2, then push the result (5) into stack. 
-- input done. check stack value... output final answer: 5

Solutions

Expert Solution

// C++ program to evaluate postfix expression consisting of single digit positive integer and

// operators and operands are fully separated by space

#include <iostream>

#include <stack>

#include <cctype>

using namespace std;

int main() {

               string postfix_exp;

               stack<int> st;

               cout<<"Program to evaluate postfix expression(consisting of single digit positive integers only and Operands and operators are fully separated by space. ) "<<endl;

               // input of postfix expression

               cout<<"Enter the postfix expression : ";

               getline(cin,postfix_exp); // input the string with spaces

               postfix_exp = postfix_exp.substr(0,postfix_exp.length()-1); // remove '\n' from end

               // loop over the expression

               for(unsigned int i=0;i<postfix_exp.length();i++)

               {

                              // check if the character is a digit, then push the int to the stack

                              if(isdigit(postfix_exp[i]))

                                             st.push((int)(postfix_exp[i]-'0')); // convert the char to int and push it to stack

                              else if(!isspace(postfix_exp[i])) // if the character is not a space

                              {

                                             int n1, n2;

                                             // check if stack is empty, then print error and exit

                                             if(st.empty())

                                             {

                                                            cout<<"ERROR: Invalid expression "<<endl;

                                                            return 1;

                                             }

                                             // if stack is not empty, remove and assign the top element to n2

                                             n2 = st.top();

                                             st.pop();

                                             // check if stack is empty, then print error and exit

                                             if(st.empty())

                                             {

                                                            cout<<"ERROR: Invalid expression "<<endl;

                                                            return 1;

                                             }

                                             // if stack is not empty, remove and assign the top element to n1

                                             n1 = st.top();

                                             st.pop();

                                             // check the operator

                                             if(postfix_exp[i] == '+') // addition

                                                            st.push(n1+n2); // perform addition and push the result back to stack

                                             else if(postfix_exp[i] == '-') // subtraction

                                                            st.push(n1-n2); // perform n1-n2 and push the result back to stack

                                             else if(postfix_exp[i] == '*') // multiplication

                                                            st.push(n1*n2); // perform multiplication and push the result back to stack

                                             else if(postfix_exp[i] == '/') // division

                                             {

                                                            // check if n2 is not zero, then perform n1/n2 and push the result back to stack

                                                            if(n2 != 0)

                                                                           st.push(n1/n2);

                                                            else // if n2 is 0, print error and exit

                                                            {

                                                                           cout<<"ERROR: division by zero "<<endl;

                                                                           return 1;

                                                            }

                                             }else // if any other character found, print error and exit

                                             {

                                                            cout<<"ERROR: Invalid operator "<<endl;

                                                            return 1;

                                             }

                              }

               }

               // if stack is empty after scanning the entire expression, print error and exit

               if(st.empty())

                              cout<<"ERROR: Invalid expression "<<endl;

               else{

                              // get the result from top of stack

                              int result = st.top();

                              st.pop(); // remove the result from top

                              // if stack is non-empty after getting the result, print error and exit else print result

                              if(st.empty())

                                             cout<<"Result : "<<result<<endl;

                              else

                                             cout<<"ERROR: Invalid expression "<<endl;

               }

               return 0;

}

//end of program

Output:


Related Solutions

In C++ and pls comment every line so I UNDERSTAND Implement a class named DynamicArray that...
In C++ and pls comment every line so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep...
CS 400 Assignment 4 Stack application: postfix expression evaluation. Description: - The stack data structure can...
CS 400 Assignment 4 Stack application: postfix expression evaluation. Description: - The stack data structure can be used to evaluate postfix expressions. Please refer to the first 14 pages of this tutorial for postfix expression evaluation: http://www.cs.nthu.edu.tw/~wkhon/ds/ds10/tutorial/tutorial2.pdf Requirement: - deal with single digit positive integers only. Operands and operators are fully separated by space. - learn and use STL stack: http://www.cplusplus.com/reference/stack/stack/ - learn and use isdigit(): http://www.cplusplus.com/reference/cctype/isdigit/ - take in a postfix arithmetic expression from cin, and evaluate its value....
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Can you please write this in python and comment as well so I can understand what...
Can you please write this in python and comment as well so I can understand what yo are doing. Thank you. 1)Loop through list A, at each iteration, show the square root result of that element. Add proper text to your print function. A = [-4, 1, -16, 36, -49, 64, -128] 2)Create a counter variable, name it i and initialize it to 0. Using a for loop, count how many numbers are divisible by 3 in range of 1...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
This code is an expression of cp command in c language. But I don't understand this...
This code is an expression of cp command in c language. But I don't understand this code very well. Please explain in notes one by one.(in detail) #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<errno.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc, char *argv[]) { char ch; int src, dst; struct stat st; if(argc != 3) { printf("argument error \n"); printf("usage: ./a.out src dest \n"); exit(0); } if (stat(argv[1], &st) == -1) { perror("stat : "); exit(-1); } src = open(argv[1], O_RDONLY); if(src...
This code is an expression of cp command in c language. But I don't understand this...
This code is an expression of cp command in c language. But I don't understand this code very well. Please explain in notes one by one. #define SIZE 1024 #include<string.h> #include<stdio.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char *argv[]){ if(argc != 3){ perror("argument 부족\n"); exit(0); } struct stat frstatbuf; FILE* fr = fopen(argv[1], "r"); if(fr == NULL){ perror("read file 읽기 오류\n"); exit(0); } int frfd = fileno(fr); fstat(frfd, &frstatbuf); FILE* fw=fopen(argv[2], "w+"); int fwfd=fileno(fw); fchmod(fwfd,frstatbuf.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO)); char buf[1024]; while(1){...
Please explain to me the answers so that I can understand the concept. Thank you! You...
Please explain to me the answers so that I can understand the concept. Thank you! You must evaluate a proposal to buy a new milling machine. The purchase price of the milling machine, including shipping and installation costs, is $126,000, and the equipment will be fully depreciated at the time of purchase. The machine would be sold after 3 years for $85,000. The machine would require a $3,500 increase in net operating working capital (increased inventory less increased accounts payable)....
what are the formulas for each column so i can understand where the numbers are coming...
what are the formulas for each column so i can understand where the numbers are coming from. refer to Question: Historical data indicate that a student's income for any month...part of the answer has been posted on Chegg but the formulas are needed.Thanks! Assuming the student begins the school year with a balance of $1200, use Excel to simulate 12 months of activity and to predict the position of the student at the end of the year. Historical data indicate...
(Please show work so I can understand how you got to the answer - Thank you...
(Please show work so I can understand how you got to the answer - Thank you very much ) Via Gelato is a popular neighborhood gelato shop. The company has provided the following data concerning its operations: Fixed Element per Month Variable Element per Liter Actual Total for June Revenue $ 13.00 $ 72,540 Raw materials $ 4.75 $ 30,330 Wages $ 5,700 $ 1.50 $ 14,560 Utilities $ 1,730 $ 0.30 $ 3,800 Rent $ 2,700 $ 2,700 Insurance...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT