运算符重载的问题 求解答……
题目要求如下:【问题描述】设计复数类,利用友元实现两个复数的相加运算。【输入形式】1+2i,3+4i
【输出形式】4+6i
【样例输入】1+2i,3+4i
【样例输出】4+6i
我的代码如下:
程序代码:
#include<iostream.h> class Complex { double real; double image; public: Complex(double r=0,double i=0) { real=r; image=i; } friend void inputcomplex(Complex &comp); friend Complex addcomplex(Complex c1,Complex c2); friend void outputcomplex(Complex comp); }; void inputcomplex(Complex &comp) { cin>>comp.real>>comp.image; } Complex addcomplex(Complex c1,Complex c2) { Complex c; c.real=c1.real+c2.real; c.image=c1.image+c2.image; return c; } void outputcomplex(Complex comp) { cout<<"("<<comp.real<<","<<comp.image<<")"; } void main() { Complex c1,c2,result; inputcomplex(c1); inputcomplex(c2); result=addcomplex(c1,c2); outputcomplex(c1); cout<<"+"; outputcomplex(c2); cout<<"="; outputcomplex(result); cout<<endl; }
我该怎么让我的输入变成1+2i,3+4i 然后换行就变成4+6i这个结果?