/*
Calculator in C++. Takes 3 inputs same as we write on our copy (in expression form) using Switch Statement.
*/
#include <iostream>
using namespace std;
void main()
{
double x,y,z; char o,O;
cout << "How to write: 2-5/2" << endl;
cout << "Write expression consisting of 3 terms: ";
cin>>x>>o>>y>>O>>z;
cout << "Result: ";
switch(o)
{
case '+':
switch(O)
{
case '+':
cout <<x+y+z;
break;
case '-':
cout <<x+y-z;
break;
case '*':
cout <<x+y*z;
break;
case '/':
cout <<x+y/z;
break;
default:
cout << "Error in the last two terms";
}
break;
case '-':
switch(O)
{
case '+':
cout <<x-y+z;
break;
case '-':
cout <<x-y-z;
break;
case '*':
cout <<x-y*z;
break;
case '/':
cout <<x-y/z;
break;
default:
cout << "Error in the last two terms";
}
break;
case '*':
switch(O)
{
case '+':
cout <<x*y+z;
break;
case '-':
cout <<x*y-z;
break;
case '*':
cout <<x*y*z;
break;
case '/':
cout <<x*y/z;
break;
default:
cout << "Error in the last two terms";
}
break;
case '/':
switch(O)
{
case '+':
cout <<x/y+z;
break;
case '-':
cout <<x/y-z;
break;
case '*':
cout <<x/y*z;
break;
case '/':
cout <<x/y/z;
break;
default:
cout << "Error in the last two terms";
}
break;
default:
cout <<"Syntax Error in 1st two terms.";
}
cout << "." << endl;
}
No comments:
Post a Comment