Created
October 15, 2012 06:31
-
-
Save paranoiacblack/3891078 to your computer and use it in GitHub Desktop.
CS10 SI Classroom Session 3 - Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// Remember to include cmath for math functions | |
#include <cmath> | |
using namespace std; | |
int main() { | |
// Most math functions deal with doubles so we might as well make it easy. | |
double leftOperand = 0.0, rightOperand = 0.0; | |
double result = 0.0; | |
const double PI = 3.14159265, TO_RAD = PI / 180; | |
string operation = ""; | |
cout << "Welcome to easy_calc, a simple calculator!" << endl; | |
cout << "You can perform any math operation possible by the functions " | |
"in the cmath library." << endl << endl; | |
cout << "To use this, simply enter in two floating point numbers and an " | |
<< "operator. For example, to compute 3^4, enter \"3 4 pow\"" | |
<< endl << endl; | |
cout << "If the math calculation you want to compute only takes one operand, " | |
<< "enter two numbers and an operator and only the first number will be " | |
<< "used." << endl; | |
cout << "Enter two floating points numbers and an operator: "; | |
cin >> leftOperand >> rightOperand >> operation; | |
if (operation == "+") { | |
result = leftOperand + rightOperand; | |
} else if (operation == "-") { | |
result = leftOperand - rightOperand; | |
} else if (operation == "*") { | |
result = leftOperand * rightOperand; | |
} else if (operation == "/") { | |
// Sometimes if might be useful to have if statements inside of if | |
// statements. This is called nesting if statements. | |
if (rightOperand != 0.0) { | |
// Now we can avoid those pesky logical errors. | |
result = leftOperand / rightOperand; | |
} else { | |
cout << "I can't divide by 0!" << endl; | |
} | |
} else if (operation == "%") { | |
if (rightOperand != 0.0) { | |
// Remember, remainders only exist in integer division. | |
result = static_cast<int>(leftOperand) % static_cast<int>(rightOperand); | |
} else { | |
cout << "I can't divide by 0!" << endl; | |
} | |
} else if (operation == "abs") { | |
result = abs(leftOperand); | |
} else if (operation == "sin") { | |
// Operations like sin, cos, and tan take in radians instead of degrees. | |
result = sin(leftOperand * TO_RAD); | |
} else if (operation == "cos") { | |
result = cos(leftOperand * TO_RAD); | |
} else if (operation == "tan") { | |
result = tan(leftOperand * TO_RAD); | |
} else if (operation == "pow") { | |
result = pow(leftOperand, rightOperand); | |
} else if (operation == "sqrt") { | |
if (leftOperand >= 0.0) { | |
result = sqrt(leftOperand); | |
} else { | |
cout << "Woah, is the square root of " << leftOperand << " even real?\n"; | |
} | |
} else if(operation == "log") { | |
if (leftOperand > 0.0) { | |
result = log(leftOperand); | |
} else { | |
cout << "You can only take the log of positive numbers. " | |
<< "And 0 isn't positive." << endl; | |
} | |
} else if (operation == "exp") { | |
result = exp(leftOperand); | |
} else { | |
// The default in case we don't know about the operation. | |
cout << "I'm not familiar with how to perform a " << operation | |
<< ". Make sure you spelled it correctly and try again." << endl; | |
} | |
cout << endl << "If everything went fine, your result is " << result << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment