Thursday 20 October 2016

calculator using Switch Case

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//developer:rushikesh sanjay palke
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

/*Write a C++ program create a calculator for an arithmetic operator (+,-,*,/).The program should take two operands from user and performs the operation on those two operands  depending upon the operator entered by user.
Use a switch statement to select the operation. Finally, display the result.
Some sample interaction with the program might look like this:
Enter first number, operator, second number: 10 / 3
Answer = 3.333333
Do another (y/n)? y
Enter first number, operator, second number: 12 + 100
Answer = 112
Do another (y/n)? n
*/

# include <iostream>
using namespace std;
class calci
{
char o;
float num1,num2;
public:
void calculator()
{
cout << "Enter first number, operator, second number: ";
cin >>num1>>o>>num2;
cout<<"Answer =";
switch(o) {
case '+':
cout << num1+num2<<endl;
break;
case '-':
cout << num1-num2<<endl;
break;
case '*':
cout << num1*num2<<endl;
break;
case '/':
cout << num1/num2<<endl;
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
cout << "Error! operator is not correct\n";
break;
}
}
};
int main()
{
calci o;
char ch;
do
{
cout<<">>>>>>>>CALCULATOR<<<<<<<<<\n";
o.calculator();
cout<<"do you want to run calculator again (y/n) ";
cin>>ch;
}while(ch=='y');
return 0;
}


/*output:

>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 10/3
Answer =3.33333
do you want to run calculator again (y/n) y
>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 10+2
Answer =12
do you want to run calculator again (y/n) y
>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 1-1
Answer =0
do you want to run calculator again (y/n) y
>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 4*1.2
Answer =4.8
do you want to run calculator again (y/n) y
>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 4&4
Answer =Error! operator is not correct
do you want to run calculator again (y/n) ^C

*/

No comments:

Post a Comment