#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#ifndef _COMPLEX0_H_
#define _COMPLEX0_H_
#include<iostream>
using namespace std;
class Complex{
private:
double real, imaginary;
public:
Complex();
Complex(double, double);
Complex operator+(const Complex &) const;
Complex operator-(const Complex &) const;
Complex operator*(const Complex &) const;
Complex operator*(const double &) const;
friend Complex operator~(const Complex &);
friend Complex operator+(const double&, const Complex &);
friend Complex operator*(const double&, const Complex &);
friend ostream & operator << (ostream &, const Complex &);
friend bool operator >> (istream &, Complex &);
};
Complex::Complex(){
real = 0;
imaginary = 0;
}
Complex::Complex(double r, double i){
real=r;
imaginary=i;
}
Complex Complex::operator+(const Complex &x) const{
return Complex (real + x.real ,imaginary + x.imaginary);
}
Complex operator+(const double &x, const Complex &y){
return Complex (x + y.real, y.imaginary);
}
Complex Complex::operator-(const Complex &x) const{
return Complex (real - x.real, imaginary - x.imaginary);
}
Complex Complex::operator*(const Complex &x) const{
return Complex (real * x.real - imaginary * x.imaginary, real * x.imaginary + imaginary * x.real );
}
Complex Complex::operator*(const double & x) const{
return Complex (x * real, x * imaginary);
}
Complex operator~(const Complex & x) {
return Complex (x.real ,-x.imaginary);
}
Complex operator*(const double & x, const Complex &y) {
return Complex (x * y.real, x * y.imaginary);
}
ostream & operator << (ostream & os, const Complex & x) {
os << "("<< x.real <<", " << x.imaginary << "i)";
return os;
}
bool operator >> (istream & is, Complex & x) {
cout<<"Real part: ";
cin>> x.real;
if (cin.fail()) {
return false;
}
else {
cout<<"Imaginary part: ";
cin>> x.imaginary;
return true;
}
#endif
}
int main()
{
Complex a(3.0, 4.0); // initialize to (3,4i)
Complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c) // **********20%
{
cout << "c is " << c << '\n'; // **********10%
cout << "complex conjugate is " << ~c << '\n'; // **********10%
cout << "a is " << a << '\n'; // **********10%
cout << "a + c is " << a + c << '\n'; // **********10%
cout << "2.0 + c is " << 2.0 +c << '\n'; // **********10% global
cout << "a - c is " << a - c << '\n'; // **********10%
cout << "a * c is " << a * c << '\n'; // **********10%
cout << "2 .0* c is " << 2.0 * c << '\n'; // **********10% global
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";
return 0;
}