Programming and Graphics

(Kiana) #1

70 Introduction to C++ Programming and Graphics


double result; // Resulting value
char oper; // Operator

cout << "Please enter: a (+-*/) b, and hit return" << endl;

while (cin >> left >> oper >> right)
{
switch (oper)
{
case ’+’: result = left + right; break;
case ’-’: result = left - right; break;
case ’*’: result = left * right; break;
case ’/’: result = left / right; break;
default : cout << "Bad operator ’" << oper << "’" << endl;
continue; // Start next loop iteration.
}
cout << "="<< result << endl << "another (q to quit): "
<< endl;
}
return 0;
}

The program performs addition, subtraction, multiplication, and division in a
text mode.


Problems


3.5.1.Discuss whether the statement


y = pow(x, pow(a, b) );

is equivalent to the statement

y = pow(x, a*b);

3.5.2.Investigate by numerical experimentation the action of theceiland
floorfunctions.


3.5.3.Add to the calculator the exponential and the logarithm buttons.


3.6 Readfromafileandwritetoafile


We have learned how to read data from the keyboard and write data to the
screen. To read data from a file and write data to a file, we use the intrinsic
libraryfstream.

Free download pdf