Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class complex{
- public:
- float real,imag;
- complex(){
- real=0;
- imag=0;
- }
- complex operator +(complex);
- complex operator -(complex);
- complex operator *(complex);
- complex operator /(complex);
- friend ostream &operator <<(ostream &output,complex &t)
- {
- output<<t.real<<"+"<<t.imag<<"i\n";
- return output;
- }
- friend istream &operator >>(istream &input,complex &nn)
- {
- cout<<"Enter the real part:";
- input>>nn.real;
- cout<<"Enter the imaginary part";
- input>>nn.imag;
- return input;
- }
- };
- complex complex::operator +(complex n2){
- complex n3;
- n3.real=real+n2.real;
- n3.imag=imag+n2.imag;
- return(n3);
- }
- complex complex::operator -(complex n2){
- complex n3;
- n3.real=real-n2.real;
- n3.imag=imag-n2.imag;
- return n3;
- }
- complex complex::operator *(complex n2){
- complex n4;
- n4.real=(real*n2.real)-(imag*n2.imag);
- n4.imag=(imag*n2.real)+(real*n2.imag);
- return (n4);
- }
- complex complex::operator /(complex n2){
- complex n3;
- int temp=n2.real*n2.real+n2.imag*n2.imag;
- n3.real=(real*n2.real+n2.imag*imag)/temp;
- n3.imag=(imag*n2.real-n2.imag*real)/temp;
- return n3;
- }
- int main(){
- complex c1,c2,c3,c4,c5,c6;
- cout<<"Default constructor value=\n";
- int ch=10;
- cout<<c1;
- do{
- cout<<"Enter 1 to calculate sum of complex number\n";
- cout<<"Enter 2 to calculate difference of complex numbers\n";
- cout<<"Enter 3 to calculate multiplication of two complex numbers\n";
- cout<<"Enter 4 to calculate division of two complex numbers\n";
- cout<<"Enter 0 to exit\n";
- cout<<"Enter your choice:\n";
- cin>>ch;
- switch(ch){
- case 1:
- cout<<"Enter the details for first number\n";
- cin>>c1;
- cout<<"Enter the details for second number\n";
- cin>>c2;
- c3=c1+c2;
- cout<<c3;
- break;
- case 2:
- cout<<"Enter the details for first number\n";
- cin>>c1;
- cout<<"Enter the details for second number\n";
- cin>>c2;
- c3=c1-c2;
- cout<<"Subtraction of "<<c1<<" and "<<c2<<" is "<<c3;
- break;
- case 3:
- cout<<"Enter the details for first number\n";
- cin>>c1;
- cout<<"Enter the details for second number\n";
- cin>>c2;
- c3=c1*c2;
- cout<<c3;
- break;
- case 4:
- cout<<"Enter the details for first number\n";
- cin>>c1;
- cout<<"Enter the details for second number\n";
- cin>>c2;
- c3=c1/c2;
- cout<<c3;
- break;
- default:
- cout<<"Enter proper choice!!"<<endl;
- break;
- }
- }while(ch!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement