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

头文件:
#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
快速回复:复数问题~
数据加载中...
 
   



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

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