C++复数类编程

使用VisualStudio.net2003建立一个复数类,并编制主程序来验证对所创建的复数类的操作的正确性。实验要求:1、有两个私有成员x,y2、分别有一个带参数构造函... 使用Visual Studio.net 2003建立一个复数类,并编制主程序来验证对所创建的复数类的操作的正确性。 实验要求: 1、有两个私有成员x,y 2、分别有一个带参数构造函数和无参数构造函数 3、定义一个可以显示复数的实部和虚部的成员函数 哪位高手救我下哈~~明天试验,好急~~给个具体的程序吧~~~ 展开
 我来答
如期而至327
2014-08-16 · TA获得超过204个赞
知道答主
回答量:119
采纳率:0%
帮助的人:116万
展开全部
类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; }
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式