Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Complex
- {
- private:
- double real, imag;
- public:
- Complex(double, double);
- void print();
- double abs();
- friend Complex add(Complex, Complex);
- friend Complex sub(Complex, Complex);
- friend Complex mul(Complex, Complex);
- friend Complex div(Complex, Complex);
- };
- Complex::Complex(double real, double imag)
- {
- this->real = real;
- this->imag = imag;
- }
- void Complex::print()
- {
- cout << real << " + " << imag << "*i" << endl;
- }
- double Complex::abs()
- {
- return(sqrt(real*real + imag * imag));
- }
- Complex add(Complex c1, Complex c2)
- {
- Complex c3(c1.real + c2.real, c1.imag + c2.imag);
- return c3;
- }
- Complex sub(Complex c1, Complex c2)
- {
- Complex c3(c1.real - c2.real, c1.imag - c2.imag);
- return c3;
- }
- Complex mul(Complex c1, Complex c2)
- {
- Complex c3(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real);
- return c3;
- }
- Complex div(Complex c1, Complex c2)
- {
- double c = c2.real * c2.real + c2.imag * c2.imag;
- Complex c3((c1.real * c2.real + c1.imag * c2.imag) / c, (c1.imag * c2.real - c1.real * c2.imag) / c);
- return c3;
- }
- int main()
- {
- double r, i;
- cout << "Enter the real and imaginary parts of the first complex number: ";
- cin >> r >> i;
- Complex c1(r, i);
- cout << "You have entered the following complex number: ";
- c1.print();
- cout << "Its absolute value is " << c1.abs() << endl << endl;
- cout << "Enter the real and imaginary parts of the second complex number: ";
- cin >> r >> i;
- Complex c2(r, i);
- cout << "You have entered the following complex number: ";
- c2.print();
- cout << "Its absolute value is " << c2.abs() << endl << endl;
- Complex c3 = add(c1, c2);
- cout << "Their sum is: ";
- c3.print();
- cout << "The absolute value of the sum is " << c3.abs() << endl << endl;
- Complex c4 = sub(c1, c2);
- cout << "Their difference is: ";
- c4.print();
- cout << "The absolute value of the sum is " << c4.abs() << endl << endl;
- Complex c5 = mul(c1, c2);
- cout << "Their product is: ";
- c5.print();
- cout << "The absolute value of the product is " << c5.abs() << endl << endl;
- Complex c6 = div(c1, c2);
- cout << "Their quotient is: ";
- c6.print();
- cout << "The absolute value of the quotient is " << c6.abs() << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement