|
I have two questions, someoen can help me to answer or explain them. Thanks!!
if we use the standard library class stack in our calculator, the method top() returns the top entry off the stack as its result. Then the function do_command can then be shortened considerably by writing such statements as case'-': numbers.push(nubers.pop()-numbers.pop())
1. assuming that this statement works correctly, explain why it whould still be bad programming style. 2. It is possible that two diffrent C++ compilers, both adhering strictly to standard C++, would translate this statement in ways that would give different answers when the program runs. Explain how this could happen.
Thank you!!!!
void introduction();
void instructions();
char get_command();
bool do_command(char command, Stack &numbers);
int main() /*Post: The program has executed simple arithmetic commands entered by the user. Uses: The class Stack and the functions introduction, instructions, do_command, and get_command. */ { Stack stored_numbers; introduction(); instructions(); while (do_command(get_command(), stored_numbers)); }
void introduction() /* Stub */ { }
void instructions() /* Stub */ { }
char get_command() /* Post: Returns a command (?, =, +, -, *, /, q) entered by the user */ { char command; bool waiting = true; cout << "Select command and press < Enter > :"; while (waiting){ cin >> command; command = tolower(command); if (command == '?' || command == '=' || command == '+' || command == '-' || command == '*' || command == '/' || command == 'q') waiting = false; else { cout << "Please enter a valid command:" << endl << "[?]push to stack [=]print top" << endl << "[+][-][*][/] are arithmetic operations" << endl << "[Q]uit." << endl; } } return command; }
bool do_command(char command, Stack &numbers) /* Pre: The first parameter specifies a valid calculator command. Post: The command specified by the first parameter has been applied to the Stack of numbers given by the second parameter. A result of true is returned unless command == 'q'. Uses: The class Stack. */ { double p, q; switch (command) { case '?': cout << "Enter a real number: " << flush; cin >> p; if (numbers.push(p) == overflow) cout << "Warning: Stack full, lost number" << endl; break; case '=': if (numbers.top(p) == underflow) cout << "Stack empty" << endl; else cout << p << endl; break; case '+': if (numbers.top(p) == underflow) cout << "Stack empty" << endl; else { numbers.pop(); if (numbers.top(q) == underflow) { cout << "Stack has just one entry" << endl; numbers.push(p); } else { numbers.pop(); if (numbers.push(q + p) == overflow) cout << "Warning: Stack full, lost result" << endl; } } break; case '-': if (numbers.top(p) == underflow) cout << "Stack empty" << endl; else { numbers.pop(); if (numbers.top(q) == underflow) { cout << "Stack has just one entry" <<endl; numbers.push(p); } else { numbers.pop(); if(numbers.push(q - p) == overflow) cout << "Warning: Stack full, lost result" <<endl; } } break; case '*': if (numbers.top(p) == underflow) cout << "Stack empty" << endl; else { numbers.pop(); if (numbers.top(q) == underflow) { cout << "Stack has just one entry" <<endl; numbers.push(p); } else { numbers.pop(); if(numbers.push(q * p) == overflow) cout << "Warning: Stack full, lost result" <<endl; } } break; case '/': if (numbers.top(p) == underflow) cout << "Stack empty" << endl; else { numbers.pop(); if (numbers.top(q) == underflow) { cout << "Stack has just one entry" <<endl; numbers.push(p); } else { numbers.pop(); if(numbers.push(q / p) == overflow) cout << "Warning: Stack full, lost result" <<endl; } } break; case 'q': cout << "Calculation finished.\n"; return false; } return true; }
This post has been edited by ickiepig: 3 Feb, 2008 - 04:21 PM
|