一个有理数类(C++作业)

Augustus
• 阅读 260

头文件:

#ifndef Rational_H
#define Rational_H
class Rational
{
private:
    int numerator;
    int denominator;
public:
    Rational();
    Rational(int , int );
    Rational simplification(Rational&);
    Rational addition(Rational& , Rational& );
    Rational subtraction(Rational& , Rational& );
    Rational multiplication(Rational& a, Rational& b);
    Rational division(Rational& a, Rational& b);
    void showvalue1();//分数形式展现
    void showvalue2();//有理数形式展现
};
#endif

定义类的文件:

#include<math.h>
Rational::Rational() :numerator(1), denominator(1)
{
}
Rational::Rational(int a, int b):numerator(a), denominator(b)
{
    Rational::simplification(*this);
}
Rational Rational::simplification(Rational&a)
{
    int mini = std::min(numerator, denominator);
    for (int i = mini; i > 0; i--)
    {
        if (numerator % i == 0 && denominator % i == 0)
        {
            numerator /= i;
            denominator /= i;
            break;
        }
    }
    return a;
}
Rational Rational::addition(Rational&a, Rational&b)
{
    this->numerator = a.numerator * b.denominator + a.denominator * b.numerator;
    this->denominator = a.denominator * b.denominator;
    Rational::simplification(*this);
    return *this;
}
Rational Rational::subtraction(Rational& a, Rational& b)
{
    this->numerator = a.numerator * b.denominator - a.denominator * b.numerator;
    this->denominator = a.denominator * b.denominator;
    Rational::simplification(*this);
    return *this;
}
Rational Rational::multiplication(Rational& a, Rational& b)
{
    this->numerator = a.numerator * b.numerator;
    this->denominator = a.denominator * b.denominator;
    Rational::simplification(*this);
    return *this;
}
Rational Rational::division(Rational& a, Rational& b)
{
    this->numerator = a.numerator * b.denominator;
    this->denominator = a.denominator * b.numerator;
    Rational::simplification(*this);
    return *this;
}
void Rational::showvalue1()
{
    std::cout << numerator << "/" << denominator<<std::endl;
}
void Rational::showvalue2()
{
    double value = (double)numerator / denominator;
    std::cout << value << std::endl;
}

一个demo:

#include"head.h"
int main()
{
    //测试
    Rational x(16, 12);
    Rational y(4, 9);
    Rational a1;
    Rational a2;
    Rational a3;
    Rational a4;
    a1.addition(x, y);
    a2.subtraction(x, y);
    a3.multiplication(x, y);
    a4.division(x, y);
    a1.showvalue1();
    a2.showvalue1();
    a3.showvalue1();
    a4.showvalue1();
    a1.showvalue2();
    a2.showvalue2();
    a3.showvalue2();
    a4.showvalue2();
}
点赞
收藏
评论区
推荐文章
Stella981 Stella981
3年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Stella981 Stella981
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
C++ string类
http://blog.csdn.net/pipisorry/article/details/36869741(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Fpipisorry%2Farticle%2Fdetails%2F36869741)使用标准
Easter79 Easter79
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Easter79 Easter79
3年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Wesley13 Wesley13
3年前
C++:模板类
22.模板类22.1模板类模板是泛型编程的基础,那什么是泛型编程呢?泛型编程是一种独立于任何特定数据类型编写代码的方式。C标准模板库中的数据容器、迭代器和算法,都是泛型编程的例子,它们都使用了模板的概念。比如动态数组vector是可以存放任何类型数据的容器,我们可以定义许多不同类型的vector,比如vector<int或vect
Stella981 Stella981
3年前
Scala函数式对象
有理数类的表示实现规范:支持有理数的加减乘除,并支持有理数的规范表示1.定义Rational首先,考虑用户如何使用这个类,我们已经决定使用“Immutable”方式来使用Rational对象,我们需要用户在定义Rational对象时提供分子和分母。classRation
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Augustus
Augustus
Lv1
计算机专业学生
文章
3
粉丝
2
获赞
2