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

头文件:
#ifndef COMPLEX_H_
#define COMPLEX_H_

class Complex0
{
private:
double real;
double imaginary;
public:
//construction
Complex0();
Complex0( double rea = 0.0, double ima = 0.0 );
~Complex0();
// set real,imag from coordinate.
void setReal( double rea );
void setImag( double ima );
Complex0 operator+( const Complex0 &a );
Complex0 operator-( const Complex0 &a );
Complex0 operator*( const Complex0 &a );
Complex0 operator*( double m );
}
#endif

函数实现:
#include <iostream>
using namespace std;
#include "complex.h"

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

Complex0::Complex0( double rea , double ima )
{
real = real;
imaginary = ima;
}
Complex0::~Complex0()
{
cout << "Bye1" << endl;
}
// set real,imag from coordinate.
void Complex0::setReal( double rea )
{
real = rea;
}
void Complex0::setImag( double ima )
{
imaginary = ima;
}
Complex0 Complex0::operator+( const Complex0 &a )
{
Complex0 ccc;
ccc.real = real + a.real;
ccc.imaginary = imaginary + a.imaginary;
return ccc;
}
Complex0 Complex0::operator-( const Complex0 &a )
{
Complex0 ccc;
ccc.real = real - a.real;
ccc.imaginary = imaginary - a.imaginary;
return ccc;
}
Complex0 Complex0::operator*( const Complex0 &a )
{
Complex0 ccc;
ccc.real = real * a.real - imaginary * a.imaginary;
ccc.imaginary = real * a.imaginary + imaginary * a.real;
return ccc;
}
Complex0 Complex0::operator*( double m )
{
Complex0 ccc;
ccc.real = m * real;
ccc.imaginary = m * imaginary;
return ccc;
}

主函数:
#include <iostream>
using namespace std;
#include "complex.h"

int main (void)
{
Complex0 a( 3.0, 4.0 );
Complex0 c;
cout << "Enter a complex number (q to quit )";
while( cin >> c )
{
cout << "c is "<< c << endl;
cout << "complex conjugate is " << -c << endl;
cout << "a is "<< a << endl;
cout <<"a + c = "<<a + c << endl;
cout << "Enter a complex number (q to quit)";
}
cout << "Bye`!";
return 0;
}

搜索更多相关主题的帖子: 复数 
2006-10-07 10:10
jxry8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-8-13
收藏
得分:0 

修改不出来,还请楼下的帮忙,


2006-10-07 10:12
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
Complex0();
Complex0( double rea = 0.0, double ima = 0.0 );
都是默认构造函数。


Complex0 Complex0::operator*( const Complex0 &a )
{
Complex0 ccc; 有一个宽字符


还有,使用了<<, >>, 一元-操作符但是没有重载他们。

2006-10-07 10:53
jxry8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-8-13
收藏
得分:0 

什么叫做宽字符?


2006-10-07 16:51
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
我并不详细了解
是相对于ANSI字符来说的。
象我们中文输入的一个汉字,是一个宽字符。

2006-10-07 17:14
jxry8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-8-13
收藏
得分:0 

头文件"
#ifndef COMPLEX_H_
#define COMPLEX_H_

class Complex0
{
private:
double real;
double imaginary;
public:
//construction
Complex0();
Complex0( double rea , double ima = 0.0 );
~Complex0();
// set real,imag from coordinate.
void setReal( double rea );
void setImag( double ima );
Complex0 operator+( const Complex0 &a );
// Complex0 operator-( const Complex0 &a );//删去,主函数没有用
// Complex0 operator*( const Complex0 &a );
// Complex0 operator*( double m );
friend ostream operator<<(ostream &os ,Complex0 & com );//去掉了const;
friend ostream operator>>(ostream &os ,Complex0 & com );//去掉了const
};
#endif
函数实现:
#include <iostream>
using namespace std;

#include "complex.h"

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

Complex0::Complex0( double rea , double ima )
{
real = real;
imaginary = ima;
}
Complex0::~Complex0()
{
cout << "Bye1" << endl;
}
// set real,imag from coordinate.
void Complex0::setReal( double rea )
{
real = rea;
}
void Complex0::setImag( double ima )
{
imaginary = ima;
}
Complex0 Complex0::operator+( const Complex0 &a )
{
Complex0 ccc;
ccc.real = real + a.real;
ccc.imaginary = imaginary + a.imaginary;
return ccc;
}
/* Complex0 Complex0::operator-( const Complex0 &a ) //删去,主函数没有用
{
Complex0 c;
ccc.real = real - a.real;
ccc.imaginary = imaginary - a.imaginary;
return c;
}
Complex0 Complex0::operator*( const Complex0 &a )
{
Complex0 c;
ccc.real = real * a.real - imaginary * a.imaginary;
ccc.imaginary = real * a.imaginary + imaginary * a.real;
return ccc;
}
Complex0 Complex0::operator*( double m )
{
Complex0 c;
ccc.real = m * real;
ccc.imaginary = m * imaginary;
return c;
}
*/
ostream operator <<(ostream &os ,Complex0 & com ) // 后加的,按照楼上的提示
{
os << "the complex's real :" << com.real << endl;
os << "the complex's imaginary: "<< com.imaginary << endl;
return os;
}
ostream operator >>(ostream &os ,Complex0 & com )
{
os << "the complex's real :" ;
os >> com.real << endl;
os << "the complex's imaginary: ";
os >> com.imaginary << endl;
return os;
}
主函数:
#include <iostream>
using namespace std;
#include "complex.h"

int main(void)
{
Complex0 a( 3.0, 4.0 );
Complex0 c;
cout << "Enter a complex number (q to quit )";
while( cin >> c )
{
cout << "c is "<< c << endl;
cout << "complex conjugate is " << -c << endl;
cout << "a is "<< a << endl;
cout <<"a + c = "<<a + c << endl;
cout << "Enter a complex number (q to quit)";
}
cout << "Bye!";
return 0;
}

[此贴子已经被作者于2006-10-7 17:35:35编辑过]


2006-10-07 17:20
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
[CODE]#ifndef COMPLEX_H_
#define COMPLEX_H_class Complex0
{
private:
double real;
double imaginary;
public:
//construction
// Complex0(); 这个去掉。
Complex0( double rea = 0.0, double ima = 0.0 );
~Complex0();
// set real,imag from coordinate.
void setReal( double rea );
void setImag( double ima );
Complex0 operator+( const Complex0 &a );
Complex0 operator-( const Complex0 &a );
Complex0 operator*( const Complex0 &a );
Complex0 operator*( double m );

friend ostream& operator<<(ostream &os, const Complex0 &a);
friend istream& operator>>(istream &is, Comlex0 &a); //不要加const,因为要改变a
Complex0 operator-();//一元,好象是这样。
}[/CODE]

2006-10-07 17:45
jxry8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-8-13
收藏
得分:0 
你把 / / Complex0(); 这个去掉。
那么在函数中友个 +重载里面的第一句不好实现!Complex0 ccc;


2006-10-07 18:00
jxry8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-8-13
收藏
得分:0 
怎么没有高手帮忙呀

2006-10-07 23:42
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
[CODE]
#include <iostream>
#include <cstdlib>
using namespace std;

class Complex0
{
private:
double real;
double imaginary;
public:
//construction
Complex0( double rea = 0.0, double ima = 0.0 );
~Complex0();
// set real,imag from coordinate.
void setReal( double rea );
void setImag( double ima );
Complex0 operator+( const Complex0 &a ) const;
Complex0 operator-( const Complex0 &a ) const;
Complex0 operator-() const;
Complex0 operator*( const Complex0 &a ) const;
Complex0 operator*( double m ) const;
friend ostream & operator<<(ostream & os, const Complex0 & c);
friend istream & operator>>(std::istream & is, Complex0 & c);
};

/*Complex0::Complex0()
{
real = 0.0;
imaginary = 0.0;
}*/

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

Complex0::~Complex0()
{
cout << "Bye1" << endl;
}

// set real,imag from coordinate.
void Complex0::setReal( double rea )
{
real = rea;
}

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

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

Complex0 Complex0::operator-( const Complex0 &a ) const
{
return Complex0(real - a.real, imaginary - a.imaginary);
}

Complex0 Complex0::operator-() const
{
return Complex0(-real, -imaginary);
}

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

Complex0 Complex0::operator*( double m ) const
{
return Complex0(m * real, m * imaginary);
}

ostream & operator<<(ostream & os, const Complex0 & c)
{
if(c.imaginary > 0)
os<<c.real<<"+i"<<c.imaginary;
else if(c.imaginary == 0)
os<<c.real;
else
os<<c.real<<"-i"<<-c.imaginary;
return os;
}

istream & operator>>(std::istream & is, Complex0 & c)
{
if(is>>c.real)
{
if(is>>c.imaginary)
return is;
else
{
exit(1);
}
}
else
{
exit(1);
}
}

int main (void)
{
Complex0 a(3.0, 4.0);
Complex0 c;
cout << "Enter a complex number (q to quit )\n";
while( cin >> c )
{
cout << "c is "<< c << endl;
cout << "complex conjugate is " << -c << endl;
cout << "a is "<< a << endl;
cout <<"a + c = "<<a + c << endl;
cout << "Enter a complex number (q to quit)";
}
cout << "Bye`!";
system("pause");
return 0;
}
[/CODE]

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-10-08 04:11
快速回复:复数问题~
数据加载中...
 
   



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

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