题目:一个复数类,运算符重载 + ,实现复数和复数的相加。输入一组复数,每行一个复数,直到输入0结束。 输出这组复数的结果。
提示: 复数的输入和输出符合数学书写规范
输入示例
3+2i
2+3i
0
输出示例
5+5i
include<iostream>
using namespace std;
class Complex{
private:
int real;
int image;
public:
Complex(int a=0,int b=0):real(a),image(b){
};
void display(); //输出使用函数
void get(int ,int ); //设置私有成员大小
friend Complex operator+(const Complex &c1,const Complex &c2); //重载+运算符
};
Complex operator+(const Complex &c1,const Complex &c2){
Complex c3;
c3.real=(c1.real+c2.real); //重载+实现
c3.image=(c1.image+c2.image);
return c3;
}
void Complex::get(int a,int b){
real=a; //设置大小
image=b;
}
void Complex::display(){
//输出函数
if(real!=0&&image>0){
if(image!=1){
cout<<real<<"+"<<image<<"i"<<endl;
}else{
//注意虚数是1或者-1的时候不能直接输出虚数
cout<<real<<"+"<<"i"<<endl;
}
}else if(real!=0&&image<0){
if(image!=-1){
cout<<real<<image<<"i"<<endl;
}else{
cout<<real<<"-i"<<endl;
}
}else if(real!=0&&image==0){
cout<<real<<endl;
}else if(real==0&&image