SHARE
TWEET

EX_1

a guest Mar 12th, 2012 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. using namespace std;
  3. class complex
  4. {
  5.         private:
  6.         double re;
  7.         double im;
  8.         public:
  9.         complex()
  10.         {
  11.                 re=0,im=0;
  12.         }
  13.         complex(double re,double im)
  14.         {
  15.                 this->re=re;
  16.                 this->im=im;
  17.         }
  18.         void showcomplex()
  19.         {
  20.                 cout<<re<<" + i*"<<im<<endl;
  21.         }
  22.         complex add(complex temcomplex )
  23.         {
  24.                 complex newcomplex;
  25.                 newcomplex.re=this->re+temcomplex.re;
  26.                 newcomplex.im=this->im+temcomplex.im;
  27.                 return newcomplex;
  28.         }
  29.         complex sub(complex temcomplex)
  30.         {
  31.                 complex newcomplex;
  32.                 newcomplex.re=this->re-temcomplex.re;
  33.                 newcomplex.im=this->im-temcomplex.im;
  34.                 return newcomplex;
  35.         }
  36.         complex multip(complex temcomplex)
  37.         {
  38.                 complex newcomplex;
  39.                 newcomplex.re=(this->re)*temcomplex.re - (this->im)*temcomplex.im;
  40.                 newcomplex.im=(this->re)*temcomplex.im + (this->im)*temcomplex.re;
  41.                 return newcomplex;
  42.         }
  43.         complex divi(complex temcomplex)
  44.         {
  45.                 complex newcomplex;
  46.                 double modu;
  47.                 modu=temcomplex.re*temcomplex.re+temcomplex.im*temcomplex.im;
  48.                 newcomplex.re=((this->re)*temcomplex.re + (this->im)*temcomplex.im)/modu;
  49.                 newcomplex.im=(-(this->re)*temcomplex.im +(this->im)*temcomplex.re)/modu;
  50.                 return newcomplex;
  51.         }
  52.        
  53. };
  54.  
  55. int main()
  56. {
  57.         double a,b,x,y;
  58.         cout<<"input two complex:"<<endl;
  59.         cin>>a;
  60.         cin>>b;
  61.         cin>>x;
  62.         cin>>y;
  63.         cout<<"Z1="<<a<<" + i*"<<b<<endl;
  64.         cout<<"Z2="<<x<<" + i*"<<y<<endl;
  65.         complex z1(a,b),z2(x,y);
  66.         cout<<"Z1+Z2"<<endl;
  67.         complex z3;
  68.         z3=z1.add(z2);
  69.         z3.showcomplex();
  70.         cout<<"Z1-Z2"<<endl;
  71.         z3=z1.sub(z2);
  72.         z3.showcomplex();
  73.         cout<<"Z1*Z2"<<endl;
  74.         z3=z1.multip(z2);
  75.         z3.showcomplex();
  76.         if(x!=0||y!=0)
  77.         {
  78.                 cout<<"Z1/Z2"<<endl;
  79.                 z3=z1.divi(z2);
  80.                 z3.showcomplex();
  81.         }
  82.         else
  83.                 cout<<"Can not divide because Z2=0"<<endl;     
  84.         return 0;      
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top