20140409 Complex again(for lab)
来源:互联网 发布:geo数据库使用方法 编辑:程序博客网 时间:2024/05/16 07:01
Description
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
main.h
#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;}
#ifndef COMPLEX_H#define COMPLEX_Husing namespace std;class Complex;ostream& operator << (ostream& out, const Complex& comp);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); friend ostream& operator << (ostream& out, const Complex& comp); Complex operator = (const Complex& other); Complex operator + (const Complex& other); Complex operator - (const Complex& other); Complex operator * (const Complex& other); Complex operator / (const Complex& other); void operator += (const Complex& other); void operator -= (const Complex& other); void operator *= (const Complex& other); void operator /= (const Complex& other); bool operator == (const Complex& other); bool operator != (const Complex& other); //Some function about operator overloading. void SetReal(double re){real = re;} void SetImag(double im){imag = im;}private: double real; double imag; };#endif
Complex.cpp
#include <iostream>#include "Complex.h"using namespace std;Complex::Complex(double a, double b) : real(a), imag(b) {}ostream& operator << (ostream& out, const Complex& comp) { if (comp.imag > 0) out << comp.real << "+" << comp.imag << "i"; else if (comp.imag < 0) out << comp.real << "-" << (0-comp.imag) << "i";}Complex Complex::operator + (const Complex& other) { Complex tmp; tmp.real = real + other.real; tmp.imag = imag + other.imag; return tmp; }Complex Complex::operator - (const Complex& other) { Complex tmp; tmp.real = real - other.real; tmp.imag = imag - other.imag; return tmp; }Complex Complex::operator * (const Complex& other) { Complex tmp; tmp.real = real*other.real - imag*other.imag; tmp.imag = real*other.imag + imag*other.real; return tmp; }Complex Complex::operator / (const Complex& other) { Complex tmp; tmp.real = (real*other.real + imag*other.imag); tmp.real = tmp.real/(other.real*other.real+other.imag*other.imag); tmp.imag = imag*other.real - real*other.imag; tmp.imag = tmp.imag/(other.real*other.real+other.imag*other.imag); return tmp; }void Complex::operator += (const Complex& other) { real = real + other.real; imag = imag + other.imag;}void Complex::operator -= (const Complex& other) { real = real - other.real; imag = imag - other.imag; }void Complex::operator *= (const Complex& other) { double a, b; a = real * other.real - imag * other.imag; b = real * other.imag + imag * other.real; real = a; imag = b; }void Complex::operator /= (const Complex& other) { double a, b; a = (real*other.real + imag*other.imag); a = a/(other.real*other.real+other.imag*other.imag); b = imag*other.real - real*other.imag; b = b/(other.real*other.real+other.imag*other.imag); real = a; imag = b;}bool Complex::operator == (const Complex& other) { if (real == other.real && imag == other.imag) return true; else return false; }bool Complex::operator != (const Complex& other) { if (real != other.real || imag != other.imag) return true; else return false; } // Some function about operator overloading.
这道题比较简单,主要是运算符重载和友元
- 20140409 Complex again(for lab)
- Complex number(for lab)
- Complex Again
- Complex Again
- StringProblem(for lab)
- SimpleCalculator(for lab)
- POJ 3224 Go for Lab Cup!(水题)
- POJ 3224 Go for Lab Cup!(水~)
- hdu1867A + B for you again(kmp)
- Complex Networks Package for MatLab
- poj3224 Go for Lab Cup!
- Account's Balance(for lab)
- ‘孤单北半球’ for DP Lab
- Just For Test Again
- hdu 1867 A + B for you again(KMP)
- HDOJ 题目1867A + B for you again(KMP)
- HDU 1867 A + B for you again(KMP)
- HDU 1867 A + B for you again(KMP)
- c++ vector删除元素
- MFC中设置对话框颜色、添加背景图片、添加音乐的方法
- 北邮新OJ87
- ZOJ 3492//见过骰子 就有思路的说
- jQuery Validate
- 20140409 Complex again(for lab)
- KM算法
- YARN/MRv2 Node Manager深入剖析—整体架构
- URAL 1450 无环图最短路
- poj 3252 Round Numbers
- BNU 29379斩
- ubuntu12.04 安装2013QQ
- windows7,windows8,ubuntu三系统64位安装
- 单例模式的实现技巧