Complex Again
来源:互联网 发布:2016大数据企业排行榜 编辑:程序博客网 时间:2024/05/20 06:10
题目描述:
I know you have learned operator overloading. Your task is to finish this easy class Complex according to the function main.
class Complex {
//A friend function to print Complex numbers like a+bi where a is the real part and b is the imaginary part
public:
Complex(double = 0.0, double = 0.0);
//Some function about operator overloading.
void SetReal(double re){real = re;}
void SetImag(double im){imag = im;}
private:
double real;
double imag;
};
tip:
You should first enter the real part and then the imaginary part.
That is , cin >> x1 >> y1; Complex C(x1,y1); x1 is the real part and y1 is the imaginary part.
Sample Input
4 6 1 1
2 3
Sample Output
2+3i
4+6i
1+1i
5+7i
3+5i
-2+10i
5+1i
5+7i
4+6i
-2+10i
4+6i
1
0
代码:
#include <iostream>#include "Complex.h"using namespace std;int main(int argc, const char *argv[]) { double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; // x1 and x2 are the real part, y1 and y2 are the imaginary part Complex a(x1, y1), b(x2, y2); Complex c = a; double c1, c2; // c1 is the real part and c2 is the imaginary part cin >> c1 >> c2; c.SetReal(c1); c.SetImag(c2); cout << c << endl; cout << a << endl; c = b; cout << c << endl; // a and b are not changed. cout << (a + b) << endl; cout << (a - b) << endl; cout << (a * b) << endl; cout << (a / b) << endl; a += b; cout << a << endl; a -= b; cout << a << endl; a *= b; cout << a << endl; a /= b; cout << a << endl; cout << (a == a) << endl; cout << (a != a) << endl; return 0;}
#include <iostream>#include "Complex.h"using namespace std;Complex::Complex(double real, double imag) { this->real = real; this->imag = imag;} Complex::Complex(const Complex& otherComplex) { real = otherComplex.real; imag = otherComplex.imag;} ostream& operator <<(ostream& out, const Complex& c) { out << c.real << "+" << c.imag << "i"; return out;} Complex Complex:: operator +(const Complex& c) { double tmpreal = real + c.real; double tmpimag = imag + c.imag; Complex tmp(tmpreal, tmpimag); return tmp;} Complex Complex:: operator -(const Complex& c) { double tmpreal = real - c.real; double tmpimag = imag - c.imag; Complex tmp(tmpreal, tmpimag); return tmp;} Complex Complex:: operator *(const Complex& c) { double tmpreal = real * c.real - imag * c.imag; double tmpimag = imag * c.real + real * c.imag; Complex tmp(tmpreal, tmpimag); return tmp;} Complex Complex:: operator /(const Complex& c) { double t = c.real * c.real + c.imag * c.imag; double tmpreal = (real * c.real + imag * c.imag) / t; double tmpimag = (imag * c.real - real * c.imag) / t; Complex tmp(tmpreal, tmpimag); return tmp;} Complex& Complex:: operator = (const Complex& otherComplex) { real = otherComplex.real; imag = otherComplex.imag; return *this;} Complex& Complex:: operator += (const Complex& otherComplex) { *this = *this + otherComplex; return *this;} Complex& Complex:: operator -= (const Complex& otherComplex) { *this = *this - otherComplex; return *this;} Complex& Complex:: operator *= (const Complex& otherComplex) { *this = *this * otherComplex; return *this;} // a / b, b != 0Complex& Complex:: operator /= (const Complex& otherComplex) { *this = *this / otherComplex; return *this;} bool Complex:: operator == (const Complex& otherComplex) { if (real == otherComplex.real && imag == otherComplex.imag) return true; else return false;} bool Complex:: operator != (const Complex& otherComplex) { return !(*this == otherComplex);}#ifndef COMPLEX_H#define COMPLEX_H#include <iostream>using namespace std;class Complex { friend ostream& operator <<(ostream&, const Complex&); public: Complex(double = 0, double = 0); Complex(const Complex& otherComplex); Complex& operator = (const Complex& otherComplex); Complex& operator += (const Complex& otherComplex); Complex& operator -= (const Complex& otherComplex); Complex& operator *= (const Complex& otherComplex); Complex& operator /= (const Complex& otherComplex); Complex operator +(const Complex&); Complex operator -(const Complex&); Complex operator *(const Complex&); Complex operator /(const Complex&); bool operator == (const Complex& otherComplex); bool operator != (const Complex& otherComplex); void SetReal(double re) { real = re; } void SetImag(double im) { imag = im; } private: double real; double imag;};#endif
0 0
- Complex Again
- Complex Again
- 20140409 Complex again(for lab)
- complex
- Complex
- Complex
- complex
- Complex
- Again
- Complex Editor
- Class Complex
- Complex 赋值
- Complex类
- Complex类
- complex network1
- complex network2
- complex network3
- ROOT COMPLEX
- 黑马程序员 自学11C#编程基础之复习与冒泡排序
- iphone 开发文章链接
- PAT Advanced Level 1010
- java序列化
- New的放置语法
- Complex Again
- 页面置换算法总结-FIFO-LRU-OPT
- Java中toString的使用
- 输入一个字符串,打印出该字符串中字符的所有排列
- php缓存技术介绍
- 将vim打造成类似source insight的IDE
- Android学习笔记(十六)进度条
- Point and Circle
- java 多线程