首页 技术 正文
技术 2022年11月18日
0 收藏 443 点赞 3,024 浏览 1112 个字

Description

封装一个分数类Fract,用来处理分数功能和运算,支持以下操作: 1. 构造:传入两个参数n和m,表示n/m;分数在构造时立即转化成最简分数。2. show()函数:分数输出为“a/b”或“-a/b”的形式,a、b都是无符号整数。若a为0或b为1,只输出符号和分子,不输出“/”和分母。3. double类型转换函数:用分子除以分母,得到的小数。注意:分子为0时不要输出为“-0” —————————————————————————– 你设计一个Fract类,使得main()函数能够运行并得到正确的输出。调用格式见append.cc 

Input

输入多行,每行两个整数,分别为分子和分母,至EOF结束。输入的分母不会为0; 

Output

 每行输出一个实数和分数,与输入顺序一致。实数为分子除以分母所得。 分数输出时为最简形式,负号只会出现在最前面,若分母为1或分子为0,则只输出一个整数,即分子部分,而没有“/”和分母部分。 

Sample Input

1 3 20 -15 80 150 -9 1 6 6 12 16 -33 -48 6 11 0 -10

Sample Output

0.333333 1/3 -1.33333 -4/3 0.533333 8/15 -9 -9 1 1 0.75 3/4 0.6875 11/16 0.545455 6/11 0 0

HINT

 

Append Code

append.cappend.cc,

[Submit][Status][Web Board]

#include <bits/stdc++.h>using namespace std;int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}class Fract{public:    int n,m;    Fract(int a=0,int b=0):n(a),m(b){}    void show()    {        int t=gcd(n,m);        int x=n/t;        int y=m/t;        if(x==0||y==1)            cout<<x<<endl;        else        {            if(x*y<0)                cout<<"-"<<abs(x)<<"/"<<abs(y)<<endl;            else                cout<<abs(x)<<"/"<<abs(y)<<endl;        }    }    operator double ()    {        if(n==0)            return (double)0;        else            return (double)n/m;    }};int main(){    int n, m;    while(cin >> n >> m)    {        Fract fr(n, m);        cout << (double)fr << " ";        fr.show();    }}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918