C++习题Complex Number(Eden)
来源:互联网 发布:windows 轻量级虚拟机 编辑:程序博客网 时间:2024/05/16 19:57
Question:
Create a class called Complex for performing arithmetic with complex numbers.
Complex numbers have the form
realPart + imaginaryPart * i where i is √-1
Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided.
Provide public member functions that perform the following tasks:
a) Adding two Complex numbers: The real parts are added together and the imaginary parts are added together.
b) Subtracting two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Printing Complex numbers in the form (a, b) where a is the real part and b is the imaginary part.
Hint:
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 */} // 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 */} // end function subtractvoid Complex::printComplex() { cout << '(' << realPart << ", " << imaginaryPart << ')';} // end function printComplexvoid Complex::setComplexNumber( double rp, double ip ) { realPart = rp; imaginaryPart = ip;} // end function setComplexNumber
Sample input:
1 2 3 4 5 6 7 8
Sample output:
(1, 2) + (3, 4) = (4, 6)
(5, 6) - (7, 8) = (-2, -2)
main.cpp
#include <iostream>using std::cin;using std::cout;using std::endl;#include "Complex.hpp"int main() { int n[8]; cin >> n[0] >> n[1] >> n[2] >> n[3]; cin >> n[4] >> n[5] >> n[6] >> n[7]; Complex a(n[0], n[1]), b(n[2], n[3]), c; // 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(n[4], n[5]); // reset realPart and b.setComplexNumber(n[6], n[7]); // 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
Complex.hpp*
using namespace std;class Complex { public : Complex(double ,double); //创建带参数的构造函数 /*创建类构造函数,如无创建,则系统默认生成无参数构造函数*/ Complex() //创建不带参数的函数 { realPart = 0.0; imaginaryPart = 0.0; }; void printComplex(); void setComplexNumber( double, double ); Complex add(const Complex &); Complex subtract(const Complex &); private : double realPart; double imaginaryPart; };
Complex.cpp*
#include "Complex.hpp"#include <iostream>using namespace std;using std::cout;using std::endl;Complex::Complex( double real, double imaginary ) { setComplexNumber( real, imaginary ); }Complex Complex::add( const Complex &right ) { realPart += right.realPart; imaginaryPart += right.imaginaryPart; return *this;}Complex Complex::subtract( const Complex &right ) { realPart -= right.realPart; imaginaryPart -= right.imaginaryPart; return *this;} void Complex::printComplex() { cout << '(' << realPart << ", " << imaginaryPart << ')';} void Complex::setComplexNumber( double rp, double ip ) { realPart = rp; imaginaryPart = ip;}
http://blog.csdn.net/tiantang46800/article/details/6938762
// 无参数构造函数
// 如果创建一个类你没有写任何构造函数,则系统会自动生成默认的无参构造函数,函数为空,什么都不做
// 只要你写了一个下面的某一种构造函数,系统就不会再自动生成这样一个默认的构造函数,如果希望有一个这样的无参构造函数,则需要自己显示地写出来。
// 一般构造函数(也称重载构造函数)
// 一般构造函数可以有各种参数形式,一个类可以有多个一般构造函数,前提是参数的个数或者类型不同(基于c++的重载函数原理)
// 例如:你还可以写一个 Complex( int num)的构造函数出来
// 创建对象时根据传入的参数不同调用不同的构造函数
- C++习题Complex Number(Eden)
- 537. Complex Number Multiplication(C++)
- 【leetcode】537. Complex Number Multiplication(Python & C++)
- leetcode 537 Complex Number Multiplication(Medium)
- Complex number(for lab)
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- leetcode537: Complex Number Multiplication
- Complex Number Multiplication
- Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- leetcode537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- F. The Weakest Sith----排序水题
- SQL高级查询
- TCP-IP协议栈总结、can资料网址
- jboss vfsfile ClassNotFoundException解决办法
- 天梯赛练习 天梯地图(SPFA)
- C++习题Complex Number(Eden)
- Markdown使用其二
- C++_重载、重写和重定义的区别
- SQL查询学生信息表中的学生记录
- Android中IPC机制(一)
- 程序员面试笔记---数组
- Nexus Repositories 介绍
- HTML限制不让修改窗口大小
- 一名优秀产品设计师,该做好的5项工作