第十七周oj刷题——Problem B: 分数类的四则运算【C++】
Description
Input
Output
Sample Input
- 1 2 -1 2
- 4 3 3 4
- 0 0 0 0
Sample Output
- 1 -1
- 7/12 16/9
(1)普通版。
- /* All rights reserved.
- * 文件名:test.cpp
- * 作者:陈丹妮
- * 完毕日期:2015年 7 月 2 日
- * 版 本 号:v1.0
- */
- #include <iostream>
- using namespace std;
- class Fraction
- {
- private:
- int x;
- int y;
- public:
- Fraction(int a=0,int b=1):x(a),y(b) {}
- friend istream& operator>>(istream& input,Fraction &f);
- bool operator ==(int a);
- void output();
- Fraction operator -(Fraction &f1);
- Fraction operator /(Fraction &f1);
- };
- istream& operator>>(istream& input,Fraction &f)
- {
- input>>f.x>>f.y;
- return input;
- }
- bool Fraction::operator==(int a)
- {
- if(x==a)
- return true;
- else
- return false;
- }
- Fraction Fraction::operator -(Fraction &f1)
- {
- Fraction f;
- f.x=x*f1.y-f1.x*y;
- f.y=y*f1.y;
- return f;
- }
- Fraction Fraction::operator /(Fraction &f1)
- {
- Fraction f;
- if(!f1.x) return *this;
- f.x=x*f1.y;
- f.y=y*f1.x;
- return f;
- }
- void Fraction::output()
- {
- int r,m=x,n=y,t;
- if(m<n)
- {
- t=n;
- n=m;
- n=t;
- }
- while(n!=0)
- {
- r=m%n;
- m=n;
- n=r;
- }
- if(y==m)
- cout<<x/m;
- else if(x/m<0&&y/m<0)
- cout<<(-1)*x/m<<'/'<<(-1)*y/m;
- else if(y/m<0)
- cout<<-x/m<<'/'<<-y/m;
- else
- cout<<x/m<<'/'<<y/m;
- }
- int main()
- {
- Fraction f1,f2,f3;
- while(cin>>f1>>f2)
- {
- if(f1==0&&f2==0)
- break;
- f3=f1-f2;
- f3.output();
- cout<<" ";
- f3=f1/f2;
- f3.output();
- cout<<endl;
- }
- return 0;
- }
(2)函数版
- #include <iostream>
- using namespace std;
- int gcd(int d,int n);
- class Fraction
- {
- private:
- double deno; //分母
- double nume;//分子
- public:
- Fraction();
- Fraction operator-(Fraction &f);
- Fraction operator/(Fraction &f);
- friend istream & operator>>(istream &input,Fraction &f);
- bool operator==(int c);
- void output();
- void simplify();
- };
- void Fraction::output()
- {
- if(nume/deno==int(nume/deno))
- cout<<nume/deno;
- else
- {
- if(nume>0&&deno<0)
- {
- nume=-nume;
- deno=-deno;
- }
- else
- {
- nume=nume;
- deno=deno;
- }
- cout<<nume<<"/"<<deno;
- }
- }
- bool Fraction::operator==(int c)
- {
- if(nume==0&&c==0)
- return true;
- else return false;
- }
- istream & operator>>(istream &input,Fraction &f)
- {
- input>>f.nume>>f.deno;
- return input;
- }
- void Fraction::simplify()
- {
- int n=gcd(deno, nume);
- deno/=n; // 化简
- nume/=n;
- }
- Fraction::Fraction()
- {
- deno=0;
- nume=1;
- }
- Fraction Fraction:: operator-(Fraction &c)
- {
- Fraction t;
- t.nume=nume*c.deno-c.nume*deno;
- t.deno=deno*c.deno;
- t.simplify();
- return t;
- }
- Fraction Fraction:: operator/(Fraction &c)
- {
- Fraction t;
- if (!c.nume) return *this; //除法无效时,这样的情况须要考虑。但这样的处理仍不算合理
- t.nume=nume*c.deno;
- t.deno=deno*c.nume;
- t.simplify();
- return t;
- }
- int gcd(int m, int n) //这个函数能够定义为类的成员函数,也能够为一般函数
- {
- int r;
- if (m==0)
- {
- return n;
- }
- while(r=m%n) // 求m,n的最大公约数
- {
- m=n;
- n=r;
- }
- return n;
- }
- int main()
- {
- Fraction f1,f2,f3;
- while(cin>>f1>>f2) {
- if(f1==0&&f2==0)
- break;
- f3=f1-f2;
- f3.output();
- cout<<' ';
- f3=f1/f2;
- f3.output();
- cout<<endl;
- }
- return 0;
- }
- /* All rights reserved.
- * 文件名:test.cpp
- * 作者:陈丹妮
- * 完毕日期:2015年 7 月 2 日
- * 版 本 号:v1.0
- */
- #include <iostream>
- using namespace std;
- class Fraction
- {
- private:
- int x;
- int y;
- int e;
- public:
- Fraction(int a=0,int b=1):x(a),y(b)
- {
- e=0;
- }
- friend istream& operator>>(istream& input,Fraction &f);
- bool operator ==(int a);
- void output();
- Fraction operator -(Fraction &f1);
- Fraction operator /(Fraction &f1);
- };
- istream& operator>>(istream& input,Fraction &f)
- {
- input>>f.x>>f.y;
- return input;
- }
- bool Fraction::operator==(int a)
- {
- if(x==a)
- return true;
- else
- return false;
- }
- Fraction Fraction::operator -(Fraction &f1)
- {
- Fraction f;
- f.x=x*f1.y-f1.x*y;
- f.y=y*f1.y;
- return f;
- }
- Fraction Fraction::operator /(Fraction &f1)
- {
- Fraction f;
- if (!f1.x) return *this;
- f.x=x*f1.y;
- f.y=y*f1.x;
- return f;
- }
- void Fraction::output()
- {
- int r,m=x,n=y,t;
- if(m<n)
- {
- t=n;
- n=m;
- n=t;
- }
- while(n!=0)
- {
- r=m%n;
- m=n;
- n=r;
- }
- if(y==m)
- cout<<x/m;
- else if(x/m<0&&y/m<0)
- cout<<-x/m<<'/'<<-y/m;
- else if(y/m<0)
- cout<<-x/m<<'/'<<-y/m;
- else
- cout<<x/m<<'/'<<y/m;
- if(e==0)
- cout<<" ";
- e++;
- }
- int main()
- {
- Fraction f1,f2,f3;
- while(cin>>f1>>f2)
- {
- if(f1==0&&f2==0)
- break;
- f3=f1-f2;
- f3.output();
- f3=f1/f2;
- f3.output();
- cout<<endl;
- }
- return 0;
- }
学习心得:这道题提交了非常多次。原来就是由于没有考虑分子为0的情况,导致错误。前两个都AC对了,最后一个没有AC可是,编译时都是对的,就是不知道格式是哪里出错了。希望有人能帮我指出来,谢谢,继续努力吧!!
!
第十七周oj刷题——Problem B: 分数类的四则运算【C++】的更多相关文章
- 第十六周oj刷题——Problem I: 改错题:类中私有成员的訪问
Description 改错题: 设计一个日期类和时间类,并编写全局函数display用于显示日期和时间. 要求:display函数作为类外的普通函数,而不是成员函数 在主函数中调用display函数 ...
- 第十六周oj刷题——Problem E: B 构造函数和析构函数
Description 在建立类对象时系统自己主动该类的构造函数完毕对象的初始化工作, 当类对象生命周期结束时,系统在释放对象空间之前自己主动调用析构函数. 此题要求: 依据主程序(main函数)和程 ...
- 第十六周oj刷题——Problem J: 填空题:静态成员---计算学生个数
Description 学生类声明已经给出.在主程序中依据输入信息输出实际建立的学生对象个数,以及全部学生对象的成绩总和. Input 学生个数 相应学生个数的学生信息(姓名 年龄 成绩) ...
- 第十五周oj刷题——Problem M: C++习题 矩阵求和--重载运算符
Description 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运算符"+",使之能用于矩阵相加(如c=a+b). 重载流插入运算符"<<&quo ...
- Sublime Text3 配置C++(附oj刷题常用模板)
# 下载对应平台的sublime sublime最新版下载, 字体样式个人喜欢Consolas, 另附注册码: -– BEGIN LICENSE -– TwitterInc 200 User Lice ...
- Problem E: 分数类的输出
Problem E: 分数类的输出 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2699 Solved: 1227[Submit][Status][ ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- 小米OJ刷题日志
虽然这OJ上的题比较水,但还是挺有意思的.关键是能赚钱 特别是提交方式 居然不支持C++,垃圾OJ 4. 最长连续数列 排序后dp 5. 找出旋转有序数列的中间值 写个排序就做完了. 6. 交叉队列 ...
- 九度OJ刷题报告
从8月初到现在,已经刷了400道题,越到后面题目越难,但仍会继续努力. 现将自己所AC的代码贴到博客上整理,同时供大家交流参考. 所有代码均为本人独立完成,全部采用C语言进行编写.
随机推荐
- C/C++ 用libcurl库进行http通讯网络编程
C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...
- Android stagefright与opencore对比
[转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...
- 为Spring添加REST功能
1 关于REST 我的理解,REST就是将资源以最合适的形式在服务端和客户端之间传递. 系统中资源采用URL进行标识(可以理解为URL路径中带参数) 使用HTTP方法进行资源的管理(GET,PUT,P ...
- c++适配器模式
你想使用一个已经存在的类,而它的接口不符合你的需求. 创建一个类需要和其他类协同完成任务,需要一个适配器将其他类的方法都转接到适配器当中 什么是适配器模式:有一个目标客户类想适用已经存在类的接口,但是 ...
- Hadoop学习之编译eclipse插件
近期准备開始学习Hadoop1.2.1的源码,感觉最好的方法还是能够在运行Hadoop及hadoop作业时跟踪调试代码的实际运行情况.因为选择的IDE为eclipse,所以准备编译一下hadoop的e ...
- Mustache学习
Mustache是基于JavaScript的一款模版Web引擎,Web 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,通常是标准的 HTML 文档. 一.Musta ...
- 相比于python2.6,python3.0的新特性。
这篇文章主要介绍了相比于python2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动. ...
- Linux(CentOS)安装配置zeromq、jzmq(解决各种问题)
今天为Hadoop配置zeromq.jzmq遇到各种问题,先是编译出错,到编译成功后测试出错等等,下面将我遇到的问题与大家分享一下. 第一个注意点是:必须先编译安装zeromq,然后在编译jzmq,否 ...
- Web 应用性能提升 10 倍的 10 个建议
转载自http://blog.jobbole.com/94962/ 提升 Web 应用的性能变得越来越重要.线上经济活动的份额持续增长,当前发达世界中 5 % 的经济发生在互联网上(查看下面资源的统计 ...
- C语言栈的实现
栈是常用的数据结构之一,下面给出一个链式栈的实现~~头文件Stack.h #ifndef Stack_H #define Stack_H typedef int Item; typedef struc ...