(COMP2012)[2016](f)midterm_=vngkoee^_10579.pdf

If the element is an operator pop operands for the

Info icon This preview shows pages 12–15. Sign up to view the full content.

If the element is an operator, pop operands for the operator from stack, evaluate the operator and push the result back to the stack When the expression is ended, the number in the stack is the final answer Your task is to write a main function (”main-postfix.cpp”) that prompts the user to input an postfix expression, evaluates the expression and output the result on screen. Include all the preprocessor directives that your program will use. The following shows the expected output of your solution: Enter a postfix expression: 7 2 * 3 4 / - 5 + Result: 18.25 You may assume that the user will always enter correct expression in the correct format, and you don’t need to check for invalid inputs. Also the length of the postfix expression is less than 100. Hints: getline() function can be used to get a line of string from the input console, e.g., string str; getline(cin, str); The length of a string can be found using length() of string class, e.g., string str = "Hello World"; str.length(); // Length is 11 operator[] can be used to get the character at certain position, e.g., string str = "Hello World"; char ch = str[1]; // ch = ’e’ Conversion of numeric character to int can be done as follows: char ch = ’9’; int val = ch - ’0’; // val = 9 12 (COMP2012)[2016](f)midterm~=vngkoee^_10579.pdf downloaded by xjiae from at 2018-03-13 17:27:22. Academic use within HKUST only.
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Answer: /* ”main-postfix.cpp” */ // Implement your main function here #include < iostream > #include < string > #include Stack.h using namespace std; int main() { string expression; Stack < double > stack(100); // 1 point cout "Enter a postfix expression: " ; // 1 point for cout and getline getline(cin, expression); for ( int i=0; i < expression.length(); i++) { // 1 point char ch = expression[i]; if (ch != ’+’ && ch != ’-’ && ch != ’ * ’ && ch != ’/’ && ch != ’ ’) // 1 point stack.push(ch - ’0’); // 1 point else if (ch != ’ ’) { // 1 point double * val1 = stack.pop(); // 1 point for performing pop operations double * val2 = stack.pop(); double result; switch (ch) { // 1 point case ’+’: result = * val2 + * val1; break ; case ’-’: result = * val2 - * val1; break ; case * ’: result = * val2 * * val1; break ; case ’/’: result = * val2 / * val1; break ; } stack.push(result); // 1 point } } cout "Result: " * (stack.pop()) endl; // 1 point return 0; } 13 (COMP2012)[2016](f)midterm~=vngkoee^_10579.pdf downloaded by xjiae from at 2018-03-13 17:27:22. Academic use within HKUST only.
Image of page 13
Problem 7 [30 points] Class and Operator Overloading (a) The following shows a typical class definition for a Student. Complete the missing parts in the space provided under Part(a)(i)-(a)(iii) ” ADD YOUR CODE HERE ” by declaring (i) greater than operator operator> , (ii) less than operator operator< for the Student class, and making (iii) the insertion operator operator<< as a friend function of Student.
Image of page 14

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern