C++ Program : To Make a simple Caculator




#include<iostream.h>
#include<conio.h>
class calculator
{
   float a,b,ans;
   char op;
   float cal();
   public:
   void mobcal();
}ob;
float calculator::cal()
{
   switch(op)
   {
      case '+':ans=a+b;break;
      case '-':ans=a-b;break;
      case '*':ans=a*b;break;
      case '/':ans=a/b;break;
      default:cout<<"\t"<<op<<" is not a operator(+,-,*,/)";
   }
   return ans;
}
void calculator::mobcal()
{
   cout<<"\tSimple Calculator\nEnter 1st number\t:";
   cin>>a;
   cout<<"Operator and 2nd number\t:";
   op=getche();
   do{
      cin>>b;
      a=cal();
      cout<<"\n\t="<<a;
      op=getche();
   }while(op!='\r'&& op!=' ');
}

int main()
{
   clrscr();
   ob.mobcal();
   cout<<"\nPress any key to exit";
   getch();
   return 0;
}

Class diagram 

(click image to view large)

 

 

Related Posts:

0 comments:

Post a Comment