upsidedown

opovrld bin r.i.p

Mar 9th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include<iostream.h>
  2.  
  3. class complex
  4. {
  5.     float r,i;
  6. public:
  7.     complex()
  8.     {
  9.         r=0;
  10.         i=0;
  11.     }
  12.  
  13.     complex(float a,float b)
  14.     {
  15.         r=a;
  16.         i=b;
  17.     }
  18.  
  19.     complex operator+(complex k)
  20.     {
  21.         complex t;
  22.         t.r=r+k.r;
  23.         t.i=i+k.i;
  24.         return t;
  25.     }
  26.  
  27.     complex operator-(complex k)
  28.     {
  29.         complex t;
  30.         t.r=r-k.r;
  31.         t.i=i-k.i;
  32.         return t;
  33.     }
  34.  
  35.     complex operator*(complex k)
  36.     {
  37.         complex t;
  38.         t.r=(r*k.r-i*k.i);
  39.         t.i=(r*k.i+i*k.r);
  40.         return t;
  41.     }
  42.  
  43.     complex operator/(complex k)
  44.     {
  45.         complex t;
  46.         t.r=(r*k.r-i*k.i)/(k.i*k.i+k.r*k.r);
  47.         t.i=(-r*k.i+i*k.r)/(k.r*k.r+k.i*k.i);
  48.         return t;
  49.     }
  50.  
  51.     void display()
  52.     {
  53.         cout<<"\n\nresult is "<<r<<" +j "<<i<<" \n";
  54.     }
  55. };
  56.  
  57. int main()
  58. {
  59.     float l[3],w[3];
  60.     int a,t=1,i;
  61.     complex p,q,s;
  62.     do
  63.     {
  64.         cout<<"\n\nenter the values of two complex nos\n";
  65.         for(i=0;i<2;i++)
  66.         {
  67.             cout<<"real part of "<<i+1<<" complex No.\n";
  68.             cin>>l[i];
  69.             cout<<"imaginary part of "<<i+1<<" complex No.\n";
  70.             cin>>w[i];
  71.         }
  72.         p=complex(l[0],w[0]);
  73.         q=complex(l[1],w[1]);
  74.         cout<<"\n\nenter a choice:\n";
  75.         cout<<"1.addition of complex nos\n";
  76.         cout<<"2.subtraction of complex nos\n";
  77.         cout<<"3.multiplication of complex nos\n";
  78.         cout<<"4.division of complex nos\n";
  79.         cout<<"5.exit\n";
  80.         cin>>a;
  81.         switch(a)
  82.         {
  83.         case 1:
  84.             s=p+q;
  85.             s.display();
  86.             break;
  87.  
  88.         case 2:
  89.             s=p-q;
  90.             s.display();
  91.             break;
  92.  
  93.         case 3:
  94.             s=p*q;
  95.             s.display();
  96.             break;
  97.  
  98.         case 4:
  99.             s=p/q;
  100.             s.display();
  101.             break;
  102.  
  103.         case 5:
  104.             t=0;
  105.             break;
  106.  
  107.         default:
  108.             cout<<"invalid\n";
  109.             break;
  110.         }
  111.     }while(t!=0);
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment