C++编程定义一个复数类2
发布网友
发布时间:2023-10-20 22:41
我来回答
共1个回答
热心网友
时间:2024-12-05 07:03
#include<iostream>
using namespace std;
struct math{double m;double n;};
int main(){
math a,b,c;
char d;
cout<<"请输入第一个复数的实部和虚部"<<endl;
cin>>a.m>>a.n;
cout<<"请输入第二个复数的实部和虚部"<<endl;
cin>>b.m>>b.n;
cout<<"请选择运算法则(+,-,*,/)"<<endl;
cin>>d;
while(d!='+'&&d!='-'&&d!='*'&&d!='/')
{cout<<"输入错误!!!请重新输入"<<endl;
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<<"计算结果="<<c.m<<"+"<<c.n<<"i"<<endl;
}