山东科技大学OJ Problem H: 分数类之一

HomeProblemSetStandingStatusStatistics

Problem H: 分数类之一

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 812  Solved: 335
[Submit][Status]

Description

一个分数类(Fraction)对象由两部分组成,分别是分子(numerator)和分母(denominator)。分子和分母均为整数类(Integer)对象,用以存储一个整型数值。

         用C++编写Fraction类和Integer类来完成代码,调用格式见“Append Code”。

Fraction::numerator()取得分子数值。

Fraction:: denominator ()取得分母数值。

Integer::value()取得整数值。

         Fraction类和Integer类的构造函数,根据题意设计。

Input

输入为多组。每组包括分子分母两个整数。

Output

按格式输出分数。

Sample Input

3 5 5 3

Sample Output

Integer 3 construction. Integer 5 construction. Integer 3 duplicate. Integer 5 duplicate. Fraction 3/5 construction. 3/5=0.6 Fraction 3/5 destruction. Integer 5 destruction. Integer 3 destruction. Integer 5 destruction. Integer 3 destruction. Integer 5 construction. Integer 3 construction. Integer 5 duplicate. Integer 3 duplicate. Fraction 5/3 construction. 5/3=1.66667 Fraction 5/3 destruction. Integer 3 destruction. Integer 5 destruction. Integer 3 destruction. Integer 5 destruction.

HINT

Append Code

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Integer
{public:
    int x;
    Integer(){}
    Integer(int nn):x(nn){
        cout<<"Integer "<<x<<" construction."<<endl;
    }
    Integer(Integer& dd){
        this->x=dd.x;
        cout<<"Integer "<<x<<" duplicate."<<endl;
    }
    int value()
    {return x;
    }
    ~Integer(){
        cout<<"Integer "<<x<<" destruction."<<endl;
    }
};
class Fraction
{public:
    Integer n,d;//分子、分母 
    Fraction(Integer& nn,Integer& dd):n(nn),d(dd){
        cout<<"Fraction "<<n.x<<"/"<<d.x<<" construction."<<endl;
    }
    Integer& numerator(){
        return n;
    }
    Integer& denominator(){
        return d;
    }
    double value()
    {return (double)1.0*n.x/d.x;
    }
    ~Fraction(){
        cout<<"Fraction "<<n.x<<"/"<<d.x<<" destruction."<<endl;
    }
};
 
int main()
{
    int n, d;
    while(cin >> n >> d)
    {
        Integer num(n), den(d);
        Fraction f(num, den);
        cout << f.numerator().value() << "/" << f.denominator().value() << "=" << f.value() << endl;
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值