鱼C论坛

 找回密码
 立即注册
查看: 1776|回复: 2

关于重载运算符的课后作业

[复制链接]
发表于 2019-5-14 17:41:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 1955207586 于 2019-5-14 17:49 编辑
  1. // // test.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6. class Math
  7. {
  8. public:
  9.         Math();
  10.         int son,mom;
  11.         Num(int a,int b);
  12.         void print();
  13.         Math operator+(Math &c);
  14.         Math operator-(Math &c);
  15.         Math operator*(Math &c);
  16.         Math operator/(Math &c);
  17. };
  18. Math::Math()
  19. {
  20.         son=0;
  21.         mom=1;
  22. }
  23. Math::Num(int a,int b)
  24. {
  25.         son=a;
  26.         mom=b;
  27. }
  28. void Math::print()
  29. {
  30.         cout<<son<<"/"<<mom<<endl;
  31. }
  32. Math Math::operator +(Math &c)
  33. {
  34.         return Math::Num(son*c.mom+mom*c.son,mom*c.mom);
  35. }
  36. Math Math::operator -(Math &c)
  37. {
  38.         return Math::Num(son*c.mom-mom*c.son,mom*c.mom);
  39. }
  40. Math Math::operator *(Math &c)
  41. {
  42.         return Math::Num(son*c.son,mom*c.mom);
  43. }
  44. Math Math::operator /(Math &c)
  45. {
  46.         return Math::Num(son*c.mom,mom*c.son);
  47. }
  48. int main(int argc, char* argv[])
  49. {
  50.         Math a,b,c;
  51. /*
  52.         c=a+b;
  53.         c.print();
  54.         c=a-b;
  55.         c.print();
  56.         c=a*b;
  57.         c.print();
  58.         c=a/b;
  59.         c.print();*/
  60.         return 0;
  61. }
复制代码

结果报错D:\C_test\test\test.cpp(35) : error C2664: '__thiscall Math::Math(const class Math &)' : cannot convert parameter 1 from 'int' to 'const class Math &'
        Reason: cannot convert from 'int' to 'const class Math'
        No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C_test\test\test.cpp(35) : error C2553: no legal conversion of return value to return type 'class Math *'
D:\C_test\test\test.cpp(39) : error C2664: '__thiscall Math::Math(const class Math &)' : cannot convert parameter 1 from 'int' to 'const class Math &'
        Reason: cannot convert from 'int' to 'const class Math'
        No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C_test\test\test.cpp(39) : error C2553: no legal conversion of return value to return type 'class Math *'
D:\C_test\test\test.cpp(43) : error C2664: '__thiscall Math::Math(const class Math &)' : cannot convert parameter 1 from 'int' to 'const class Math &'
        Reason: cannot convert from 'int' to 'const class Math'
        No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C_test\test\test.cpp(43) : error C2553: no legal conversion of return value to return type 'class Math *'
D:\C_test\test\test.cpp(47) : error C2664: '__thiscall Math::Math(const class Math &)' : cannot convert parameter 1 from 'int' to 'const class Math &'
        Reason: cannot convert from 'int' to 'const class Math'
        No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C_test\test\test.cpp(47) : error C2553: no legal conversion of return value to return type 'class Math *'
执行 cl.exe 时出错.
求大神解答。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-5-14 17:59:25 | 显示全部楼层
本帖最后由 Croper 于 2019-5-14 18:06 编辑

第一个问题、
你”>>"反了,是“<<”好吧,
  1. void Math::print()
  2. {
  3.         std::cout<<son<<"/"<<mom<<std::endl;
  4. }
复制代码


第二个问题、原来的Math()是构造函数
你没头没尾的Num()是什么鬼,系统当然没法识别

第三:作为一个分数,你似乎应该给它加上自动化简

第四:建议养成使用const的习惯,
  1. Math Math::operator +(const Math &c) const
  2. {
  3.         return Math(son*c.mom+mom*c.son,mom*c.mom);
  4. }
复制代码


不然你这种表达式
  1. Math a,b,c;
  2. Math d=a+(b+c);
复制代码

将会报错


第五、(建议)构造函数加上分母不能为0的检测代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-14 18:11:10 | 显示全部楼层
本帖最后由 Croper 于 2019-5-14 18:15 编辑

这是我以前写的分数类,你可以参考下
  1. //========================================
  2. //fraction.h
  3. //Created by Croper
  4. //last modified in Dec.15.2018
  5. //实现分数类,并通过重载,实现分数类的四则运算以及类型转换
  6. //=========================================================


  7. #pragma once
  8. #include <iostream>
  9. #include <stack>

  10. double fraction_accuracy = 0.000001;   //当与实数进行转换时允许的最大误差

  11. class fraction
  12. {
  13.         friend const fraction& operator+(const fraction, const  fraction);
  14.         friend const fraction& operator-(const fraction, const  fraction);
  15.         friend const fraction& operator*(const fraction, const  fraction);
  16.         friend const fraction& operator/(const fraction, const  fraction);
  17.         friend bool operator==(const fraction, const  fraction);
  18.         friend bool operator!=(const fraction, const  fraction);
  19.         friend std::ostream &operator<<(std::ostream&, fraction);
  20. private:
  21.         long num;   //分子
  22.         long den;   //分母
  23.         void Reduce();    //约分
  24. public:
  25.         fraction();
  26.         fraction(int);
  27.         fraction(int,int);
  28.        
  29.         fraction(double);
  30.         fraction(const fraction&);

  31.         explicit operator int() const;     //向下取整
  32.         explicit operator long() const;
  33.         explicit operator double() const;
  34.         explicit operator float() const;

  35.         fraction& operator+=(const fraction);
  36.         fraction& operator-=(const fraction);
  37.         fraction& operator*=(const fraction);
  38.         fraction& operator/=(const fraction);
  39. };

  40. void fraction::Reduce() //约分
  41. {
  42.         long a;
  43.         long p, q;

  44.         if (den < 0) {
  45.                 num = 0 - num;
  46.                 den = 0 - den;
  47.         }
  48.         p = num;
  49.         q = den;
  50.         do                         //辗转相除求最大公约数
  51.         {
  52.                 a = p % q;
  53.                 p = q;
  54.                 q = a;
  55.         } while (a != 0);

  56.         num/= p;
  57.         den/= p;
  58. }

  59. fraction::fraction()
  60. {
  61.         num = 0;
  62.         den = 1;
  63. }

  64. fraction::fraction(int a)
  65. {
  66.         num = a;
  67.         den = 1;
  68. }

  69. fraction::fraction(int a, int b)
  70. {
  71.         if (b == 0) throw "除数不能为 0" ;
  72.         num = a;
  73.         den = b;
  74.         Reduce();
  75. }

  76. #if 0==1
  77. fraction::fraction(double d)
  78. {
  79.         static fraction fsum;
  80.         double e;
  81.         fsum = (long)floor(d + 0.5);
  82.         e = abs((double)fsum - d);
  83.         while (abs(e) > abs(fraction_accuracy))
  84.         {
  85.                 double x = 1. / e;
  86.                 double y = x + 0.5;
  87.                 int z = (int)floor(y);

  88.                 fsum += fraction(1,z);
  89.                 e = d-(double)fsum;
  90.         }
  91.         num = fsum.num;
  92.         den = fsum.den;
  93. }
  94. #endif

  95. //连分数法浮点数化分数
  96. fraction::fraction(double d)
  97. {
  98.         std::stack<int> stk,stk1;
  99.         static fraction fsum;
  100.         long n;
  101.         double e,err;

  102.         n = (long)floor(d + 0.5);
  103.         stk.push(n);
  104.         e = d-(double)n;
  105.         err = abs(e);
  106.         while (err > fraction_accuracy)
  107.         {
  108.                 e = 1. / e;
  109.                 n = (long)floor(e + 0.5);
  110.                 e = e - (double)n;
  111.                 stk.push(n);
  112.                 stk1 = stk;
  113.                 fsum = 0;
  114.                 while (!stk1.empty())
  115.                 {
  116.                         n = stk1.top();
  117.                         stk1.pop();
  118.                         fsum = 1 / (fsum+n);
  119.                 }
  120.                 fsum = 1 / fsum;
  121.                 err = abs((double)fsum - d);
  122.         }
  123.         num = fsum.num;
  124.         den = fsum.den;
  125. }
  126. fraction::fraction(const fraction& f)
  127. {
  128.         num = f.num;
  129.         den = f.den;
  130. }

  131. fraction& fraction::operator+=(const fraction f)
  132. {
  133.         num = num*f.den + f.num*den;
  134.         den = den * f.den;
  135.         Reduce();
  136.         return *this;
  137. }

  138. fraction& fraction::operator-=(const fraction f)
  139. {
  140.         num = num * f.den - f.num*den;
  141.         den = den * f.den;
  142.         Reduce();
  143.         return *this;
  144. }
  145. fraction& fraction::operator*=(const fraction f)
  146. {
  147.         num = num * f.num;
  148.         den = den * f.den;
  149.         Reduce();
  150.         return *this;
  151. }
  152. fraction& fraction::operator/=(const fraction f)
  153. {
  154.         if (f.num == 0) throw "除数不能为0";
  155.         num = num * f.den;
  156.         den = den * f.num;
  157.         Reduce();
  158.         return *this;
  159. }

  160. fraction::operator int() const      //按c++取整规则
  161. {
  162.         return (num / den);
  163. }

  164. fraction::operator long() const      //按c++取整规则
  165. {
  166.         return (num / den);
  167. }

  168. fraction::operator double() const
  169. {
  170.         return ((double)num / (double)den);
  171. }

  172. fraction::operator float() const
  173. {
  174.         return ((float)num / (float)den);
  175. }

  176. const fraction& operator+(const fraction f1, const fraction f2)
  177. {
  178.         static fraction f;
  179.         f.num = f1.num*f2.den + f2.num*f1.den;
  180.         f.den = f1.den*f2.den;
  181.         f.Reduce();
  182.         return f;
  183. }
  184. const fraction& operator-(const fraction f1, const fraction f2)
  185. {
  186.         static fraction f;
  187.         f.num = f1.num*f2.den - f2.num*f1.den;
  188.         f.den = f1.den*f2.den;
  189.         f.Reduce();
  190.         return f;
  191. }

  192. const fraction& operator*(const fraction f1, const fraction f2)
  193. {
  194.         static fraction f;
  195.         f.num = f1.num*f2.num;
  196.         f.den = f1.den*f2.den;
  197.         f.Reduce();
  198.         return f;
  199. }

  200. const fraction& operator/(const fraction f1, const fraction f2)
  201. {
  202.         static fraction f;
  203.         if (f2.num == 0) throw "除数不能为0";
  204.         f.num = f1.num*f2.den;
  205.         f.den = f1.den*f2.num;
  206.         f.Reduce();
  207.         return f;
  208. }
  209. bool operator==(const fraction f1, const fraction f2)
  210. {
  211.         return ((f1.num == f2.num) && (f1.den == f2.den));
  212. }
  213. bool operator!=(const fraction f1, const fraction f2)
  214. {
  215.         return ((f1.num != f2.num) || (f1.den != f2.den));
  216. }

  217. std::ostream &operator<<(std::ostream& os, fraction f)
  218. {
  219.         os << f.num << "/" << f.den;
  220.         return os;
  221. }

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 21:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表