Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class ComplexNumber {
- private:
- double real;
- double imaginary;
- public:
- ComplexNumber(double real=0, double imaginary=0) {
- this->real = real;
- this->imaginary = imaginary;
- }
- ComplexNumber(const ComplexNumber &a) {
- this->real = a.real;
- this->imaginary = a.imaginary;
- }
- ComplexNumber &operator=(const ComplexNumber &a) {
- if(this == &a) {
- return *this;
- }
- this->real = a.real;
- this->imaginary = a.imaginary;
- return *this;
- }
- double module() {
- return sqrt(pow(real, 2) + pow(imaginary, 2));
- }
- friend ostream &operator<<(ostream &out, const ComplexNumber &a) {
- out<<a.real<<"+"<<a.imaginary<<"i"<<endl;
- return out;
- }
- friend ComplexNumber operator+(const ComplexNumber &a, const ComplexNumber &b) {
- ComplexNumber c(a.real + b.real, a.imaginary + b.imaginary);
- return c;
- }
- friend ComplexNumber operator-(const ComplexNumber &a, const ComplexNumber &b) {
- ComplexNumber c(a.real - b.real, a.imaginary - b.imaginary);
- return c;
- }
- friend ComplexNumber operator*(const ComplexNumber &a, const ComplexNumber &b) {
- ComplexNumber c(a.real * b.real, a.imaginary * b.imaginary);
- return c;
- }
- friend ComplexNumber operator/(const ComplexNumber &a, const ComplexNumber &b) {
- ComplexNumber c(a.real / b.real, a.imaginary / b.imaginary);
- return c;
- }
- friend bool operator==(const ComplexNumber &a, const ComplexNumber &b) {
- if(a.real == b.real && a.imaginary == b.imaginary) {
- return true;
- }
- return false;
- }
- friend bool operator>(const ComplexNumber &a, const ComplexNumber &b) {
- if(a.real > b.real) {
- return true;
- } else if(a.real == b.real) {
- if(a.imaginary > b.imaginary) {
- return true;
- }
- }
- return false;
- }
- friend bool operator<(const ComplexNumber &a, const ComplexNumber &b) {
- if(a.real < b.real) {
- return true;
- } else if(a.real == b.real) {
- if(a.imaginary < b.imaginary) {
- return true;
- }
- }
- return false;
- }
- };
- class Equation {
- private:
- ComplexNumber *p;
- char *op;
- int n;
- public:
- Equation() {
- n = 0;
- p = NULL;
- op = NULL;
- }
- friend istream &operator>>(istream &in, Equation &a) {
- double r, im;
- char rator;
- while(1) {
- in>>r>>im>>rator;
- ComplexNumber pom(r, im);
- ComplexNumber *tmp = new ComplexNumber[a.n+1];
- char *tmp2 = new char[a.n+1];
- for(int i=0; i<a.n; i++) {
- tmp2[i] = a.op[i];
- tmp[i] = a.p[i];
- }
- tmp[a.n] = pom;
- tmp2[a.n] = rator;
- a.n++;
- delete [] a.op;
- delete [] a.p;
- a.p = tmp;
- a.op = tmp2;
- if(rator == '=') {
- break;
- }
- }
- return in;
- }
- ~Equation() {
- delete [] p;
- delete [] op;
- }
- ComplexNumber result() {
- ComplexNumber res(p[0]);
- for(int i=1; i<n; i++) {
- if(op[i-1] == '+') {
- res = res + p[i];
- }
- if(op[i-1] == '-') {
- res = res - p[i];
- }
- if(op[i-1] == '*') {
- res = res * p[i];
- }
- if(op[i-1] == '/') {
- res = res / p[i];
- }
- }
- return res;
- }
- double sumModules() {
- double sum = 0;
- for(int i=0; i<n; i++) {
- sum+= p[i].module();
- }
- return sum;
- }
- };
Add Comment
Please, Sign In to add comment