0

Write a Program that models a simple calculator. Each data entry line should consist of a valid operator (from the list below), and the right-hand operand. Assume that the left-hand operand is the accumulated value in the calculator, with an initial value of 0.0.

Acceptable operators:

+ ..Add

- ..Subtract

* ..Multiply

/ ..Divide

^ ..Power (raise left operand to the power of the right operand)

q or = ..Quit

Your Calculator should display the accumulated value after each operation. A sample run might be:

+ 5.0

Result so far is 5.00

^ 2

Result so far is 25.00

/ 2.0

Result so far is 12.50

Q 0

The final result is 12.50

Include (define and call) at least THREE functions:

a function that displays instructions to the user.
a function do_next_op() that has 3 input parameters (the operator, the operand, and the current accumulated value), and returns the new value for the accumulated value. An alternative implementation may use 2 input parameters (operator and operand), and 1 input/output parameter (the accumulated value)
at least one other function - of your choice! Make sure that it does something useful.

3
Contributors
3
Replies
5
Views
8 Years
Discussion Span
Last Post by Freaky_Chris
0

So, what code have you done already, and what are your problems? (In case you haven't heard, we won't do your homework)

0

I am not sure if I did anything work and I was working it now, but can you help me that be much appreciated, I not sure if I got the Flush(cin) right? so please help me. Thanks.

#include <iostream>
using namespace std;
#include <cmath>




void instruction();
int calculator (char);
void divide_by_zero ();
float do_next_op (char, float, float);
istream& FLUSH(istream& stream);

int main()
{ 
//input
char input;
float accum,num;

// instructions ();
void intruction();
accum = 0 ;

do
{ 
//get input 
cout<< " : " ;
cin >> input;
cin >> num;
if (cin.fail(c);
cerr << "Unknown operator " << input << '\n';
if (value <= 0)
{
cout << "error! DIVIDE BY ZERO!! "; 
cout << endl;
}

// Do op
accum = do_next_op (input, num, accum);
//display result
cout << " Result so far : " << accum << endl;

cout << "  "<< endl      ;
} // end not quit
while (input != 'Q' && input != 'q' && input != '=');

cout << "Final result : " ;
system ("pause") ;  
return 0 ;
} 
 
istream& FLUSH(istream& stream);
{
stream.clear();
int ch;
while ((ch = stream.get()) != '\n' && ch != EOF) 
{
   ; // Do nothing inside loop - it's all in the condition!
}
stream.clear();
return stream;
}

//instruct user 
void instruction()
{ 
cout <<" This program models a simple calculator which can add, subtract,multiply,divide, power" << endl;
cout << " the calculator accumulate value after each operation " << endl;
cout << " press Q to quit the program " << endl;
}

float do_next_op (char op, float num, float total)
{
switch (op)
{
case '+' :
total += num;
break;

case '-':
total -= num;
break;

case '*':
total *= num;
break;

case '/': 
total /= num;
break;

case '^':
total = pow (total,num);
break ;

default :
cout << " syntax error" << endl;
}

return total;
}
0

Please use [code=cplusplus] [/code] tags.
You need to learn how to call functions since from what I can see you have no idea. I suggest going over the basics again before attempting to carry on.

Chris

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.