主题:一个简单的计算器!
#include<math.h>
#include<stdlib.h>
#include<iostream.h>
class Calculator
{
double a,b;
public:
Calculator(){a=0;b=0;}; //could be omitted
void newa()
{
double num;
cout<<"Input the number:";
cin>>num;
a=num;
}
void newab()
{
double num1,num2;
cout<<"Input the numbers.the first number:";
cin>>num1;
cout<<"Input the senond number:";
cin>>num2;
a=num1;
b=num2;
}
double Geta(){return a;}
double Getb(){return b;}
double Add(Calculator &A);
double Sub(Calculator *A);
double Mul(Calculator &A);
double Div(Calculator &A);
double Sin(Calculator &A);
double Cos(Calculator &A);
double Tan(Calculator &A);
double Exp(Calculator &A);
double Fabs(Calculator &A);
double LogE(Calculator &A);
double Log10(Calculator &A);
double Pow(double x,double y);
double Sqrt(double x);
};
double Calculator::Add(Calculator &A)
{
return A.a+A.b;
}
double Calculator::Sub(Calculator *A)
{
return A->a-A->b;
}
double Calculator::Mul(Calculator &A)
{
return A.a*A.b;
}
double Calculator::Div(Calculator &A)
{
if(A.b==0)
{cout<<"Error! The program will be terminated!"<<endl;exit(0);}
return A.a/A.b;
}
double Calculator::Sin(Calculator &A)
{
return sin(A.a);
}
double Calculator::Cos(Calculator &A)
{
return cos(A.a);
}
double Calculator::Tan(Calculator &A)
{
return tan(A.a);
}
double Calculator::Exp(Calculator &A)
{
return exp(A.a);
}
double Calculator::Fabs(Calculator &A)
{
return fabs(A.a);
}
double Calculator::LogE(Calculator &A)
{
return log(A.a);
}
double Calculator::Log10(Calculator &A)
{
return log10(A.a);
}
double Calculator::Pow(double x,double y)
{
return pow(x,y);
}
double Calculator::Sqrt(double x)
{
if(x<0)
{cout<<"Error! The program will be terminated!"<<endl;exit(0);}
return sqrt(x);
}
void main()
{
int sel;
Calculator cal;
cout<<"Welcome to use the calculator!please select."<<endl;
cout<<"1:\'+\' 2:\'-\' 3:\'*\' 4:\'/\' 5:\'sin\' 6:\'cos\' 7:\'tan\'"<<endl;
cout<<"8:\'exp\' 9:\'fabs\' 10:\'logE\' 11:\'log10\' 12:\'pow\' 13:\'sqrt\'"<<endl;
do
{
cout<<"Please select:(0--exit)";
cin>>sel;
switch (sel)
{
case 0: break;
case 1: cal.newab();
cout<<"The result is:"<<cal.Add(cal)<<endl;
break;
case 2: cal.newab();
cout<<"The result is:"<<cal.Sub(&cal)<<endl;
break;
case 3: cal.newab();
cout<<"The result is:"<<cal.Mul(cal)<<endl;
break;
case 4: cal.newab();
cout<<"The result is:"<<cal.Div(cal)<<endl;
break;
case 5: cal.newa();
cout<<"The result is:"<<cal.Sin(cal)<<endl;
break;
case 6: cal.newa();
cout<<"The result is:"<<cal.Cos(cal)<<endl;
break;
case 7: cal.newa();
cout<<"The result is:"<<cal.Tan(cal)<<endl;
break;
case 8: cal.newa();
cout<<"The result is:"<<cal.Exp(cal)<<endl;
break;
case 9: cal.newa();
cout<<"The result is:"<<cal.Fabs(cal)<<endl;
break;
case 10: cal.newa();
cout<<"The result is:"<<cal.LogE(cal)<<endl;
break;
case 11: cal.newa();
cout<<"The result is:"<<cal.Log10(cal)<<endl;
break;
case 12: cal.newab();
cout<<"The result is:"<<cal.Pow(cal.Geta(),cal.Getb())<<endl;
break;
case 13: cal.newa();
cout<<"The result is:"<<cal.Sqrt(cal.Geta())<<endl;
break;
default: cout<<"Your selection is ERROR! Select again!"<<endl;
}
}while(sel!=0);
}
本人水平有限!有错误请指正!谢谢
#include<stdlib.h>
#include<iostream.h>
class Calculator
{
double a,b;
public:
Calculator(){a=0;b=0;}; //could be omitted
void newa()
{
double num;
cout<<"Input the number:";
cin>>num;
a=num;
}
void newab()
{
double num1,num2;
cout<<"Input the numbers.the first number:";
cin>>num1;
cout<<"Input the senond number:";
cin>>num2;
a=num1;
b=num2;
}
double Geta(){return a;}
double Getb(){return b;}
double Add(Calculator &A);
double Sub(Calculator *A);
double Mul(Calculator &A);
double Div(Calculator &A);
double Sin(Calculator &A);
double Cos(Calculator &A);
double Tan(Calculator &A);
double Exp(Calculator &A);
double Fabs(Calculator &A);
double LogE(Calculator &A);
double Log10(Calculator &A);
double Pow(double x,double y);
double Sqrt(double x);
};
double Calculator::Add(Calculator &A)
{
return A.a+A.b;
}
double Calculator::Sub(Calculator *A)
{
return A->a-A->b;
}
double Calculator::Mul(Calculator &A)
{
return A.a*A.b;
}
double Calculator::Div(Calculator &A)
{
if(A.b==0)
{cout<<"Error! The program will be terminated!"<<endl;exit(0);}
return A.a/A.b;
}
double Calculator::Sin(Calculator &A)
{
return sin(A.a);
}
double Calculator::Cos(Calculator &A)
{
return cos(A.a);
}
double Calculator::Tan(Calculator &A)
{
return tan(A.a);
}
double Calculator::Exp(Calculator &A)
{
return exp(A.a);
}
double Calculator::Fabs(Calculator &A)
{
return fabs(A.a);
}
double Calculator::LogE(Calculator &A)
{
return log(A.a);
}
double Calculator::Log10(Calculator &A)
{
return log10(A.a);
}
double Calculator::Pow(double x,double y)
{
return pow(x,y);
}
double Calculator::Sqrt(double x)
{
if(x<0)
{cout<<"Error! The program will be terminated!"<<endl;exit(0);}
return sqrt(x);
}
void main()
{
int sel;
Calculator cal;
cout<<"Welcome to use the calculator!please select."<<endl;
cout<<"1:\'+\' 2:\'-\' 3:\'*\' 4:\'/\' 5:\'sin\' 6:\'cos\' 7:\'tan\'"<<endl;
cout<<"8:\'exp\' 9:\'fabs\' 10:\'logE\' 11:\'log10\' 12:\'pow\' 13:\'sqrt\'"<<endl;
do
{
cout<<"Please select:(0--exit)";
cin>>sel;
switch (sel)
{
case 0: break;
case 1: cal.newab();
cout<<"The result is:"<<cal.Add(cal)<<endl;
break;
case 2: cal.newab();
cout<<"The result is:"<<cal.Sub(&cal)<<endl;
break;
case 3: cal.newab();
cout<<"The result is:"<<cal.Mul(cal)<<endl;
break;
case 4: cal.newab();
cout<<"The result is:"<<cal.Div(cal)<<endl;
break;
case 5: cal.newa();
cout<<"The result is:"<<cal.Sin(cal)<<endl;
break;
case 6: cal.newa();
cout<<"The result is:"<<cal.Cos(cal)<<endl;
break;
case 7: cal.newa();
cout<<"The result is:"<<cal.Tan(cal)<<endl;
break;
case 8: cal.newa();
cout<<"The result is:"<<cal.Exp(cal)<<endl;
break;
case 9: cal.newa();
cout<<"The result is:"<<cal.Fabs(cal)<<endl;
break;
case 10: cal.newa();
cout<<"The result is:"<<cal.LogE(cal)<<endl;
break;
case 11: cal.newa();
cout<<"The result is:"<<cal.Log10(cal)<<endl;
break;
case 12: cal.newab();
cout<<"The result is:"<<cal.Pow(cal.Geta(),cal.Getb())<<endl;
break;
case 13: cal.newa();
cout<<"The result is:"<<cal.Sqrt(cal.Geta())<<endl;
break;
default: cout<<"Your selection is ERROR! Select again!"<<endl;
}
}while(sel!=0);
}
本人水平有限!有错误请指正!谢谢