Calculator Program
/********************************************************************** * Calculator program. Runs in integer or floating point mode. * Commands are: * <number> Enter <number> into the display. (Number mustn't start * with a + or -, but if it does you can use:) * =<number> Enter <number> into the display. * <op><number> Compute the operation: + - * / ^ * 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. All operations are floating point. EOF on * input is treated as a quit. **********************************************************************/ #include <iostream> #include <string> #include <cctype> #include <cmath> using namespace std; int main() { double disp = 0.0; // Display value. // Loop until exited by a quit command. while(true) { // Display the display, then a prompt. cout << " " << disp << endl << " >> "; // Read a command. If the read fails, we leave the loop. // Note that this "command" might actually be a number // instead of a command. But we'll check it later. string command; cin >> command; if(!cin) break; // If we were asked to quit, now is an excellent time. if(command[0] == 'q') break; // Now, the command could actually be a number entered into // the display. Or it could just be a one-letter command, // and the number comes later. Or it could be a one-letter // command followed by a number. Let's find out. We'll // make command exactly the command, arg the number. double arg; if(command[0] == '.' || isdigit(command[0])) { // Seems to be a number. Convert it, and set the // command to = which assigns the argument to the // display. arg = stod(command); command = "="; } else if(command.length() > 1 && (command[1] == '.' || isdigit(command[1]))) { // Now, the character letter didn't look numberish, // but the second does. We think it's a one-letter // command followed by a number. We extract the // number part into arg, and erase that part of // command. arg = stod(command.substr(1)); command.erase(1); } else { // Well, maybe we just put the command, and the // number comes later. Keep the command, and // read the number. cin >> arg; } // If we reached eof, time to stop. if(cin.eof()) break; // Reading numbers will fail if you type a bad character. // We're guessing that's what happened, since most of the other // possiblities mean you're not connected anymore and won't see // our message anyway. if(!cin) { cout << "You need to enter a number." << endl; // We want to clear out whatever crud caused the // read error. We use cin.clear() to return the // strean to the good state (otherwise it won't // try to actually read anything), then read the // rest of the line into a string, which we ignore. // And try again. cin.clear(); string discard; getline(cin, discard); continue; } // Obey the command. Unfortunately, C++ won't let you // switch on a string. By a happy coincidence, all our // commands are one-letter, and we can switch just fine // on a single character. switch(command[0]) { case '=': // Enter. disp = arg; break; case '+': // Add. disp += arg; break; case '-': // Subtract. disp -= arg; break; case '*': // Multiply. disp *= arg; break; case '/': // Divide. disp /= arg; break; case '^': // Raise to power. Pow() is std library function. disp = pow(disp,arg); break; default: cout << "Bad command, " << command << endl; // Read and discard the rest of the line. string discard; getline(cin, discard); } } }