Tuesday, 23 August 2016

PR: 03 Write a program to create four-function calculator using C++



Write a program to create four-function calculator using C++. The Program should ask the user to enter a number, an operator, and another number.(use floating point.) It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result. When it finishes the calculation, the program should ask whether the user wants to do another calculation. The response can be _y’ or _n’. 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)?y


Program: 
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
class calc
{
float a,b,result;
char op;
public:
void operation()
{
cout<<"\n Enter expression(First number operator second number):";
cin>a>>op>>b;
switch(op)
{
case'+':
result=a+b:
cout<<"\n Result: "<<result;
break;
case'-':
result=a-b;
cout<<"\n Result: "<<result;
break;
case'*':
result=a*b;
cout<<"\n Result: "<<result;
break;
case'/':
result=a/b;
cout<<"\n Result: "<<result;
break;
}
}
};
int main()
{
calc c;
char ch;
 cout<<"\n  calculator";
do
{
c.operation();
cout<<"\n Do you want to perform another operation (y/n): ";
cin>>ch;
}while(ch=='y'|ch=='Y');
return 0;
}

 

No comments:

Post a Comment