複數運算

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<string>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<fstream>
#include<ctype.h>

using namespace std;
class Complex
{
public:
	Complex(int a, int b) {
		real = a;
		vitual = b;
	}
	Complex operator + (Complex a) {//重載加法運算
		return Complex(real + a.real, vitual + a.vitual);
	}
	Complex operator - (Complex a) {//重載減法運算
		return Complex(real - a.real, vitual - a.vitual);
	}
	Complex operator * (Complex a) {//重載乘法運算
		return Complex(real*a.real - vitual * a.vitual, real*a.vitual + vitual * a.real);
	}
	friend ostream & operator <<(ostream & out, Complex &c);//重載插入運算符(輸出)
private:
	int real, vitual;
};
ostream & operator <<(ostream &out, Complex &c) {
	if (c.real == 0) {
		if (c.vitual == 0)
			out << "0" << endl;
		else if (c.vitual == -1)
			out << "-i" << endl;
		else if (c.vitual == 1)
			out << "i" << endl;
		else
			out << c.vitual << "i" << endl;
	}
	else if (c.vitual == 0) {
		out << c.real << endl;
	}
	else {
		out << c.real;
		if (c.vitual == -1)
			out << "-i" << endl;
		else if (c.vitual == 1)
			out << "+i" << endl;
		else
			out << (c.vitual > 0 ? "+" : "") << c.vitual <<"i"<< endl;
	}
	return out;
}
int main()
{
	int x, y,xx,yy,op;
	while (cin >> x >> y) {
		Complex ans(x, y);
		while (cin >> xx >> yy >> op) {
			if (xx == yy && yy == op && op == 0)
				break;
			Complex cur(xx, yy);
			if (op == 1) {
				ans = ans + cur;
			}
			else if (op == 2) {
				ans = ans - cur;
			}
			else {
				ans = ans * cur;
			}
		}
		cout << ans;
	}
	getchar();
	getchar();
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章