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:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

4-) 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:

 

 

#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;

}

 

Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY