使用Visual Studio.net 2003建立一个复数类,并编制主程序来验证对所创建的复数类的操作的正确性。
实验要求:
1、有两个私有成员x,y
2、分别有一个带参数构造函数和无参数构造函数
3、定义一个可以显示复数的实部和虚部的成员函数
哪位高手救我下哈~~明天试验,好急~~给个具体的程序吧~~~
使用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;
}