1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream> //
#include <cmath> //
#include <cctype> //
#include <sstream> //
#include <string> //
using namespace std; // standard namespace
char operation; //declared variables
float variable;
float num;
float addition(float, float); // function protoype
float subtraction(float, float);
float multiply(float, float);
float divide(float, float);
float power(float, float);
int main() // main function
{
cout << "Calculator Program!" << endl; // title
cout << endl;
cout << "The following operations are valid:" << endl;
cout << "+ add" << endl;
cout << "- subtract" << endl;
cout << "* multiply" << endl;
cout << "/ divide" << endl;
cout << "^ power" << endl;
cout << "q quit" << endl;
cout << endl;
cout << "Please enter the operation you would like to do and then the number: " << endl; // prompt to choose operation
cin >> operation; // choose operation
float myNumber = 0;
while (operation) // while loop when operation is chosen
{
switch (operation) // switch statement for operation
{
case '+' :
getline(cin, variable);
stringstream myStream(variable);
if (myStream >> myNumber)
{
cout << "invalid number" << endl;
break;
}
else
{
addition(variable, num); // addition function
cout << "the result is now " << num << endl;
break;
}
case '-' :
subtraction(variable, num); // subtraction function
cout << "the result is now " << num << endl;
break;
case '*' :
multiply(variable, num); // multipliation function
cout << "the result is now " << num << endl;
break;
case '/' :
divide(variable, num); // division function
cout << "the result is now " << num << endl;
break;
case '^' :
power(variable, num); // power function
cout << "the result is now " << num << endl;
break;
case 'q' :
case 'Q' :
cout << "The final result was " << num << endl;
cout << endl;
cout << "Thanks for using the calculator program!" << endl;
return 0; // ends program
break;
default:
cout << "invalid operation entered, please try again." << endl; // if a letter or symbol other than the cases was picked
}
cin >> operation;
}
}
float addition(float, float) // addition function
{
num = num + variable; // added to last result
return num; // new result is returned
}
float subtraction (float, float) // subtraction function
{
num = num - variable; // subtracted from result
return num;
}
float multiply (float, float)
{
num = num * variable;
return num;
}
float divide (float, float)
{
if (variable == 0)
{
cout << "Cannot divide by zero, thank you." << endl;
return 1;
}
else
num = num / variable; // divided by result
return num;
}
float power (float, float)
{
num = pow(num, variable); // result raised to power
return num;
}
| |