#include <iostream>
#include <cmath>
using namespace std;
class calc
{
private:
int a,b;
int p,m;
public:
calc(){}
void input();
void input1();
void input2();
void input3();
void input4();
void input5();
double input6();
int jia();
int jian();
int mul();
int div();
//long pow(); //平方
void display1(); //加
void display2(); //减
void display_mul();
void display_div();
int display_pow(int);
};
//以下是对成员函数的定义
int calc::jia()
{
//int sum;
//sum=a+b;
//return sum;
return a+b;
}
int calc::jian()
{
return a-b;
}
int calc::mul()
{
return a*b;
}
int calc::div()
{
if(b==0)
{
cout<<"除数为零!请重新输入..."<<endl;
cout<<"请输入两个整数:";
cin>>a>>b;
return 1;
}
else
return a/b;
}
//long calc::pow()
//{
// return p*p;
//}
void calc::input()
{
cout<<"请输入两个整数:";
cin>>a>>b;
}
void calc::input1()
{
input();
jia();
}
void calc::input2()
{
input();
jian();
}
void calc::input3()
{
input();
mul();
}
void calc::input4()
{
input();
div();
}
/*
void calc::input5()
{
input();
pow();
}
*/
double calc::input6()
{
cout<<"请输入一个整数,求该数的平方根:";
cin>>m;
double q=sqrt(m);
return q;
}
void calc::display1()
{
cout<<"两数之和="<<jia()<<endl;
}
void calc::display2()
{
cout<<"两数之差="<<jian()<<endl;
}
void calc::display_mul()
{
cout<<"两数之积="<<mul()<<endl;
}
void calc::display_div()
{
cout<<"两数之商="<<div()<<endl;
}
int calc::display_pow(int q)
{
return q*q;
}
//菜单
void menu()
{
cout<<"*********************************"<<endl;
cout<<endl;
cout<<" ===简单计算器==="<<endl;
cout<<endl;
cout<<" ---1.加---"<<endl;
cout<<endl;
cout<<" ---2.减---"<<endl;
cout<<endl;
cout<<" ---3.乘---"<<endl;
cout<<endl;
cout<<" ---4.除---"<<endl;
cout<<endl;
cout<<" ---5.平方---"<<endl;
cout<<endl;
cout<<" ---6.平方根---"<<endl;
cout<<endl;
cout<<" ---0.退出---"<<endl;
cout<<endl;
cout<<"*********************************"<<endl;
}
//退出菜单
void _exit()
{
cout<<"*********************************"<<endl;
cout<<endl;
cout<<" 欢迎使用本系统"<<endl;
cout<<endl;
cout<<endl;
cout<<" ---再见---"<<endl;
cout<<endl;
cout<<"*********************************"<<endl;
}
////////////主函数/////////////
int main()
{
calc add;
calc sub;
calc mul;
calc div;
calc pow;
calc sqrt;
int c;
menu(); //调用主菜单
while(1)
{
cout<<"请选择:";
cin>>c;
switch(c)
{
case 1:
add.input1();
add.display1();
break;
case 2:
sub.input2();
sub.display2();
break;
case 3:
mul.input3();
mul.display_mul();
break;
case 4:
div.input4();
div.display_div();
break;
case 5:
//pow.input5();
int p;
cout<<"请输入一个正整数:";
cin>>p;
cout<<"该数的平方是:"<<pow.display_pow(p)<<endl;
break;
case 6:
cout<<"该数的平方根为:"<<sqrt.input6()<<endl;
break;
case 0:
system("cls");
_exit();
exit(1);
break;
default:
cout<<"输入错误!"<<endl;
break;
}
}
return 1;
}
原文地址:https://www.cnblogs.com/duanqibo/p/11111044.html