Thu Nov 13, 2008 2:18 pm
/********************************
* Calculator program. Runs in integer or floating point mode.
* Commands are:
* = <number> Enter <number> into the display.
* <op> <number> Compute the operation: + - * / ^
* Note: The space after = or <op> is required.
* i[nteger] Go to integer mode.
* d[ouble] Go to double mode.
* f[loat] Go to double mode.
* q[uit] Exit
*
* There is a "display," and after each command is executed, the display
* is printed out. Operation commands operate on the display as left
* operand and the input number as the right operand, and return the
* result to the display. Each command must be followed by a newline.
* The operations are on integer or double type, depending on mode.
* Changes into the current mode do nothing. Initially, the mode is
* double and the display is zero, and the display is printed once
* before the first command is read. Assumes that execution is
* terminated with a 'q' command, so does not check EOF.
**********************************************************************/
#include <iostream>
#include <math.h>
#include <ctype.h>
using namespace std;
main()
{
int idisp; // Integer mode display val
char mode = 'd'; // Mode = 'd' or 'i'
double ddisp = 0.0; // Double mode display val
// Loop until exited by a quit command.
while(1)
{
// Display.
if(mode == 'd') cout << " " << ddisp << endl;
else cout << " " << idisp << endl;
// Input a command. We issue a prompt, skip leanding
// blanks, read one non-blank which is our command, then
// skip to the next digit, space, or lineend.
char comd; // Command character.
char ch; // Other input character.
cout << '>';
if(cin >> comd) {
// Skip to a digit, space, lineend, or EOF.
while(cin.get(ch)) {
if(isspace(ch)) break;
if(isdigit(ch)) break;
}
// If we reached a digit, pretend we never saw it.
if(isdigit(ch)) cin.unget();
} else
// If we reached EOF, pretend command is quit.
comd = 'q';
// Obey the command.
if(comd == 'q') break;
switch(comd)
{
int in; // Integer input value.
double din; // Double input value.
case '=': // Enter.
if(mode == 'd')
cin >> ddisp;
else
cin >> idisp;
break;
case '+': // Add.
if(mode == 'd')
{
cin >> din;
ddisp += din;
}
else
{
cin >> in;
idisp += in;
}
break;
case '-': // Subtract.
if(mode == 'd')
{
cin >> din;
ddisp -= din;
}
else
{
cin >> in;
idisp -= in;
}
break;
case '*': // Multiply.
if(mode == 'd')
{
cin >> din;
ddisp *= din;
}
else
{
cin >> in;
idisp *= in;
}
break;
case '/': // Divide.
if(mode == 'd')
{
cin >> din;
ddisp /= din;
}
else
{
cin >> in;
idisp /= in;
}
break;
case '^': // Raise to power.
if(mode == 'd')
{
cin >> din;
ddisp = pow(ddisp,din);
}
else
{
cin >> in;
din = rint(pow((double)idisp,(double)in));
}
break;
case 'i': // Integer mode.
if(mode == 'd')
{
idisp = rint(ddisp);
mode = 'i';
}
if(ch == '\n') continue;
break;
case 'd': // Floating mode.
case 'f':
if(mode == 'i')
{
ddisp = idisp;
mode = 'd';
}
if(ch == '\n') continue;
break;
default:
cout << "Bad command, " << comd << endl;
}
/* discard up to a newline. */
while(ch != '\n')
if(!cin.get(ch)) break;
}
exit(0);
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.