新微赢技术网
标题:
复数问题~
[打印本页]
作者:
若叶花吹雪
时间:
2009-11-4 00:24
标题:
复数问题~
头文件:
#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;
}
作者:
王者风范
时间:
2009-11-4 00:24
修改不出来,还请楼下的帮忙,
作者:
$星辰
时间:
2009-11-4 00:24
Complex0();
Complex0( double rea = 0.0, double ima = 0.0 );
都是默认构造函数。
Complex0 Complex0::operator*( const Complex0 &a )
{
Complex0 ccc; 有一个宽字符
还有,使用了<<, >>, 一元-操作符但是没有重载他们。
作者:
青松
时间:
2009-11-4 00:24
什么叫做宽字符?
作者:
最ヅ后愛上你
时间:
2009-11-4 00:24
我并不详细了解
是相对于ANSI字符来说的。
象我们中文输入的一个汉字,是一个宽字符。
作者:
⊿°屵重缺銭
时间:
2009-11-4 00:24
头文件"
#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;
}
作者:
红苹果校园
时间:
2009-11-4 00:24
#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-();//一元,好象是这样。
}
复制代码
作者:
听妈妈的话
时间:
2009-11-4 00:24
你把 / / Complex0(); 这个去掉。
那么在函数中友个 +重载里面的第一句不好实现!Complex0 ccc;
作者:
賤xs騷
时间:
2009-11-4 00:24
怎么没有高手帮忙呀
作者:
v九天〓云龙
时间:
2009-11-4 00:24
#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;
}
复制代码
欢迎光临 新微赢技术网 (http://bbs.weiying.cn/)
Powered by Discuz! X3.2