• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
daily pastebin goal
75%
SHARE
TWEET

Untitled

tienanh1999 Aug 10th, 2018 50 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 print()
26.     {
27.         cout <<"("<< real << "," << ima<<")"<<endl;
28.     }
29.
30.     friend Complex operator+(Complex a, Complex b);            //-----------OVERLOAD OPERATORS------------
31.     friend Complex operator-(Complex a, Complex b);
32.     friend Complex operator*(Complex a, Complex b);
33. };
34.
35. Complex operator+(Complex a, Complex b)                         //-----------OVERLOAD OPERATORS------------
36. {
37.     return Complex(a.real+b.real, a.ima + b.ima);
38. }
39. Complex operator-(Complex a, Complex b)
40. {
41.     return Complex(a.real-b.real, a.ima - b.ima);
42. }
43. Complex operator*(Complex a, Complex b)
44. {
45.     return Complex(a.real*b.real-a.ima*b.ima,a.ima*b.real+a.real*b.ima);
46. }
47.
48. class CRationalNum
49. {
50. public:
51.     Complex num, denum;
52.
53.     CRationalNum(Complex a, Complex b)
54.     {
55.         a=num;
56.         b=denum;
57.     }
58.     void print()
59.     {
60.         num.print();
61.         cout<<"/";
62.         denum.print();
63.     }
64.     friend CRationalNum operator+(CRationalNum a, CRationalNum b);
65.     friend CRationalNum operator-(CRationalNum a, CRationalNum b);
66.     friend CRationalNum operator*(CRationalNum a, CRationalNum b);
67.     friend CRationalNum operator/(CRationalNum a, CRationalNum b);
68. };
69.
70. CRationalNum operator+(CRationalNum a, CRationalNum b)
71. {
72.     return CRationalNum(a.num * b.denum + b.num * a.denum, a.denum * b.denum);
73. }
74. CRationalNum operator-(CRationalNum a, CRationalNum b)
75. {
76.     return CRationalNum(a.num * b.denum - b.num * a.denum, a.denum * b.denum);
77. }
78. CRationalNum operator*(CRationalNum a, CRationalNum b)
79. {
80.     return CRationalNum(a.num*b.num,a.denum*b.denum);
81. }
82. CRationalNum operator/(CRationalNum a, CRationalNum b)
83. {
84.     return CRationalNum(a.num * b.denum, a.denum * b.num);
85. }
86.
87. int main()
88. {
89.             Complex a(3,2), b(2,4), c(3,5), d(4,9);
90.             CRationalNum e(a,b), f(c,d), x;
91.             x=e+f;
92.             x.print();
93. return 0;
94. }
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.

Top