Build a simple “English language” calculator that does the following: - Takes th
ID: 3623724 • Letter: B
Question
Build a simple “English language” calculator that does the following:- Takes three inputs from the keyboard, two of them single digits (0 to 9)
- Takes a char from the keyboard, representing one of the following five operations from the
keyboard: + (addition), - (subtraction), * (multiplication), / (division), and ^ (exponentiation).
- Outputs the description of the operation in plain English, as well as the numeric result
The operations should be translated into English as follows:
+ plus / divided by
- minus
* multiplied by ^ to the power
The switch…case statement might be helpful when translating the input values and operators into
words.
You need to consider three special situations:
1) one or two of the numbers is not a valid digit; output an error message.
2) the “operator” is not one of the allowed five operators; in that case output a message saying that the
operator is not a valid one.
3)for division there is a special constraint ; you cannot divide by 0, and you should therefore test
whether the second number is 0. If it is 0, you should output a message saying that division by zero is
not allowed and end your program.
Explanation / Answer
please rate - thanks
you didn't say if the digits should be input as integers or characters. I did it as integers
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
char oper;
int var1 = 0, var2 = 0;
cout<<"enter number 1 operation number 2 (ex 1 + 2) -99 to exit ";
cin>>var1;
while(var1!=-99)
{cin>>oper>>var2;
if(var1<0||var1>9||var2<0||var2>9)
cout<<"number must be 1 digit ";
else
{
switch(oper)
{case '+':
cout<<var1<<" plus "<<var2<<" = "<<var1+var2;
break;
case '-':cout<<var1<<" minus "<<var2<<" = "<<var1-var2;
break;
case '*':cout<<var1<<" multiplied by "<<var2<<" = "<<var1*var2;
break;
case '/':if(var2==0)
cout<<var1<<" divided by "<<var2<<" is "<<"undefined";
else
cout<<var1<<" divided by "<<var2<<" = "<<(double)var1/var2;
break;
case '^': cout<<var1<<" to the power "<<var2<<" = "<<pow((double)var1,var2);
break;
default: cout<<"Invalid operator";
}
cout<<endl;
}
cout<<"enter number 1 operation number 2 (ex 1 + 2) -99 to exit ";
cin>>var1;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.