Below is a program written in order to perform a task, trace the program and provide comments for the code. In a few sentences, describe what does the program do? and show the result of the execution:
4-) Below is a
#include <iostream> #include <cstring> #include <stack> using namespace std; #define maxchars 100//
int main() { char input_str[maxchars+1], *p; stack<double> num_stack; int c; double a, b, n;
cout << "Enter string: "; cin.getline(input_str, maxchars); p = strtok(input_str, " "); while (p) { c = p[0]; if (c == '+' || c == '*' || c == '/' || c == '-') { if (num_stack.size() < 2) { cout << "Error: too many ops." << endl; system("pause"); return -1; } b = num_stack.top(); num_stack.pop(); a = num_stack.top(); num_stack.pop(); switch (c) { case '+': n = a + b; break; case '*': n = a * b; break; case '/': n = a / b; break; case '-': n = a - b; break; } num_stack.push(n); } else { num_stack.push(atof(p));
} p = strtok(NULL, " "); } cout << "The answer is: " << num_stack.top() << endl; system("pause"); return 0; }
|
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)