daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex                    // --------------COMPLEX------------------
  5. {
  6.     public:
  7.     double real, ima;
  8.  
  9.     Complex()
  10.     {
  11.         real=0.0;
  12.         ima=0.0;
  13.     }
  14.     Complex(double a)
  15.     {
  16.         real=a;
  17.         ima=0.0;
  18.     }
  19.     Complex(double a, double b)
  20.     {
  21.         real=a;
  22.         ima=b;
  23.     }
  24.    
  25.     void add(Complex a)                     // --------------ADD COMPLEX------------------
  26.     {
  27.         real = real + a.real;
  28.         ima = ima + a.ima;
  29.     }
  30.    
  31.     void sub(Complex a)                      // --------------SUBTRACT COMPLEX------------------
  32.     {
  33.         real = real - a.real;
  34.         ima = ima - a.ima;
  35.     }
  36.    
  37.     void print()
  38.     {
  39.         cout <<"("<< real << "," << ima<<")"<<endl;
  40.     }
  41.    
  42.     friend Complex operator+(Complex a, Complex b);            //-----------OVERLOAD OPERATORS------------
  43.     friend Complex operator-(Complex a, Complex b);
  44.     friend Complex operator*(Complex a, Complex b);
  45. };
  46.  
  47. Complex operator+(Complex a, Complex b)                         //-----------OVERLOAD OPERATORS------------
  48. {
  49.     return Complex(a.real+b.real, a.ima + b.ima);
  50. }
  51. Complex operator-(Complex a, Complex b)
  52. {
  53.     return Complex(a.real-b.real, a.ima - b.ima);
  54. }
  55. Complex operator*(Complex a, Complex b)
  56. {
  57.     return Complex(a.real*b.real-a.ima*b.ima,a.ima*b.real+a.real*b.ima);
  58. }
  59.  
  60. class CRationalNum
  61. {
  62. public:
  63.     Complex num, denum;
  64.  
  65.     CRationalNum(Complex a, Complex b)
  66.     {
  67.         a=num;
  68.         b=denum;
  69.     }
  70.     void print()
  71.     {
  72.         num.print();
  73.         cout<<"/";
  74.         denum.print();
  75.     }
  76.     friend CRationalNum operator+(CRationalNum a, CRationalNum b);
  77.     friend CRationalNum operator-(CRationalNum a, CRationalNum b);
  78.     friend CRationalNum operator*(CRationalNum a, CRationalNum b);
  79.     friend CRationalNum operator/(CRationalNum a, CRationalNum b);
  80. };
  81.  
  82. CRationalNum operator+(CRationalNum a, CRationalNum b)
  83. {
  84.     return CRationalNum(a.num * b.denum + b.num * a.denum, a.denum * b.denum);
  85. }
  86. CRationalNum operator-(CRationalNum a, CRationalNum b)
  87. {
  88.     return CRationalNum(a.num * b.denum - b.num * a.denum, a.denum * b.denum);
  89. }
  90. CRationalNum operator*(CRationalNum a, CRationalNum b)
  91. {
  92.     return CRationalNum(a.num*b.num,a.denum*b.denum);
  93. }
  94. CRationalNum operator/(CRationalNum a, CRationalNum b)
  95. {
  96.     return CRationalNum(a.num * b.denum, a.denum * b.num);
  97. }
  98.  
  99. int main()
  100. {
  101.             Complex a(3,2), b(2,4), c(3,5), d(4,9);
  102.             CRationalNum e(a,b), f(c,d), x;
  103.             x=e+f;
  104.             x.print();
  105. return 0;
  106. }
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