C#编程:定义一个复数类,实现复数的简单加减法运算,并能显示结果。
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
展开全部
// 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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询