SE Comp Sem-II OOP Prac-5 (Complex numbers)

#include<iostream.h>
#include<conio.h>

class complex
{
float  a,b;
public:
complex()

{
 a=0;b=0;
 }
 complex(float x,float y);
void display();
complex operator -(complex x) ;
complex operator /(complex x) ;
friend complex operator +(complex x,complex y) ;
friend complex operator *(complex x,complex y) ;
};

complex::complex(float x,float y)
{
  a=x;
   b=y;
}



void complex::display()
{
 cout<<"\nentered complex no\t";
 cout<<a<<"+"<<b<<"i";
 }

complex complex::operator -(complex y)
{
  complex z;
  z.a= a-y.a;
  z.b=b-y.b;
  return(z);
}
complex complex::operator /(complex y)
{
 complex z,l;
 float q;
  z.a= a*y.a+y.b*b;
  z.b=b*y.a-y.b*a;
  q=(y.a*y.a)+(y.b*y.b);
   l.a=z.a/(q) ;
      l.b=z.b/(q) ;
  return(l);
}
complex operator +(complex x,complex y)
{
  complex z;
     z.a=x.a+y.a;
  z.b=x.b+y.b;
  return(z);
}
 complex operator *(complex x,complex y)
{
  complex z;
     z.a= x.a*y.a-y.b*x.b;
  z.b=x.a*y.b+y.a*x.b;
  return(z);
}

int main()
{
complex a,b,c;
int o;
float x,y;
char d;
clrscr();

do
{
  cout<<"\nenter your choice\t";
  cout<<"\n1:to add two complex numbers \t";
    cout<<"\n2:to subtract two complex numbers \t";
      cout<<"\n3:to multiply two complex numbers \t";
    cout<<"\n4:to divide two complex numbers \t";
    cin>>o;
  switch(o)
  {
  case 1:
  {
   cout<<"\nenter the data\t";
 cout<<"\nenter the real part\t";
 cin>>x;
 cout<<"\n enter imaginary part\t";
 cin>>y;
 a=complex(x,y);


cout<<"\nenter the data\t";
 cout<<"\nenter the real part\t";
 cin>>x;
 cout<<"\n enter imaginary part\t";
 cin>>y;
  b=complex(x,y);
    c=a+b;
    c.display();
   }
  break;
    case 2:
     {
       cout<<"\nenter the data\t";
 cout<<"\nenter the real part\t";
 cin>>x;
 cout<<"\n enter imaginary part\t";
 cin>>y;
 a=complex(x,y);


cout<<"\nenter the data\t";
 cout<<"\nenter the real part\t";
 cin>>x;
 cout<<"\n enter imaginary part\t";
 cin>>y;
  b=complex(x,y);
    c=a-b;
    c.display();
   }
  break;
    case 3:
     {
     cout<<"\nenter the data\t";
 cout<<"\nenter the real part\t";
 cin>>x;
 cout<<"\n enter imaginary part\t";
 cin>>y;
 a=complex(x,y);


cout<<"\nenter the data\t";
 cout<<"\nenter the real part\t";
 cin>>x;
 cout<<"\n enter imaginary part\t";
 cin>>y;
  b=complex(x,y);
    c=a*b;
    c.display();
   }
  break;
    case 4:
     {
     cout<<"\nenter the data\t";
 cout<<"\nenter the real part\t";
 cin>>x;
 cout<<"\n enter imaginary part\t";
 cin>>y;
 a=complex(x,y);


cout<<"\nenter the data\t";
 cout<<"\nenter the real part\t";
 cin>>x;
 cout<<"\n enter imaginary part\t";
 cin>>y;
  b=complex(x,y);
    c=a/b;
    c.display();
   }
  break;
  default:
  cout<<"\nwrong choice\t";
  break;
  }
   cout<<"\nif you want to continue please press .....y\t";
    cin>>d;
  }while(d=='y'||d=='Y');

  return(0);
  }

No comments:

Post a Comment