In this homework, you will write a C++ program that works as a simple calculator
ID: 3732052 • Letter: I
Question
In this homework, you will write a C++ program that works as a simple calculator that performs only addition, subtraction, multiplication and division operations with many operands. Your program will ask the user to enter the expression that they want to be calculated. After receiving the input, the program will perform the necessary input checks. If there is an error in the expression, an appropriate message will be displayed and the user will be asked to enter another expression. If there are no errors in the input expression, then the calculated result will be displayed and the program will ask for another expression until the user enters a termination command. Termination commands are “QUIT”, “quit”, “EXIT”, “exit”, “END” and “end”. Suggest you to use ATOI function Below there is sample runs. Should be exactly same. Sample Run 2 Please enter an expression to calculate: 10/ Invalid entry. Please enter an expression to calculate: 1/2 0.5 Please enter an expression to calculate: 11-151 -140 Please enter an expression to calculate: EXIT GOOD BYE Press any key to continue . Sample Run 3 Please enter an expression to calculate: 1678+2508 4186 Please enter an expression to calculate: 56/-9 100/6*4 414.815 Please enter an expression to calculate: Invalid entry. Please enter an expression to calculate: 45*-1/9 Please enter an expression to calculate: 45*-1/-9 Please enter an expression to calculate: 45 +1/9 Please enter an expression to calculate:Explanation / Answer
Hi.. I have written cpp code for the above. Please check below.
Main.cpp
#include <iostream>
using namespace std;
bool isOperand(char c) { return (c >= '0' && c <= '9'); }
int value(char c) { return (c - '0'); }
int evaluate(char *exp)
{
if (*exp == '') return -1;
float res = value(exp[0]);
for (int i = 1; exp[i]; i += 2)
{
char opr = exp[i], opd = exp[i+1];
if (!isOperand(opd)) return -1;
if (opr == '+') res += value(opd);
else if (opr == '-') res -= value(opd);
else if (opr == '*') res *= value(opd);
else if (opr == '/') res /= value(opd);
else return -1;
}
cout << res << endl;
return res;
}
int main()
{
bool flag = true;
while(flag){
cout << "Please enter an expression to calculate: ";
string expr;
cin >> expr;
if(expr == "exit" || expr == "EXIT" || expr == "QUIT" || expr == "quit" || expr=="END" || expr=="end"){
flag = false;
cout << "Good Bye!!!! ";
}else{
char expr1[20];
for(int i=0;i<sizeof(expr);i++){
if(expr[i]=='')
break;
expr1[i] = expr[i];
}
int res = evaluate(expr1);
if(res == -1){
cout << "Invalid entry ";
}
/*(res == -1)? cout << "Invalid entry ":
cout << res << endl;*/
}
}
return 0;
}
Output:
Please enter an expression to calculate:
10/
Invalid entry
Please enter an expression to calculate:
1/2
0.5
Please enter an expression to calculate:
end
Good Bye!!!!
Please check the above and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.