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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include<iostream>
#include <string>
#include <cmath>
using namespace std;
void welcome(); // to print the welcome message
void promptUser(int&, char&, int&); // prompt user for expression to evaluate.
// Function should return the 2 operands and the operator
// in reference parameters
int calculate(int, char, int); // evaluate the expression and return the result
string numToString(int, int); // Returns the description of a digit
string opToString(char); // Returns the description of an operator
int main()
{
int digit1,digit2;
char op;
welcome();
promptUser(digit1,op, digit2);//need to return 2 operands and 1 operator
calculate(digit1,op,digit2);//use the 2 operands, and the operator from prompt User to calculate
numToString(digit1,digit2);//change the numbers from integers to string(1 to one)
opToString(op); //change the operator from ( + to plus)
system("pause");
}
void welcome()
{
// Welcome message
cout << " ------------------------------------------------ \n\n"
<< " Welcome to Ali's English Language Calculator\n"
<< " ------------------------------------------------- \n\n";
}
void promptUser(int& digit1, char& op, int& digit2)
{
cout<<"Please enter the simple expressions you would like "<<endl;
cout<<"calculated in the following format : "<<endl;
cout<<endl;
cout<<" ____________"<<endl;
cout<<" | d1 op d2 |"<<endl;
cout<<" ~~~~~~~~~~~~~~"<<endl;
cout<<endl;
cout<<"Where d1 and d2 are single digits from 0 to 9, and "<<endl;
cout<<"op is one the following operators: +, -, *, /, and ^ (power)."<<endl;
cout<<endl;
cout<<"Please enter your expression here: ";
cin>>digit1>>op>>digit2;
//Looking for digits bigger than 9, since this is a single digit calculator
if (digit1>9||digit2>9)
{
cout<<"Invalid Input!!!"<<endl;
cout<<"You entered a digit bigger than 9. This is a single digit "<<endl;
cout<<"calculator."<<endl;
}
//First it looks for a division, then it looks if the 2nd digit is a 0
else if (op=='/'&&digit2==0)
{
cout<<"Invalid Input!!!"<<endl;
cout<<"A number cannot be divided by 0"<<endl;
}
else
{
cout<<digit1<<op<<digit2;
}
}
int calculate(int digit1, char op, int digit2)
{
double answer;
switch (op)
{
case '+':answer=digit1+digit2;
break;
case '-':answer=digit1-digit2;
break;
case '*':answer=digit1*digit2;
break;
case '/':answer=((float)digit1/(float)digit2);
break;
case '^':answer=pow((double)digit1, (double)digit2);
break;
default:cout<<"Invalid Operator!"<<endl;
break;
}//END SWITCH OP
return answer;
}
string numToString(int digit1, int digit2)
{
string digEn1,digEn2;
switch (digit1)
{
case 0:digEn1="Zero";
break;
case 1:digEn1="One";
break;
case 2:digEn1="Two";
break;
case 3:digEn1="Three";
break;
case 4:digEn1="Four";
break;
case 5:digEn1="Five";
break;
case 6:digEn1="Six";
break;
case 7:digEn1="Seven";
break;
case 8:digEn1="Eight";
break;
case 9:digEn1="Nine";
break;
default:cout<<"Invalid Input!!!"<<endl;
}//End switch digit1
//same as above, will translate each number to english
switch (digit2)
{
case 0:digEn2="Zero";
break;
case 1:digEn2="One";
break;
case 2:digEn2="Two";
break;
case 3:digEn2="Three";
break;
case 4:digEn2="Four";
break;
case 5:digEn2="Five";
break;
case 6:digEn2="Six";
break;
case 7:digEn2="Seven";
break;
case 8:digEn2="Eight";
break;
case 9:digEn2="Nine";
break;
default:
cout<<"Invalid INPUT"<<endl;
}return 0;
}
string opToString(char op)
{
string opStr;
switch(op)
{
case '+': opStr = " plus ";
break;
case '-': opStr = " minus ";
break;
case '*': opStr = " multiplied by ";
break;
case '/': opStr = " divided by ";
break;
default: opStr = " to the power ";
}
return 0;
}
|