Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,042 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,612 people online right now. Registration is fast and FREE... Join Now!




C++ Need Help, Please~~

 
Reply to this topicStart new topic

C++ Need Help, Please~~

ickiepig
3 Feb, 2008 - 03:54 PM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2007
Posts: 12


My Contributions
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
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: C++ Need Help, Please~~
3 Feb, 2008 - 04:17 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
Is this an assignment or something?
You could attempt the questions and we can correct them.

As a start: using 2 pop operations in the same line of code would be bad style. This assumes that the stack contains 2 elements, and does not check what the result of this subtraction is and pushes it back on the stack.
User is offlineProfile CardPM
+Quote Post

ickiepig
RE: C++ Need Help, Please~~
3 Feb, 2008 - 04:23 PM
Post #3

New D.I.C Head
*

Joined: 24 Oct, 2007
Posts: 12


My Contributions
QUOTE(William_Wilson @ 3 Feb, 2008 - 05:17 PM) *

Is this an assignment or something?
You could attempt the questions and we can correct them.

As a start: using 2 pop operations in the same line of code would be bad style. This assumes that the stack contains 2 elements, and does not check what the result of this subtraction is and pushes it back on the stack.


I have put my code on, I dont really understand how this could happen.

This post has been edited by ickiepig: 3 Feb, 2008 - 04:25 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month