复数计算器 在编译时没有错误,可是答案错了
#include<iostream>using namespace std;
class Complex
{
private:
int a,b;
public:
Complex(int a1,int b1=0):a(a1),b(b1){};
~Complex(){};
void jiaf(const Complex ©)
{
a+=copy.a;
b+=copy.b;
}
void jianf(const Complex ©)
{
a-=copy.a;
b-=copy.b;
}
void chengf(const Complex ©)
{
int c=a;
a=a*copy.a-b*copy.b;
b=c*copy.b+b*copy.a;
}
void chuf(const Complex ©)
{
int d;
b=-b;
d=-copy.b;
int c=a;
a=a*copy.a-b*d;
b=c*d+b*copy.a;
}
void Show()
{
if(b>0)
cout<<a<<"+"<<b<<endl;
else if(b==0)
{
cout<<a<<endl;
}
else
cout<<a<<b<<endl;
}
};
int main()
{
void Show1();
char c;
do
{
cout<<"输入一个复数:"<<endl;
int a1,b1;
cin>>a1>>b1;
Complex p1(a1,b1);
cout<<"输入一个复数:"<<endl;
int a2,b2;
cin>>a2>>b2;
Complex p2(a2,b2);
Show1();
int a;
cin>>a;
if(a==5)
{
exit(1);
}
while(a!=1&&a!=2&&a!=3&&a!=4&&a!=4)
{
Show1();
cout<<"重新输入a"<<endl;
cin>>a;
}
switch(a)
{
case 1:
p1.jiaf(p2);
case 2:
p1.jianf(p2);
case 3:
p1.chengf(p2);
case 4:
p1.chuf(p2);
}
cout<<"结果是:"<<endl;
p1.Show();
cout<<"是否继续?(Y/N)"<<endl;
cin>>c;
}while(c=='y'||c=='Y');
}
void Show1()
{
cout<<"进行复数的运算 "<<endl;
cout<<"1 + "<<endl;
cout<<"2 -"<<endl;
cout<<"3 * "<<endl;
cout<<"4 /"<<endl;
cout<<"5 退出"<<endl;
cout<<"输入数字编码"<<endl;
}