当前位置:   article > 正文

3.28 c++

3.28 c++

算数运算符

  1. #include <iostream>
  2. using namespace std;
  3. class Num
  4. {
  5. int rel; //实部
  6. int vir; //虚部
  7. public:
  8. Num():rel(2),vir(1){}
  9. Num(int rel,int vir):rel(rel),vir(vir){}
  10. Num &operator=(const Num &other)
  11. {
  12. cout << "Num的拷贝赋值函数" << endl;
  13. this->rel = other.rel;
  14. this->vir = other.vir;
  15. return *this;
  16. }
  17. friend Num operator+(const Num n1,const Num n2);
  18. friend Num operator-(const Num n1,const Num n2);
  19. friend Num operator*(const Num n1,const Num n2);
  20. friend Num operator/(const Num n1,const Num n2);
  21. friend Num operator%(const Num n1,const Num n2);
  22. Num operator+(const Num n1);
  23. Num operator-(const Num n1);
  24. Num operator*(const Num n1);
  25. Num operator/(const Num n1);
  26. Num operator%(const Num n1);
  27. void show();
  28. };
  29. Num Num::operator+(const Num n1 )
  30. {
  31. Num temp;
  32. temp.rel=this->rel+n1.rel;
  33. temp.vir=this->vir+n1.vir;
  34. return temp;
  35. }
  36. Num Num::operator-(const Num n1)
  37. {
  38. Num temp;
  39. temp.rel=this->rel-n1.rel;
  40. temp.vir=this->vir-n1.vir;
  41. return temp;
  42. }
  43. Num Num::operator*(const Num n1)
  44. {
  45. Num temp;
  46. temp.rel=this->rel*n1.rel;
  47. temp.vir=this->vir*n1.vir;
  48. return temp;
  49. }
  50. Num Num::operator/(const Num n1)
  51. {
  52. Num temp;
  53. temp.rel=this->rel/n1.rel;
  54. temp.vir=this->vir/n1.vir;
  55. return temp;
  56. }
  57. Num Num::operator%(const Num n1)
  58. {
  59. Num temp;
  60. temp.rel=this->rel%n1.rel;
  61. temp.vir=this->vir%n1.vir;
  62. return temp;
  63. }
  64. //全局函数版的加法运算符重载
  65. Num operator+(const Num n1,const Num n2)
  66. {
  67. Num temp;
  68. temp.rel = n1.rel+n2.rel;
  69. temp.vir = n1.vir+n2.vir;
  70. return temp;
  71. }
  72. //减法
  73. Num operator-(const Num n1,const Num n2)
  74. {
  75. Num temp;
  76. temp.rel = n1.rel-n2.rel;
  77. temp.vir = n1.vir-n2.vir;
  78. return temp;
  79. }
  80. Num operator*(const Num n1,const Num n2)
  81. {
  82. Num temp;
  83. temp.rel = n1.rel*n2.rel;
  84. temp.vir = n1.vir*n2.vir;
  85. return temp;
  86. }
  87. Num operator/(const Num n1,const Num n2)
  88. {
  89. Num temp;
  90. temp.rel = n1.rel/n2.rel;
  91. temp.vir = n1.vir/n2.vir;
  92. return temp;
  93. }
  94. Num operator%(const Num n1,const Num n2)
  95. {
  96. Num temp;
  97. temp.rel = n1.rel%n2.rel;
  98. temp.vir = n1.vir%n2.vir;
  99. return temp;
  100. }
  101. void Num::show()
  102. {
  103. cout << rel << "+" << vir << "i" << endl;
  104. }
  105. int main()
  106. {
  107. Num n1;
  108. Num n2(1,4);
  109. Num n3;
  110. n3 = n1+n2;
  111. n3.show();
  112. return 0;
  113. }

关系运算符

  1. #include <iostream>
  2. using namespace std;
  3. class Num
  4. {
  5. int rel; //实部
  6. int vir; //虚部
  7. public:
  8. Num():rel(2),vir(1){}
  9. Num(int rel,int vir):rel(rel),vir(vir){}
  10. Num &operator=(const Num &other)
  11. {
  12. cout << "Num的拷贝赋值函数" << endl;
  13. this->rel = other.rel;
  14. this->vir = other.vir;
  15. return *this;
  16. }
  17. friend bool operator>(const Num n1,const Num n2);
  18. friend bool operator>=(const Num n1,const Num n2);
  19. friend bool operator<(const Num n1,const Num n2);
  20. bool operator<=(const Num n1);
  21. bool operator==(const Num n1);
  22. bool operator!=(const Num n1);
  23. };
  24. bool operator>(const Num n1,const Num n2)
  25. {
  26. if(n1.rel>n2.rel)
  27. {
  28. return n1.rel>n2.rel;
  29. }
  30. else if(n1.rel==n2.rel)
  31. {
  32. return n1.vir>n2.vir;
  33. }
  34. return n1.rel>n2.rel;
  35. }
  36. bool operator>=(const Num n1,const Num n2)
  37. {
  38. if(n1.rel>n2.rel)
  39. {
  40. return n1.rel>=n2.rel;
  41. }
  42. else if(n1.rel==n2.rel)
  43. {
  44. return n1.vir>=n2.vir;
  45. }
  46. return n1.rel>=n2.rel;
  47. }
  48. bool operator<(const Num n1,const Num n2)
  49. {
  50. if(n1.rel<n2.rel)
  51. {
  52. return n1.rel<n2.rel;
  53. }
  54. else if(n1.rel==n2.rel)
  55. {
  56. return n1.vir<n2.vir;
  57. }
  58. return n1.rel<n2.rel;
  59. }
  60. bool Num::operator<=(const Num n1)
  61. {
  62. if(this->rel<n1.rel)
  63. {
  64. return this->rel<=n1.rel;
  65. }
  66. else if(this->rel==n1.rel)
  67. {
  68. return this->vir<=n1.vir;
  69. }
  70. return this->rel<=n1.rel;
  71. }
  72. bool Num::operator==(const Num n1)
  73. {
  74. if(this->rel==n1.rel)
  75. {
  76. return this->vir==n1.vir;
  77. }
  78. return this->rel==n1.rel;
  79. }
  80. bool Num::operator!=(const Num n1)
  81. {
  82. if(this->rel!=n1.rel)
  83. {
  84. return this->rel!=n1.rel;
  85. }
  86. else if(this->rel==n1.rel)
  87. {
  88. return this->vir!=n1.vir;
  89. }
  90. return this->rel!=n1.rel;
  91. }
  92. int main()
  93. {
  94. return 0;
  95. }

逻辑运算符

  1. #include <iostream>
  2. using namespace std;
  3. class Num
  4. {
  5. int rel; //实部
  6. int vir; //虚部
  7. public:
  8. Num():rel(2),vir(1){}
  9. Num(int rel,int vir):rel(rel),vir(vir){}
  10. Num &operator=(const Num &other)
  11. {
  12. cout << "Num的拷贝赋值函数" << endl;
  13. this->rel = other.rel;
  14. this->vir = other.vir;
  15. return *this;
  16. }
  17. friend bool operator&&(const Num n1,const Num n2);
  18. friend bool operator||(const Num n1,const Num n2);
  19. };
  20. bool operator&&(const Num n1,const Num n2)
  21. {
  22. if(n1.rel&&n2.rel)
  23. {
  24. return n1.vir&&n2.vir;
  25. }
  26. return n1.rel&&n2.rel;
  27. }
  28. bool operator||(const Num n1,const Num n2)
  29. {
  30. if(n1.rel||n2.rel)
  31. {
  32. return n1.vir||n2.vir;
  33. }
  34. return n1.rel||n2.rel;
  35. }
  36. int main()
  37. {
  38. cout << "Hello World!" << endl;
  39. return 0;
  40. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/353662
推荐阅读
相关标签
  

闽ICP备14008679号