用c++实现的简易计算器

大学项目作业,用了类和选择希望各位大佬可以指点一下。

#include
#include <math.h>
using namespace std;
int n;
class computer //加减乘除
{ public:
int op1, op2;
char ch;
float jisuan();
};
float computer::jisuan() //计算
{ switch (ch)
{ case ‘+’: return(float)(op1 + op2); break;
case ‘-’: return(float)(op1 - op2);; break;
case ‘*’: return(float)(op1 * op2); break;
case ‘/’: if (op2==0)
{cout << “分母不能为零!” << endl; }
return(float)(op1 / op2);
default:
cout << “格式错误!”;
break; }
}float pin(float c[])
{ int m; float max=0;
for ( m = 0; m < n; m++)
{ max += c[m]; }
return (max / n);
}
int jie(int l)
{ int jj,mm=1;
for(jj = 1; jj< l;jj++)
{ mm = mm * (jj + 1); }
return mm;
}
float sj(float tb)
{ float ct;
if (tb<360)
{ ct= cos((tb/180.0)M_PI); }
return ct;
}
int main()
{ computer a[100];
float b[100];
int i,x,c,ti;
cout << "\t\t\t\t\t
****************************" <<endl;
cout << “\t\t\t\t\t** 算式前先输入算数个数 " << endl;
cout << "\t\t\t\t\t
本计算器可进行以下计算 " << endl;
cout << "\t\t\t\t\t
1.进行两位数的加减乘除 " << endl;
cout << "\t\t\t\t\t
2.进行平均值的计算 " << endl;
cout << "\t\t\t\t\t
3.求n的阶乘 " << endl;
cout << "\t\t\t\t\t
4.求三角函数cos的值 " << endl;
cout <<"\t\t\t\t\t
***************************” << endl; lle:
cout << “\t\t\t\t\t请选择计算种类:” << endl;
cout << “\t\t\t\t\t”; cin >> x;
switch (x) {
case 1:
cout << “\t\t\t\t\t请输入计算式个数:” << endl;
cout << “\t\t\t\t\t”; cin >> n;
cout << “\t\t\t\t\t开始输入算式:” << endl;
cout << “\t\t\t\t\t”;
for (i = 0; i < n; i++)
{ cin >> a[i].op1 >> a[i].ch >> a[i].op2; }
cout << “\t\t\t\t\t”;
for (i = 0; i < n; i++)
{
cout << a[i].op1 << a[i].ch << a[i].op2 << “=”<<a[i].jisuan() << endl; }
break;
case 2:
cout << “\t\t\t\t\t请输入有多少个数:” << endl;
cout << “\t\t\t\t\t”; cin >> n;
cout << “\t\t\t\t\t请开始输入:” << endl;
cout << “\t\t\t\t\t”;
for ( i = 0; i < n; i++)
{ cin >> b[i]; }
pin(b);
cout << “\t\t\t\t\t平均值=” << pin(b) << endl;
break;
case 3:
cout << “\t\t\t\t\t请输入阶乘值n:” << endl;
cout << “\t\t\t\t\t”; cin >> c;
cout <<"\t\t\t\t\t" <<c << “的阶乘为:” << endl;
cout << “\t\t\t\t\t”; cout << jie©<< endl;
break;
case 4:
cout <<"\t\t\t\t\t请选择要求的所求度数(要求小于360度):"<<endl;
cout << “\t\t\t\t\t”; cin>>ti;
cout <<"\t\t\t\t\t该三角函数的值为:"<<endl;
cout << “\t\t\t\t\t”;cout <<sj(ti)<<endl;
break;
default:
cout << “\t\t\t\t\t请输入正确的运算节点” <<endl;
break;
}
goto lle;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闹腾的鑫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值