Description

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

Input

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

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++】的更多相关文章

  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. javascript函数值的重写

    原文:javascript函数值的重写 javascript函数值的重写 定义了一个函数,需要重写这个函数并使用原先的函数值.做法是: 1.定义一个变量让原先函数的值指向它,把原先函数的指向一个新的函 ...

  2. 矩阵转置 O(1)空间

    题目:用O(1)的空间实现矩阵的转置 为了方便,使用一维数组来分析.所谓矩阵转置,行变列,列变行.在转置的过程中,有的元素位置是不变的:对于变化位置的元素,要求O(1)空间完成,那么这些位置的变化一定 ...

  3. java--工具方法

    根据时间戳得到具体的时间: public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat(& ...

  4. C语言入门(21)——使用DBG对C语言进行调试

    C语言入门(21)--使用DBG对C语言进行调试 程序中除了一目了然的Bug之外都需要一定的调试手段来分析到底错在哪.到目前为止我们的调试手段只有一种:根据程序执行时的出错现象假设错误原因,然后在代码 ...

  5. Android UI SurfaceView的使用-绘制单个图型或多个图形

    新建MyView类继承自SurfaceView: public class MyView extends SurfaceView implements SurfaceHolder.Callback { ...

  6. I NEED A OFFER!

    I NEED A OFFER! Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  7. Bone Collector(01背包+记忆化搜索)

    Bone Collector Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  8. gcc中-pthread和-lpthread的区别

    最近在使用linux mint15,里面自带的gcc时4.7的,当我编译多线程程序时,使用-lpthread居然说没有找到线程库函数!!!然后man了一下,才发现在gcc 4.7中链接线程库使用-pt ...

  9. C#中使用日志类,添加dll时出现错误

    警告 1 未能解析引用的程序集 “log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, proces ...

  10. android监听键盘

    android中的带有输入功能的页面布局经常被弹出的键盘遮挡,一种处理方法是监听键盘的弹出,设置布局的padding或隐藏某些占位控件,使得输入框不被键盘遮挡.一种常用的方法是当Activity设置为 ...