#include<iostream>
#include<iomanip>
using namespace std;
class fushu
{
public:
fushu(double x = 1, double y = 1);
fushu(fushu &p);
~fushu();
double geta(void);
double getb(void);
fushu& add(fushu& c);
fushu& sub(fushu& c);
fushu& mul(fushu& c);
fushu& div(fushu& c);
private:
double a;
double b;
};
fushu::fushu(double x, double y)
{
b = y;
a = x;
}
fushu::fushu(fushu& p)
{
a = p.a;
b = p.a;
}
fushu::~fushu()
{
}
double fushu::geta(void)
{
return(a);
}
double fushu::getb(void)
{
return(b);
}
fushu& fushu::add(fushu& c)
{
fushu temp;
temp.a = a + c.a;
temp.b = b + c.b;
return(temp);
}
fushu& fushu::sub(fushu& c)
{
fushu temp;
temp.a = a - c.a;
temp.b = b - c.b;
return(temp);
}
fushu& fushu::mul(fushu& c)
{
fushu temp;
temp.a = a * c.a - b * c.b;
temp.b = a*c.b+b*c.a;
return(temp);
}
fushu& fushu::div(fushu& c)
{
fushu temp;
temp.a = (a * c.a + b * c.b) / (c.a * c.a + c.b * c.b);
temp.b = (b * c.a - a * c.b) / (c.a * c.a + c.b * c.b);
return(temp);
}
int main()
{
void set(fushu &c);
int choice;
while (1)
{
cout << "*欢迎使用复数运算操作小程序*" << endl;
cout << "* 1.加法 *" << endl;
cout << "* 2.减法 *" << endl;
cout << "* 3.乘法 *" << endl;
cout << "* 4.除法 *" << endl;
cout << "* 请选择你的操作: *" << endl;
cin>>choice;
double p = 0, q = 0, r = 0, s = 0;
cout << "输入第一个复数的实部和虚部" << endl;
cin >> p >> q;
cout << "输入第二个复数的实部和虚部" << endl;
cin >> r >> s;
fushu x(p, q),y(r, s), z;
cout << p << " + " << q << "i " << r << " + " << s << "i" << endl;
switch (choice)
{
case 1:z = x.add(y);
cout << "加法:" << endl;
set(z);
system("pause");
break;
case 2:z = x.sub(y);
cout << "减法:" << endl;
set(z);
system("pause");
break;
case 3: z = x.mul(y);
cout << "乘法:" << endl;
set(z);
system("pause");
break;
case 4: z = x.div(y);
cout << "除法:" << endl;
set(z);
system("pause");
break;
return(0);
}
system("cls");
}
}
void set(fushu& c)
{
if (c.getb() == 0)
cout << "结果是" << c.geta() << endl;
else if (c.geta() == 0)
cout << "结果是" << c.getb() << "i" << endl;
else if(c.getb()>0)
cout << "结果是" << c.geta() << "+" << c.getb() << "i" << endl;
else
cout << "结果是" << c.geta() << c.getb() << "i" << endl;
}
12-10
827
08-04
1万+
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交