用C++写一个简单计算器

包括加减乘除运算(输入为用户输入),输出为计算后结果

要求用户输入abc 求解两元一次方程的根

#include<iostream>
using namespace std;
float add(float a,float b)
{
return a+b;
}

float sub(float a,float b)
{
return a-b;
}

float mul(float a,float b)
{
return a*b;
}

float divi(float a,float b)
{
if(b==0) return 0x7fffffff;
else return a/b;
}

float (*menu[])(float a,float b) = {add,sub,mul,divi};

main()
{
float num1,num2;
int choice;
cout<<"请输入 两个 运算的数字"<<endl;
cin>>num1;
cin>>num2;
cout<<"请输入选择的功能:"<<endl;
cout<<"1 : add"<<endl;
cout<<"2 : sub"<<endl;
cout<<"3 : mul"<<endl;
cout<<"4 : divi"<<endl;
cin>>choice;
cout<<"the result is :"<<menu[choice-1](num1,num2)<<endl;
}

float a,b,c,x,x1,x2,t,m;
cout<<"imput the formula"<<endl;
cin>>a>>b>>c;
t=b*b-4*a*c;

if(t>=0)
{
m=sqrt(t);
x1=(-b+m)/2*a;
x2=(-b-m)/2*a;
if(m==0)
cout<<"x1=x2="<<x1<<endl;
else
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
}
else
{
m=sqrt(fabs(t));
x1=(-b+m)/2*a;
x2=(-b-m)/2*a;
if(x2>=0)
cout<<"the result is: "<<x1<<"+"<<x2<<"i"<<endl;
else
cout<<"the result is: "<<x1<<x2<<"i"<<endl;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答