


类与对象题不要用太高级句子(最好多些注释) 必须按照他的提示填写 最好附上运行结果 只要测试后没问题 秒通过!
#include <iostream>
#include <cstdio>
using namespace std;
class Complex
{
private:
float real; //实部
float imag; //虚部
public:
Complex() //无参构造函数
{
}
Complex(float real,float imag) //有参构造函数
{
this->real = real;
this->imag = imag;
}
~Complex() //析构函数
{
}
float getReal() //获取实部
{
return this->real;
}
float getImag() //获取虚部
{
return this->imag;
}
Complex add(const Complex& c) //加法
{
this->real += c.real;
this->imag += c.imag;
return *this;
}
Complex sub(const Complex& c) //减法
{
this->real -= c.real;
this->imag -= c.imag;
return *this;
}
};
int main()
{
float rx1,ix1,rx2,ix2;
char ch;
scanf("%f%f%f%f %c",&rx1,&ix1,&rx2,&ix2,&ch);
//定义复数类对象
Complex c1(rx1,ix1);
Complex c2(rx2,ix2);
if(ch == 'a') //复数加法
{
c1.add(c2);
printf("%.1f+%.1fi\n",c1.getReal(),c1.getImag());
}
else //复数减法
{
c1.sub(c2);
printf("%.1f+%.1fi\n",c1.getReal(),c1.getImag());
}
return 0;
}
