Description

编写分数类Fraction,实现两个分数的加、减、乘和除四则运算。主函数已给定。

Input

每行四个数,分别表示两个分数的分子和分母,以0 0 0 0 表示结束。

Output

空格分隔的两个分数的减和除的结果。

Sample Input

  1. 1 2 -1 2
  2. 4 3 3 4
  3. 0 0 0 0

Sample Output

  1. 1 -1
  2. 7/12 16/9
  1.  

(1)普通版。

  1. /* All rights reserved.
  2. * 文件名:test.cpp
  3. * 作者:陈丹妮
  4. * 完毕日期:2015年 7 月 2 日
  5. * 版 本 号:v1.0
  6. */
  7. #include <iostream>
  8. using namespace std;
  9. class Fraction
  10. {
  11. private:
  12. int x;
  13. int y;
  14. public:
  15. Fraction(int a=0,int b=1):x(a),y(b) {}
  16. friend istream& operator>>(istream& input,Fraction &f);
  17. bool operator ==(int a);
  18. void output();
  19. Fraction operator -(Fraction &f1);
  20. Fraction operator /(Fraction &f1);
  21. };
  22. istream& operator>>(istream& input,Fraction &f)
  23. {
  24. input>>f.x>>f.y;
  25. return input;
  26. }
  27. bool Fraction::operator==(int a)
  28. {
  29. if(x==a)
  30. return true;
  31. else
  32. return false;
  33. }
  34. Fraction Fraction::operator -(Fraction &f1)
  35. {
  36. Fraction f;
  37. f.x=x*f1.y-f1.x*y;
  38. f.y=y*f1.y;
  39. return f;
  40. }
  41. Fraction Fraction::operator /(Fraction &f1)
  42. {
  43. Fraction f;
  44. if(!f1.x) return *this;
  45. f.x=x*f1.y;
  46. f.y=y*f1.x;
  47. return f;
  48. }
  49. void Fraction::output()
  50. {
  51. int r,m=x,n=y,t;
  52. if(m<n)
  53. {
  54. t=n;
  55. n=m;
  56. n=t;
  57. }
  58. while(n!=0)
  59. {
  60. r=m%n;
  61. m=n;
  62. n=r;
  63. }
  64. if(y==m)
  65. cout<<x/m;
  66. else if(x/m<0&&y/m<0)
  67. cout<<(-1)*x/m<<'/'<<(-1)*y/m;
  68. else if(y/m<0)
  69. cout<<-x/m<<'/'<<-y/m;
  70. else
  71. cout<<x/m<<'/'<<y/m;
  72. }
  73.  
  74. int main()
  75. {
  76. Fraction f1,f2,f3;
  77. while(cin>>f1>>f2)
  78. {
  79. if(f1==0&&f2==0)
  80. break;
  81. f3=f1-f2;
  82. f3.output();
  83. cout<<" ";
  84. f3=f1/f2;
  85. f3.output();
  86. cout<<endl;
  87. }
  88. return 0;
  89. }

(2)函数版

  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int gcd(int d,int n);
  5. class Fraction
  6. {
  7. private:
  8. double deno; //分母
  9. double nume;//分子
  10. public:
  11. Fraction();
  12. Fraction operator-(Fraction &f);
  13. Fraction operator/(Fraction &f);
  14. friend istream & operator>>(istream &input,Fraction &f);
  15. bool operator==(int c);
  16. void output();
  17. void simplify();
  18. };
  19. void Fraction::output()
  20. {
  21. if(nume/deno==int(nume/deno))
  22. cout<<nume/deno;
  23. else
  24. {
  25. if(nume>0&&deno<0)
  26. {
  27. nume=-nume;
  28. deno=-deno;
  29. }
  30. else
  31. {
  32. nume=nume;
  33. deno=deno;
  34. }
  35. cout<<nume<<"/"<<deno;
  36. }
  37.  
  38. }
  39. bool Fraction::operator==(int c)
  40. {
  41. if(nume==0&&c==0)
  42. return true;
  43. else return false;
  44. }
  45.  
  46. istream & operator>>(istream &input,Fraction &f)
  47. {
  48. input>>f.nume>>f.deno;
  49. return input;
  50. }
  51. void Fraction::simplify()
  52. {
  53. int n=gcd(deno, nume);
  54. deno/=n; // 化简
  55. nume/=n;
  56. }
  57. Fraction::Fraction()
  58. {
  59. deno=0;
  60. nume=1;
  61. }
  62. Fraction Fraction:: operator-(Fraction &c)
  63. {
  64. Fraction t;
  65. t.nume=nume*c.deno-c.nume*deno;
  66. t.deno=deno*c.deno;
  67. t.simplify();
  68. return t;
  69. }
  70. Fraction Fraction:: operator/(Fraction &c)
  71. {
  72. Fraction t;
  73. if (!c.nume) return *this; //除法无效时,这样的情况须要考虑。但这样的处理仍不算合理
  74. t.nume=nume*c.deno;
  75. t.deno=deno*c.nume;
  76. t.simplify();
  77. return t;
  78. }
  79. int gcd(int m, int n) //这个函数能够定义为类的成员函数,也能够为一般函数
  80. {
  81. int r;
  82. if (m==0)
  83. {
  84. return n;
  85. }
  86. while(r=m%n) // 求m,n的最大公约数
  87. {
  88. m=n;
  89. n=r;
  90. }
  91. return n;
  92. }
  93. int main()
  94.  
  95. {
  96.  
  97. Fraction f1,f2,f3;
  98.  
  99. while(cin>>f1>>f2) {
  100.  
  101. if(f1==0&&f2==0)
  102.  
  103. break;
  104.  
  105. f3=f1-f2;
  106.  
  107. f3.output();
  108. cout<<' ';
  109. f3=f1/f2;
  110.  
  111. f3.output();
  112.  
  113. cout<<endl;
  114.  
  115. }
  116.  
  117. return 0;
  118.  
  119. }
  1. /* All rights reserved.
  2. * 文件名:test.cpp
  3. * 作者:陈丹妮
  4. * 完毕日期:2015年 7 月 2 日
  5. * 版 本 号:v1.0
  6. */
  7. #include <iostream>
  8. using namespace std;
  9. class Fraction
  10. {
  11. private:
  12. int x;
  13. int y;
  14. int e;
  15. public:
  16. Fraction(int a=0,int b=1):x(a),y(b)
  17. {
  18. e=0;
  19. }
  20. friend istream& operator>>(istream& input,Fraction &f);
  21. bool operator ==(int a);
  22. void output();
  23. Fraction operator -(Fraction &f1);
  24. Fraction operator /(Fraction &f1);
  25. };
  26. istream& operator>>(istream& input,Fraction &f)
  27. {
  28. input>>f.x>>f.y;
  29. return input;
  30. }
  31. bool Fraction::operator==(int a)
  32. {
  33. if(x==a)
  34. return true;
  35. else
  36. return false;
  37. }
  38. Fraction Fraction::operator -(Fraction &f1)
  39. {
  40. Fraction f;
  41. f.x=x*f1.y-f1.x*y;
  42. f.y=y*f1.y;
  43.  
  44. return f;
  45.  
  46. }
  47. Fraction Fraction::operator /(Fraction &f1)
  48. {
  49. Fraction f;
  50. if (!f1.x) return *this;
  51. f.x=x*f1.y;
  52. f.y=y*f1.x;
  53. return f;
  54. }
  55. void Fraction::output()
  56. {
  57. int r,m=x,n=y,t;
  58. if(m<n)
  59. {
  60. t=n;
  61. n=m;
  62. n=t;
  63. }
  64. while(n!=0)
  65. {
  66. r=m%n;
  67. m=n;
  68. n=r;
  69. }
  70. if(y==m)
  71. cout<<x/m;
  72. else if(x/m<0&&y/m<0)
  73. cout<<-x/m<<'/'<<-y/m;
  74. else if(y/m<0)
  75. cout<<-x/m<<'/'<<-y/m;
  76. else
  77. cout<<x/m<<'/'<<y/m;
  78. if(e==0)
  79. cout<<" ";
  80. e++;
  81. }
  82.  
  83. int main()
  84. {
  85. Fraction f1,f2,f3;
  86. while(cin>>f1>>f2)
  87. {
  88. if(f1==0&&f2==0)
  89. break;
  90. f3=f1-f2;
  91. f3.output();
  92. f3=f1/f2;
  93. f3.output();
  94. cout<<endl;
  95. }
  96. return 0;
  97. }

学习心得:这道题提交了非常多次。原来就是由于没有考虑分子为0的情况,导致错误。前两个都AC对了,最后一个没有AC可是,编译时都是对的,就是不知道格式是哪里出错了。希望有人能帮我指出来,谢谢,继续努力吧!!

第十七周oj刷题——Problem B: 分数类的四则运算【C++】的更多相关文章

  1. 第十六周oj刷题——Problem I: 改错题:类中私有成员的訪问

    Description 改错题: 设计一个日期类和时间类,并编写全局函数display用于显示日期和时间. 要求:display函数作为类外的普通函数,而不是成员函数 在主函数中调用display函数 ...

  2. 第十六周oj刷题——Problem E: B 构造函数和析构函数

    Description 在建立类对象时系统自己主动该类的构造函数完毕对象的初始化工作, 当类对象生命周期结束时,系统在释放对象空间之前自己主动调用析构函数. 此题要求: 依据主程序(main函数)和程 ...

  3. 第十六周oj刷题——Problem J: 填空题:静态成员---计算学生个数

    Description 学生类声明已经给出.在主程序中依据输入信息输出实际建立的学生对象个数,以及全部学生对象的成绩总和. Input 学生个数 相应学生个数的学生信息(姓名    年龄    成绩) ...

  4. 第十五周oj刷题——Problem M: C++习题 矩阵求和--重载运算符

    Description 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运算符"+",使之能用于矩阵相加(如c=a+b). 重载流插入运算符"<<&quo ...

  5. Sublime Text3 配置C++(附oj刷题常用模板)

    # 下载对应平台的sublime sublime最新版下载, 字体样式个人喜欢Consolas, 另附注册码: -– BEGIN LICENSE -– TwitterInc 200 User Lice ...

  6. Problem E: 分数类的输出

    Problem E: 分数类的输出 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2699  Solved: 1227[Submit][Status][ ...

  7. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  8. 小米OJ刷题日志

    虽然这OJ上的题比较水,但还是挺有意思的.关键是能赚钱 特别是提交方式 居然不支持C++,垃圾OJ 4. 最长连续数列 排序后dp 5. 找出旋转有序数列的中间值 写个排序就做完了. 6. 交叉队列 ...

  9. 九度OJ刷题报告

    从8月初到现在,已经刷了400道题,越到后面题目越难,但仍会继续努力. 现将自己所AC的代码贴到博客上整理,同时供大家交流参考. 所有代码均为本人独立完成,全部采用C语言进行编写.

随机推荐

  1. C/C++ 用libcurl库进行http通讯网络编程

    C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...

  2. Android stagefright与opencore对比

      [转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...

  3. 为Spring添加REST功能

    1 关于REST 我的理解,REST就是将资源以最合适的形式在服务端和客户端之间传递. 系统中资源采用URL进行标识(可以理解为URL路径中带参数) 使用HTTP方法进行资源的管理(GET,PUT,P ...

  4. c++适配器模式

    你想使用一个已经存在的类,而它的接口不符合你的需求. 创建一个类需要和其他类协同完成任务,需要一个适配器将其他类的方法都转接到适配器当中 什么是适配器模式:有一个目标客户类想适用已经存在类的接口,但是 ...

  5. Hadoop学习之编译eclipse插件

    近期准备開始学习Hadoop1.2.1的源码,感觉最好的方法还是能够在运行Hadoop及hadoop作业时跟踪调试代码的实际运行情况.因为选择的IDE为eclipse,所以准备编译一下hadoop的e ...

  6. Mustache学习

    Mustache是基于JavaScript的一款模版Web引擎,Web 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,通常是标准的 HTML 文档. 一.Musta ...

  7. 相比于python2.6,python3.0的新特性。

    这篇文章主要介绍了相比于python2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动. ...

  8. Linux(CentOS)安装配置zeromq、jzmq(解决各种问题)

    今天为Hadoop配置zeromq.jzmq遇到各种问题,先是编译出错,到编译成功后测试出错等等,下面将我遇到的问题与大家分享一下. 第一个注意点是:必须先编译安装zeromq,然后在编译jzmq,否 ...

  9. Web 应用性能提升 10 倍的 10 个建议

    转载自http://blog.jobbole.com/94962/ 提升 Web 应用的性能变得越来越重要.线上经济活动的份额持续增长,当前发达世界中 5 % 的经济发生在互联网上(查看下面资源的统计 ...

  10. C语言栈的实现

    栈是常用的数据结构之一,下面给出一个链式栈的实现~~头文件Stack.h #ifndef Stack_H #define Stack_H typedef int Item; typedef struc ...