C++编程定义一个复数类

2024-12-26 19:40:47
推荐回答(1个)
回答1:

#include
using namespace std;
struct math{double m;double n;};
int main(){
math a,b,c;
char d;
cout<<"请输入第一个复数的实部和虚部"< cin>>a.m>>a.n;
cout<<"请输入第二个复数的实部和虚部"< cin>>b.m>>b.n;
cout<<"请选择运算法则(+,-,*,/)"< cin>>d;
while(d!='+'&&d!='-'&&d!='*'&&d!='/')
{cout<<"输入错误!!!请重新输入"< cin>>d;
}
if(d=='+'){c.m=a.m+b.m; c.n=a.n+b.n;}
if(d=='-'){c.m=a.m-b.m;c.n=a.n-b.n;}
if(d=='*'){c.m=a.m*a.n-b.m*b.n;c.n=a.m*b.n+a.n*b.m;}
if(d=='/'){c.m=(a.m*b.m+a.n*b.n)/(b.m*b.m+b.n*b.n);
c.n=(a.n*b.m+a.m*b.n)/(b.m*b.m+b.n*b.n);}
cout<<"计算结果="<}