Create a class named Complex which contains:
Sample:
Enter the first complex number
Enter real part: 3
Enter imag part: 4
Enter the second complex number
Enter real part: 4
Enter imag part: -6
You entered the numbers
C1 = 3 + 4i
C2 = 4 – 6i
1: Addition
2: Subtraction
3: Multiplication
4: Division
5: Magnitude
6: conjugate
9: Quit
Enter your selection: 1
C1 + C2 = 7 - 2i
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
class Complex{
public:
double real, img;
Complex(){
this->real = 0;
this->img = 0;
}
Complex(int real, int img){
this->real = real;
this->img = img;
}
friend ostream& operator<<(ostream&, const Complex);
friend istream& operator>>(istream&, Complex&);
friend Complex operator+(const Complex&, const Complex&);
friend Complex operator-(const Complex&, const Complex&);
friend Complex operator*(const Complex&, const Complex&);
friend Complex operator/(const Complex&, const Complex&);
double mag();
Complex conjugate();
friend bool operator<(Complex&, Complex&);
friend bool operator>(Complex&, Complex&);
friend bool operator<=(Complex&, Complex&);
friend bool operator>=(Complex&, Complex&);
friend bool operator==(Complex&, Complex&);
friend bool operator!=(Complex&, Complex&);
};
ostream& operator<<(ostream &stream, Complex a){
stream << a.real;
if(a.img < 0) stream<< " - ";
else stream<<" + ";
stream << abs(a.img) <<"i";
return stream;
}
istream& operator>>(istream &stream, Complex& a){
cout<<"Enter real part: ";
stream>>a.real;
cout<<"Enter imaginary part: ";
stream>>a.img;
return stream;
}
Complex operator+(const Complex& a, const Complex& b){
return Complex(a.real + b.real, a.img + b.img);
}
Complex operator-(const Complex& a, const Complex& b){
return Complex(a.real - b.real, a.img - b.img);
}
Complex operator*(const Complex& a, const Complex& b){
return Complex(a.real * b.real + a.img * b.img, a.real * b.img + a.img * b.real);
}
Complex operator/(const Complex& a, const Complex& b){
return Complex((a.real * b.real + a.img * b.img)/(b.real * b.real + b.img * b.img), (a.img * b.img - a.real * b.img)/(b.real * b.real + b.img * b.img));
}
double Complex:: mag(){
return sqrt(this->real * this->real + this->img * this->img);
}
Complex Complex:: conjugate(){
return Complex(this->real, -this->img);
}
bool operator<(Complex& a, Complex& b){
return a.mag() < b.mag();
}
bool operator>(Complex& a, Complex& b){
return a.mag() > b.mag();
}
bool operator<=(Complex& a, Complex& b){
return a.mag() <= b.mag();
}
bool operator>=(Complex& a, Complex& b){
return a.mag() >= b.mag();
}
bool operator==(Complex& a, Complex& b){
return a.mag() == b.mag();
}
bool operator!=(Complex& a, Complex& b){
return a.mag() != b.mag();
}
int main(){
Complex C1, C2;
cout<<"Enter the first complex number\n";
cin>>C1;
cout<<"Enter the second complex number\n";
cin>>C2;
cout<<"You entered the numbers\n";
cout<<"C1 = "<<C1<<endl;
cout<<"C2 = "<<C2<<endl;
int choice;
cout<<"1: Addition\n";
cout<<"2: Subtraction\n";
cout<<"3: Multiplication\n";
cout<<"4: Division\n";
cout<<"5: Magnitude\n";
cout<<"6: conjugate\n";
cout<<"9: Quit\n";
do{
cin>>choice;
switch(choice){
case 1: cout<<"C1 + C2 = "<<C1 + C2<<endl; break;
case 2: cout<<"C1 - C2 = "<<C1 - C2<<endl; break;
case 3: cout<<"C1 * C2 = "<<C1 * C2<<endl; break;
case 4: cout<<"C1 / C2 = "<<C1 / C2<<endl; break;
case 5: cout<<"Magnitude of C1 = "<<C1.mag()<<endl;
cout<<"Magnitude of C2 = "<<C2.mag()<<endl;
break;
case 6: cout<<"Conjugate of C1 = "<<C1.conjugate()<<endl;
cout<<"Conjugate of C2 = "<<C2.conjugate()<<endl;
break;
default: break;
}
}while(choice != 9);
return 0;
}
Comments
Leave a comment