1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include<iostream>;
#include<complex.h>;
#include<cmath>;
#include<cstdlib>;
using namespace std;
class complex
{
public:
complex();
complex(double real);
complex(double real, double imaginary);
double getReal() const;
double getImaginary() const;
friend const complex operator +(const complex& number1, const complex& number2);
friend const complex operator -(const complex& number1, const complex& number2);
friend complex operator *(const complex& number1, const complex& number2);
friend complex operator /(const complex& number1, double divider);
friend bool operator == (const complex& number1, const complex& number2);
friend bool operator !=(const complex& number1, const complex& number2);
friend istream& operator >> (istream& inputstream, complex newNumber);
friend ostream& operator <<(ostream& outputstream, const complex& number);
private:
double real, imaginary;
friend double real(const complex&);
friend double imaginary(const complex&);
static constexpr char i = { 'i' };
};
int main()
{
}
complex::complex() :real(0), imaginary(0) {}
complex::complex(double real) : real(real) {}
complex::complex(double real, double imaginary) : real(real), imaginary(imaginary) {}
double complex::getReal()const
{
return real;
}
double complex::getImaginary()const
{
return imaginary;
}
ostream& operator <<(ostream& outputstream, const complex& number)
{
outputstream << "( " << number.real << " + " << number.imaginary << number.i << " ) " << endl;
return outputstream;
}
istream& operator >> (istream& inputstream, complex newNumber)
{
double realInput, imaginaryInput;
char startInput;
char comma;
char endInput;
inputstream >> startInput;
if ('(' != startInput)
{
cout << "Incorrect Complex Number format. Please insert a '(Real,Imaginary)' format" << endl;
exit(1);
}
inputstream >> realInput;
inputstream >> comma;
if (comma != ',')
{
cout << "Please make sure to include the comma between the complex numbers." << endl;
exit(1);
}
inputstream >> imaginaryInput;
inputstream >> endInput;
if (endInput != ')')
{
cout << "Please do not forget to close the parantheses." << endl;
exit(1);
}
newNumber = complex(realInput, imaginaryInput);
return inputstream;
}
double imaginary(const complex& number)
{
return number.getImaginary();
}
double real(const complex& number)
{
return number.getReal();
}
const complex operator +(const complex& number1, const complex& number2)
{
return complex((number1.real + number2.real), (number1.imaginary + number2.imaginary));
}
const complex operator -(const complex& number1, const complex& number2)
{
return complex((number1.real - number2.real), (number1.imaginary - number2.imaginary));
}
complex operator *(const complex& number1, const complex& number2)
{
return complex((number1.real*number2.real), (number1.imaginary*number2.imaginary));
}
complex operator /(const complex& number1, double divider)
{
return complex((number1.real / divider), (number1.imaginary / divider));
}
bool operator ==(const complex& number1, const complex& number2)
{
return ((number1.real == number2.real) && (number1.imaginary == number2.imaginary));
}
bool operator !=(const complex& number1, const complex& number2)
{
return ((number1.real != number2.real) || (number1.imaginary != number2.imaginary));
}
|