主题:谁能帮忙看一下,用友元函数方法重载插入(<<)、提取(>>)运算符不成功
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
private:
double real,image; //分别为实部、虚部
public:
Complex(double rel=0, double img=0)
{
real=rel;
image=img;
}
void display() //输出(a+b*i) 、(a-b*i) 、(a)等形式
{
cout<<" ("<<real;
if (image>0)
cout<<"+"<<image<<"*i) ";//虚部为正
else if (image<0)
cout<<image<<"*i) "; //虚部为负
else
cout<<") "; //虚部为,即为实数
}
//用成员函数方法来重载+、*运算符
Complex operator +(Complex & c);
Complex operator *(Complex & c);
Complex operator -(Complex & c);
Complex operator /(Complex & c);
friend ostream & operator<<(ostream & output,Complex & t);//重载运算符<<
friend istream & operator>>(istream & input,Complex & t); //重载运算符>>
};
Complex::Complex(real,image)
{
real=t.real;
image=t.image;
}
Complex Complex::operator +(Complex & c)
{
Complex temp;
temp.real=real+c.real;
temp.image=image+c.image;
return temp;
}
Complex Complex::operator *(Complex & c)
{
Complex temp;
temp.real=real*c.real-image*c.image ;
temp.image=real*c.image+image*c.real;
return temp;
}
Complex Complex::operator -(Complex & c)
{
Complex temp;
temp.real=real-c.real;
temp.image=image-c.image;
return temp;
}
Complex Complex::operator /(Complex & c)
{
Complex temp;
double square=c.real*c.real+c.image*c.image;
if (fabs(square)<1e-6)
{
cout<<"除数为, 不能进行除法运算!";
return temp;
}
temp.real=(real*c.real+image*c.image)/square;
temp.image=(c.real*image-real*c.image)/square;
return temp;
}
ostream & operator<<(ostream & output,Complex & real,Complex &image)//重载运算符<<
{
output<<t.real<<"實部"<<t.image<<"虛部";
return output;
}
istream & operator>>(istream & input,Complex & real,Complex &image) //重载运算符>>
{
int real, image;
input>>real;
input.ignore(); //ignore()是istream类的成员函数
input>>image; //ignore()的作用:删除输入流中指定数目的字符
input.ignore();
t=Complex(real,image); //用输入内容来修改时间对象t的值
return input;
}
void main()
{
Complex c1(1,2), c2(3,-4),c3,c4,c5,c6;
c3=c1+c2;
c4=c1*c2;
c5=c1-c2;
c6=c1/c2;
//输出加的结果
c1.display();
cout<<"+";
c2.display();
cout<<"=";
c3.display();
cout<<endl;
//输出乘的结果
c1.display();
cout<<"*";
c2.display();
cout<<"=";
c4.display ();
cout<<endl;
//输出减的结果
c1.display();
cout<<"-";
c2.display();
cout<<"=";
c5.display();
cout<<endl;
//输出除的结果
c1.display();
cout<<"/";
c2.display();
cout<<"=";
c6.display();
cout<<endl;
}
#include <cmath>
using namespace std;
class Complex
{
private:
double real,image; //分别为实部、虚部
public:
Complex(double rel=0, double img=0)
{
real=rel;
image=img;
}
void display() //输出(a+b*i) 、(a-b*i) 、(a)等形式
{
cout<<" ("<<real;
if (image>0)
cout<<"+"<<image<<"*i) ";//虚部为正
else if (image<0)
cout<<image<<"*i) "; //虚部为负
else
cout<<") "; //虚部为,即为实数
}
//用成员函数方法来重载+、*运算符
Complex operator +(Complex & c);
Complex operator *(Complex & c);
Complex operator -(Complex & c);
Complex operator /(Complex & c);
friend ostream & operator<<(ostream & output,Complex & t);//重载运算符<<
friend istream & operator>>(istream & input,Complex & t); //重载运算符>>
};
Complex::Complex(real,image)
{
real=t.real;
image=t.image;
}
Complex Complex::operator +(Complex & c)
{
Complex temp;
temp.real=real+c.real;
temp.image=image+c.image;
return temp;
}
Complex Complex::operator *(Complex & c)
{
Complex temp;
temp.real=real*c.real-image*c.image ;
temp.image=real*c.image+image*c.real;
return temp;
}
Complex Complex::operator -(Complex & c)
{
Complex temp;
temp.real=real-c.real;
temp.image=image-c.image;
return temp;
}
Complex Complex::operator /(Complex & c)
{
Complex temp;
double square=c.real*c.real+c.image*c.image;
if (fabs(square)<1e-6)
{
cout<<"除数为, 不能进行除法运算!";
return temp;
}
temp.real=(real*c.real+image*c.image)/square;
temp.image=(c.real*image-real*c.image)/square;
return temp;
}
ostream & operator<<(ostream & output,Complex & real,Complex &image)//重载运算符<<
{
output<<t.real<<"實部"<<t.image<<"虛部";
return output;
}
istream & operator>>(istream & input,Complex & real,Complex &image) //重载运算符>>
{
int real, image;
input>>real;
input.ignore(); //ignore()是istream类的成员函数
input>>image; //ignore()的作用:删除输入流中指定数目的字符
input.ignore();
t=Complex(real,image); //用输入内容来修改时间对象t的值
return input;
}
void main()
{
Complex c1(1,2), c2(3,-4),c3,c4,c5,c6;
c3=c1+c2;
c4=c1*c2;
c5=c1-c2;
c6=c1/c2;
//输出加的结果
c1.display();
cout<<"+";
c2.display();
cout<<"=";
c3.display();
cout<<endl;
//输出乘的结果
c1.display();
cout<<"*";
c2.display();
cout<<"=";
c4.display ();
cout<<endl;
//输出减的结果
c1.display();
cout<<"-";
c2.display();
cout<<"=";
c5.display();
cout<<endl;
//输出除的结果
c1.display();
cout<<"/";
c2.display();
cout<<"=";
c6.display();
cout<<endl;
}