In: Computer Science
IF YOU HAVE ANSWERED THIS QUESTION BEFORE THEN PLEASE DONOT ANSWER AGAIN.
Your task for this assignment is to implement a stack data structure in C++. This may be accomplished by utilizing the C++ standard template library (STL) or by utilizing a user-defined class.
Implement a transaction-based stack data structure using C++. The program will be interactive. Data transactions will be entered at the command line and results will be displayed on the console.
Each input transaction will contain an arithmetic expression in post-fix format. Assume that operands and operations are separated by a space on an input line that contains one arithmetic expression. An operand will be a positive or negative float number. An operation may be +, -, * or /. You may assume that each arithmetic expression is correctly formatted. However, your program should not allow an attempt to divide by zero. Instead of dividing by zero, your program should display an error message and then begin evaluating a new input transaction.
Sample input transactions are these:
2.0 3.3 + (result = 5.3)
34*-2.5/ (result=-4.8)
9.5 8 3.0 2 * 6 - / + (result= “error: division by zero”)
An input transaction containing “end-of-file” indicates there are no more transactions to be processed. Implement a stack to evaluate each expression and display the result. Use the C++ built-in class or a user-defined class to implement stack functions.
The program will be run at the command prompt by navigating to the directory containing the executable version of the program after the program is compiled. The program should display a prompt requesting input, such as “Please enter an expression in post-fix notation:”.
5. You are encouraged to add additional comments throughout the program that your feel might be helpful to the reader of your source code.
#include <bits/stdc++.h>
#include <stack>
using namespace std;
int main()
{
string S, T;
cout<<"Please enter an expression in post-fix notation:";
//to create a stack to hold input strings
stack<string> st;
//to input the entire string
getline(cin, S);
//to split the given string by the delimeter space ' '
stringstream X(S);
while (getline(X, T, ' ')) {
//if T is any operator
if(T=="+" || T=="-"|| T=="*"|| T=="/"){
//here first we need to convert the string to float using stof()
float val1=stof(st.top());
st.pop();
float val2=stof(st.top());
st.pop();
//to check for division by zero error
if(val1==0.0 && T=="/"){
cout<<"error: division by zero"<<endl;
return 0;
}
//to perform required operation and pushing the result into the stack,here we have
//to use to_string() function to convert the float value to string before pushing.
if(T=="+")
st.push(to_string(val2 + val1));
else if(T=="-")
st.push(to_string(val2 - val1));
else if(T=="*")
st.push(to_string(val2 * val1));
else if(T=="/")
st.push(to_string(val2/val1));
}
//if T is any operand then we just need to push it into the stack
else{
st.push(T);
}
}
//to print the result which will be the only one element in the stack
cout<<"result = "<<stof(st.top());
return 0;
}
Sample Input and output of above code:
Please enter an expression in post-fix notation:
Input:9.5 8 3.0 2 * 6 - / +
Output:error: division by zero
Please enter an expression in post-fix notation:
Input:2.0 3.3 +
Output: 5.3
Please enter an expression in post-fix notation:
Input : 3 4 * -2.5 /
Output: -4.8
Code Explainations:
Here we are evaluating of postfix expression by using stack.
The algorithm for evaluation of postfix expressions will be:
Step 1) Create a stack of string to store operands
(or values).
Step 2) We need to scan the given expression and
will do following for every scanned element.
a) If the element is a float number,we have to
push it into the stack
b) If the element is any operator(+,-,*,/ etc), we
need to pop two operands from stack. Then we have to evaluate the
expression as <second operand >operator< first
operand >and again need to push the result back to
the stack
c) while evaluating
expression we need to check for zero division error as well as
like if first operand is 0.0 and the operator is division
that is '/'
Step 3) When the expression is ended, the number
at the top of the stack will be our required answer.
Note: While implementing this code I have taken care of the point that the given input expression will have space seperation between operands and operator. Also I have implemented it in such a way that if there is any zero division error occurs that is whenever we try to divide a number by 0 then it will raise an error message and just return, as in case Input:9.5 8 3.0 2 * 6 - / +
If you are having doubts regarding the working of this codes or anything else which is not being understood then please feel free to ask me in comment section I will be more then happy to solve your doubts!!
further for better understanding of code I have provided the comments in the code itself and also i am providing the code screenshots for quick reference
Code Screenshots: