永发信息网

C++复数类编程

答案:2  悬赏:80  手机版
解决时间 2021-07-26 08:15

使用Visual Studio.net 2003建立一个复数类,并编制主程序来验证对所创建的复数类的操作的正确性。

实验要求:

1、有两个私有成员x,y

2、分别有一个带参数构造函数和无参数构造函数

3、定义一个可以显示复数的实部和虚部的成员函数

哪位高手救我下哈~~明天试验,好急~~给个具体的程序吧~~~

最佳答案

类complex代码


#ifndef COMPLEX_H
#define COMPLEX_H


#include<iostream>
#include<string>


using namespace std;


class Complex {
public: // menber functions
// default constructor
Complex();
// another constructor
Complex(float real, float imaginary);


// some arithmetic and stream facilitators
Complex Add(const Complex &r) const;
Complex Subtract(const Complex &r) const;
Complex Multiply(const Complex &r) const;
Complex Divide(const Complex &r) const;


void Insert(ostream &sout) const;
void Extract(istream &sin);
protected:
// inspectors
float GetReal() const;
float GetImaginary() const;
// mutators
void SetReal(float real);
void SetImaginary(float imaginary);
private:
// data members
float RealValue;
float ImaginaryValue;
};


// A description of auxiliary operators
Complex operator + (const Complex &r, const Complex &s);
Complex operator - (const Complex &r, const Complex &s);
Complex operator * (const Complex &r, const Complex &s);
Complex operator / (const Complex &r, const Complex &s);


ostream& operator << (ostream &sout, const Complex &s);
istream& operator >> (istream &sin, Complex &r);


#endif


实现代码
#include<iostream>
#include<string>
#include "complex.h"



using namespace std;



// default constructor
Complex::Complex() {
SetReal(0);
SetImaginary(0);
}



// (real, imaginary) constructor
Complex::Complex(float real, float imaginary) {
SetReal(real);
SetImaginary(imaginary);
}



// get the entered rel
float Complex::GetReal() const {
return RealValue;
}



// get the entered imaginary
float Complex::GetImaginary() const {
return ImaginaryValue;
}



// set the real
void Complex::SetReal(float real) {
RealValue = real;
}



// set the imaginary
void Complex::SetImaginary(float imaginary) {
ImaginaryValue = imaginary;
}



// adding Complexs
Complex Complex::Add(const Complex &r) const {
float a = GetReal();
float b = GetImaginary();
float c = r.GetReal();
float d = r.GetImaginary();


return Complex(a + c, b + d);
}



// subtracting Complexs
Complex Complex::Subtract(const Complex &r) const {
float a = GetReal();
float b = GetImaginary();
float c = r.GetReal();
float d = r.GetImaginary();


return Complex(a - c, b - d);
}



// multiplying Complexs
Complex Complex::Multiply(const Complex &r) const {
float a = GetReal();
float b = GetImaginary();
float c = r.GetReal();
float d = r.GetImaginary();


return Complex(a*c - b*d, a*d + b*c);
}



// dividing Complexs
Complex Complex::Divide(const Complex &r) const {
float a = GetReal();
float b = GetImaginary();
float c = r.GetReal();
float d = r.GetImaginary();
float e = c * c + d * d;


if ( ( c == 0 ) && ( d == 0 ) ) {
cout << "Can not divide 0+0i" << endl;
exit(1);
}
else {
return Complex( ( a*c + b*d )/e, (b*c - a*d)/e );
}
}



// inserting a complex
void Complex::Insert(ostream &sout) const {
sout << GetReal() << '+' << GetImaginary() << 'i';
return;
}
void Complex::Extract(istream &sin) {
float real;
float imaginary;
char character1;
char character2;
cin >> real >> character1 >> imaginary >> character2;
if ( ( character1 == '+' ) && ( character2 == 'i' ) ) {
SetReal(real);
SetImaginary(imaginary);
return;
}
else {
cout << "Wrong character(s) or number(s)entered. Using + or i." << endl;
exit(1);
}
}



// adding Complexs
Complex operator + (const Complex &r, const Complex &s) {
return r.Add(s);
}



// subtracting Complexs
Complex operator - (const Complex &r, const Complex &s) {
return r.Subtract(s);
}



// multiplying Complexs
Complex operator * (const Complex &r, const Complex &s) {
return r.Multiply(s);
}



// dividing Complexs
Complex operator / (const Complex &r, const Complex &s) {
return r.Divide(s);
}



// inserting a Complex
ostream& operator << ( ostream &sout, const Complex &r) {
r.Insert(sout);
return sout;
}



// extracting a Complex
istream& operator >> ( istream &sin, Complex &r) {
r.Extract(sin);
return sin;
}

全部回答
这是complex的完整代码,有类和实现代码,可以找到你的结果的。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
我的手机为什么下载不了主题 说什么需要cmwap
谁有qq爱墙的图标 能给我一颗吗
买八位数QQ号.而且是处号,想知道.在要号时需
怎样处理过于极端的情绪?
网上开店要什么?
中国各省的十大将军县是哪些地方?
如果无法面对现实,那要该怎么办?
求个数学提题反正我到现在是没画上来.各位你
我向女孩表白了,女孩问我喜欢她什么?我不知
你好 有谁能帮我介绍一位医生朋友吗
怎么删掉我在手机腾讯网里的新闻评论阿?
怎样做才能使心里感觉到平衡?
入门派的问题
学测量专业将来出来好分配么?
我的积分不涨,怎么还减那?
推荐资讯
财付通要是充一个QQ币,图标能亮吗?
为什么追你的女生被你拒绝后有些会要求和你做
很想戒烟从每天1包左右整到现在3-5杆了 还是
87峨眉,一身套+3j宝石,,值多少钱
孙小宝夫妻是出车祸了吗
现在汽车违章怎么查询????急
黄河远上白云间 远上是什么意思
我家的内存是256的要怎样调虚拟内存玩dnf才不
合肥市,万振逍遥苑,太湖路,紫蓬湾酒店,有
PSP3000 5.03MHU系统PS插件有吗?
今晚2009年10月29号开什么码
哪位黄钻邀请我把QQ农场开通啊
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?