C#编程:定义一个复数类,实现复数的简单加减法运算,并能显示结果。

C#编程:定义一个复数类,实现复数的简单加减法运算,并能显示结果。急,在线等... C#编程:定义一个复数类,实现复数的简单加减法运算,并能显示结果。急,在线等 展开
 我来答
  • 你的回答被采纳后将获得:
  • 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
百湛ft
2019-11-10
知道答主
回答量:2
采纳率:0%
帮助的人:1281
展开全部
// Lab 4: Complex.h#ifndef COMPLEX_H#define COMPLEX_H/* Write class definition for Complex */#include <iostream>//#include <double>using namespace std;#include "Complex.h"// Complex class definitionclass Complex{public: ComplexNumber(double ,double ); void setComplexNumber(double rp, double ip); // double getComplexNumber(); Complex add(const Complex & ); Complex subtract(const Complex &); void printComplex(); Complex(double ,double);private: double realPart; double imaginaryPart;};#endif// Lab 4: ComplexTest.cpp#include <iostream>using namespace std;#include "Complex.h"int main(){ Complex a( 1, 7 ), b( 9, 2 ), c(0,0); // create three Complex objects a.printComplex(); // output object a cout << " + "; b.printComplex(); // output object b cout << " = "; c = a.add( b ); // invoke add function and assign to object c c.printComplex(); // output object c cout << '\n'; a.setComplexNumber( 10, 1 ); // reset realPart and b.setComplexNumber( 11, 5 ); // and imaginaryPart a.printComplex(); // output object a cout << " - "; b.printComplex(); // output object b cout << " = "; c = a.subtract( b ); // invoke add function and assign to object c c.printComplex(); // output object c cout << endl;} // end main// Lab 4: Complex.cpp// Member-function definitions for class Complex.#include <iostream>using namespace std;#include "Complex.h"Complex::Complex( double real, double imaginary ){ setComplexNumber( real, imaginary );} // end Complex constructorComplex Complex::add( const Complex &right ){ /* Write a statement to return a Complex object. Add the realPart of right to the realPart of this Complex object and add the imaginaryPart of right to the imaginaryPart of this Complex object */ realPart=realPart+right.realPart; imaginaryPart=imaginaryPart+right.imaginaryPart; return Complex(realPart,imaginaryPart);} // end function addComplex Complex::subtract( const Complex &right ){ /* Write a statement to return a Complex object. Subtract the realPart of right from the realPart of this Complex object and subtract the imaginaryPart of right from the imaginaryPart of this Complex object */ realPart=realPart-right.realPart; imaginaryPart=imaginaryPart-right.imaginaryPart; return Complex(realPart,imaginaryPart);} // end function subtractvoid Complex::printComplex(){ cout << '(' << realPart << ", " << imaginaryPart << ')';} // end function printComplexvoid Complex::setComplexNumber( double rp, double ip ){ realPart = rp; imaginaryPart = ip;} // end function setComplexNumber
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式