Better integer calculator Allow as many operations (still just +, -, * and / but
ID: 3669757 • Letter: B
Question
Better integer calculator
Allow as many operations (still just
+, -, * and / but no parenthesis) until a '#' character is reached instead of an operation. Ignore order of
operations as well, and always assume the left operations are done first. For eample: 2+3/5 = 5/5 = 1.
You should then show the result (you do not need to show the original equation). As this is an integer
calculator you may assume they enter only integers and valid operations (or the stopping character).
Example 1 (user input is underlined):
Enter an equation: 2+3*2#
10
Example 2 (user input is underlined):
Enter an equation: 2+3*2/5000+9-4#
5
Example 3 (user input is underlined):
Enter an equation: 2/3#
0
Example 4 (user input is underlined):
Enter an equation: 7#
7
Also
You can also attempt it to detect an error if you enter just a '#'.
Example 5 (user input is underlined):
Enter an equation: #
Error
AND this is my code down below
But I don't know how to revise it to fit the question
Please help me
thx~
#include
using namespace std;
int main()
{
int integer1,interger2;
char operator1;
int a;
cout << "Enter the equation= ";
cin >> integer1 >>operator1>> interger2;
if(operator1=='+')
{
a=(integer1 + interger2);
cout < }
if(operator1=='-')
{
a=(integer1 - interger2);
cout < }
if(operator1=='*')
{
a=(integer1*interger2);
cout <
}
else if(operator1=='/')
{
a=(integer1/interger2);
cout <
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int integer1,integer2;
char operator1;
int a;
char eq[20];
cout << "Enter the equation= ";
cin>>eq;
int i=1;
int flag=0;
//cin >> integer1 >>operator1>> interger2;
if(eq[0]=='#')
{
cout<<"There is no equation";
flag=1;
}
else
{
integer1=eq[0]-48;
}
while(eq[i]!='#' && flag==0)
{
operator1=eq[i];
integer2=eq[i+1]-48;
i+=2;
if(operator1=='+')
{
integer1=(integer1 + integer2);
}
else if(operator1=='-')
{
integer1=(integer1 - integer2);
}
else if(operator1=='*')
{
integer1=(integer1*integer2);
}
else if(operator1=='/')
{
integer1=(integer1/integer2);
}
}
if(flag==0)
cout<<"Result : "<<integer1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.