| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1628 人关注过本帖
标题:两个 复数相加.
只看楼主 加入收藏
jxry8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-8-13
收藏
 问题点数:0 回复次数:2 
两个 复数相加.

是在是不想发到这的,但是考虑到又很多任可能会的就.........而且c++ 论坛很久都没有人给与答案不的不求助
人气旺盛的c板块了
//main函数
#include <iostream>
using namespace std;

#include "complex.h"

int main(void)
{
Complex a( 3.0, 4.0 );
Complex c( 8.7, 9.0 );
Complex cd;
cd = a.operator +(c);
cout << cd;
/* 在这个地方出错.d:\net\djx\Main.cpp(12): error C2679: 二 进制“<<” : 没有找到接受“<未知>”类型的右操作数的运算符(或没有可接受的转换)*/
cout << "Bye!";
return 0;
}
//头文件
#ifndef COMPLEX_H_
#define COMPLEX_H_

using namespace std;

class Complex
{
private:
double real;
double imaginary;
public:
Complex();
Complex( double rea , double ima = 0.0 );

void setReal( double rea );
void setImag( double ima );

Complex operator+( const Complex &a );
friend ostream & operator<<( ostream &, Complex & );
};

#endif
//函数实现
#include <iostream>
using namespace std;

#include "complex.h"

Complex::Complex()
{
real = 0.0;
imaginary = 0.0;
}

Complex::Complex( double rea , double ima )
{
real = rea;
imaginary = ima;
}

void Complex::setReal( double rea )
{
real = rea;
}

void Complex::setImag( double ima )
{
imaginary = ima;
}

Complex Complex::operator+( const Complex &a )
{
return Complex( real + a.real, imaginary + a.imaginary );
}

ostream & operator<< ( ostream &os , Complex &com )
{
os << "the real is: "<< com.real;
os << "\nthe imaginary is "<< com.imaginary ;
return os;
}

搜索更多相关主题的帖子: 复数 相加 Complex main include 
2006-10-08 00:11
unicorn
Rank: 4
等 级:贵宾
威 望:14
帖 子:1066
专家分:0
注 册:2005-10-25
收藏
得分:0 

两个复数之间的运算 重载运算符就可以了 不过以后建议去C++板块,那里的人会给你更详细的回复的

重载运算符来实现复数运算:

#include<iostream.h>
class Fushu
{
public:
Fushu(){};
Fushu(float r,float i);
friend Fushu operator +(Fushu &c1,Fushu &c2);
friend Fushu operator -(Fushu &c1,Fushu &c2);
friend Fushu operator *(Fushu &c1,Fushu &c2);
friend Fushu operator /(Fushu &c1,Fushu &c2);
void display();
private:
float real;
float imag;
};
Fushu::Fushu(float r,float i)
{
real=r;
imag=i;
}
void Fushu::display()
{

if(real!=0)
{
if(imag>0)
{
if(imag=1)
cout<<real<<\"+i\"<<endl;
else
cout<<real<<\"+\"<<imag<<\"i\"<<endl;
}
if(imag<0)
cout<<real<<imag<<\"i\"<<endl;
else
cout<<real<<endl;
}
else
{
if(imag>0)
{
if(imag=1)
cout<<\"i\"<<endl;
else
cout<<\"+\"<<imag<<\"i\"<<endl;
}
if(imag<0)
cout<<imag<<\"i\"<<endl;
if(imag=0)
cout<<\"0\"<<endl;
}
}
Fushu operator +(Fushu &c1,Fushu &c2)
{
Fushu temp;
temp.real=c1.real+c2.real;
temp.imag=c1.imag+c2.imag;
return temp;
}
Fushu operator -(Fushu &c1,Fushu &c2)
{
Fushu temp;
temp.real=c1.real-c2.real;
temp.imag=c1.imag-c2.imag;
return temp;
}
Fushu operator *(Fushu &c1,Fushu &c2)
{
Fushu temp;
temp.real=c1.real*c2.real-c1.imag*c2.imag;
temp.imag=c1.real*c2.imag+c2.real*c1.imag;
return temp;
}
Fushu operator /(Fushu &c1,Fushu &c2)
{
Fushu temp;
float tt;
tt=c2.real*c2.real+c2.imag*c2.imag;
temp.real=(c1.real*c2.real+c1.imag*c2.imag)/tt;
temp.imag=(-c1.real*c2.imag+c2.real*c1.imag)/tt;
return temp;
}
int main()
{
Fushu x(1,1),y(1,-1),z;
z=x+y;
z.display();
z=x-y;
z.display();
z=x*y;
z.display();
z=x/y;
z.display();
return 0;
}


unicorn-h.spaces. ◇◆ sava-scratch.spaces.  noh enol ! pue pu!w hw u! shemle aq ll!m noh 
2006-10-08 12:12
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

#include<iostream.h>

class Virtual{
public:
Virtual(double r=0,double v=0)
{
real=r;
virt=v;
}
friend Virtual operator +(Virtual&,Virtual&);
friend Virtual operator -(Virtual&,Virtual&);
friend Virtual operator *(Virtual&,Virtual&);
friend Virtual operator /(Virtual&,Virtual&);
friend ostream& operator<<(ostream& out, Virtual& a);
void display();
protected:
double real;
double virt;
};

void Virtual::display()//输出
{
if(virt>0)
cout <<real <<"+" <<virt <<"i\n";
else
if(virt<0)
cout <<real<<virt <<"i\n";
else
cout <<real<<endl;
}

Virtual operator +(Virtual &a,Virtual &b)//+重载
{
return Virtual(a.real+b.real,a.virt+b.virt);
}

Virtual operator -(Virtual &a,Virtual &b)//-重载
{
return Virtual(a.real-b.real,a.virt-b.virt);
}

Virtual operator *(Virtual &a,Virtual &b)//*重载
{
return Virtual(a.real*b.real-a.virt*b.virt,a.real*b.virt+a.virt*b.real);
}

Virtual operator /(Virtual &a,Virtual &b)///重载
{
return Virtual((a.real*b.real+a.virt*b.virt)/(b.real*b.real+b.virt*b.virt),
(a.virt*b.real-a.real*b.virt)/(b.real*b.real+b.virt*b.virt));
}

ostream& operator<<(ostream& out, Virtual& a)//输出重载
{
if(a.virt>0)
return out <<a.real <<"+" <<a.virt <<"i\n";
else
if(a.virt<0)
return out <<a.real<<a.virt <<"i\n";
else
return out <<a.real<<endl;
}

int main()
{
Virtual a(2,5), b(7,8), c(0,0);
c = a + b;
//cout <<c;
c.display();
c = a-b;
//cout <<c;
c.display();
c = a*b;
//cout <<c;
c.display();
c = a/b;
//cout <<c;
c.display();
return(0);
}


倚天照海花无数,流水高山心自知。
2006-10-08 15:43
快速回复:两个 复数相加.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027163 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved