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
|
#include<iostream> //allows me to use cin and cout
#include<iomanip> // allows me to use the setprecision
#include<cmath> // to use pow() and fmod()
#include<string>
using namespace std;
int main()
{
double num1 = 0, num2 = 0; //These will be the numbers entered by the user
char function; //This is the variable that will register * / + - ^ % or negation(absolute value)
// This is my Header
cout << setw(10) << left << "Course" << ":COSC 1436 Programming Fundamentals I" << endl;
cout << setw(10) << left << "Subject" << ":Project I - Simple Calculator" << endl;
cout << setw(10) << left << "Name" << ":Jose Abraham Garcia-Santos" << endl;
cout << endl;
cout << "This program will perform the \nactions of a simple calculator\n";
cout <<"_______________________________________________________________"<< endl << endl << endl;
// This promts the user to make an operation.
cout << "Enter two numbers, whole or decimals with 2 decimal places. \nThe numbers must be separated by the function you wish to execute." << endl;
cout << "If you wish to perform and absolute value, please enter it like so: abs(YourNumber) " << endl;
// I want to create an if statement where if the first character typed is ( | ) then perform the following code for absolute value
cin >> num1;
cin >> function;
function = tolower(function);
if (function == 'a')
{
cin.ignore(numeric_limits < std::streamsize>::max(),'\n');
}
else
cin >> num2;
// with the switch statement I am considering the possible inputs for the character variable function, in case by case.
//The switch will match the input to a case listed and will run the code below it. After that, it will break and skip
//the remaining cases and continue the code.
switch (function)
{
case '+':
cout << endl;
cout << "The answer to (" << num1 << " " << function << " " << num2 << ") is: " << fixed << showpoint << setprecision(2) << num1 + num2 << endl;
break;
case '-':
cout << endl;
cout << "The answer to (" << num1 << " " << function << " " << num2 << ") is: " << fixed << showpoint << setprecision(2) << num1 - num2 << endl;
break;
case '*':
cout << endl;
cout << "The answer to (" << num1 << " " << function << " " << num2 << ") is: " << fixed << showpoint << setprecision(2) << num1 * num2 << endl;
break;
case '/':
cout << endl;
cout << "The answer to (" << num1 << " " << function << " " << num2 << ") is: " << fixed << showpoint << setprecision(2) << num1 / num2 << endl;
break;
case '^':
cout << endl;
cout << "The answer to (" << num1 << " " << function << " " << num2 << ") is: " << fixed << showpoint << setprecision(2) << pow(num1, num2) << endl;
break;
case '%':
cout << endl;
//fmod(double x,double y) will work for decimals.
cout << "The answer to (" << num1 << " " << function << " " << num2 << ") is: " << fixed << showpoint << setprecision(2) << fmod(num1,num2) << endl;
break;
case 'a':
cout << "The answer to abs(" << num1 << ") is: " << abs(num1) << endl << endl;
break;
default:
// default case will run the following code if none of the cases listed are matched.
cout << endl;
cout << "You must enter an operation that contains a number, a function, and another number. Functions"<<endl;
cout<<"you must use are [+],[-],[*],[/],[%],[^],[ abs(YourNumber) ] and the numbers can be whole or decimal values." << endl;
cout << "Please re-Run the program and try again." << endl;
break;
}
return 0;
}
|