Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream.h>
- class complex
- {
- float r,i;
- public:
- complex()
- {
- r=0;
- i=0;
- }
- complex(float a,float b)
- {
- r=a;
- i=b;
- }
- complex operator+(complex k)
- {
- complex t;
- t.r=r+k.r;
- t.i=i+k.i;
- return t;
- }
- complex operator-(complex k)
- {
- complex t;
- t.r=r-k.r;
- t.i=i-k.i;
- return t;
- }
- complex operator*(complex k)
- {
- complex t;
- t.r=(r*k.r-i*k.i);
- t.i=(r*k.i+i*k.r);
- return t;
- }
- complex operator/(complex k)
- {
- complex t;
- t.r=(r*k.r-i*k.i)/(k.i*k.i+k.r*k.r);
- t.i=(-r*k.i+i*k.r)/(k.r*k.r+k.i*k.i);
- return t;
- }
- void display()
- {
- cout<<"\n\nresult is "<<r<<" +j "<<i<<" \n";
- }
- };
- int main()
- {
- float l[3],w[3];
- int a,t=1,i;
- complex p,q,s;
- do
- {
- cout<<"\n\nenter the values of two complex nos\n";
- for(i=0;i<2;i++)
- {
- cout<<"real part of "<<i+1<<" complex No.\n";
- cin>>l[i];
- cout<<"imaginary part of "<<i+1<<" complex No.\n";
- cin>>w[i];
- }
- p=complex(l[0],w[0]);
- q=complex(l[1],w[1]);
- cout<<"\n\nenter a choice:\n";
- cout<<"1.addition of complex nos\n";
- cout<<"2.subtraction of complex nos\n";
- cout<<"3.multiplication of complex nos\n";
- cout<<"4.division of complex nos\n";
- cout<<"5.exit\n";
- cin>>a;
- switch(a)
- {
- case 1:
- s=p+q;
- s.display();
- break;
- case 2:
- s=p-q;
- s.display();
- break;
- case 3:
- s=p*q;
- s.display();
- break;
- case 4:
- s=p/q;
- s.display();
- break;
- case 5:
- t=0;
- break;
- default:
- cout<<"invalid\n";
- break;
- }
- }while(t!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment