C++ | 分数的加减乘除通分约分比较大小求倒数(优化版)

#include<cmath>
#include<iostream>
using namespace std;
int gcd(int,int); //求最大公约数
class fraction{
    float above;
    float below;
public:
    void reduction();
    void makeCommond(fraction &);
    fraction(int=0,int=1);
    fraction add(fraction);
    fraction sub(fraction);
    fraction mul(fraction);
    fraction div(fraction);
    fraction reciprocal();
    bool equal(fraction );
    bool greaterThan(fraction );
    bool lessThan(fraction );
    void display();
    void input();
    void input(int, int);
};

//构造函数

fraction::fraction(int a, int b){
    above = a;
    below = b;
}

//通分

void fraction::makeCommond(fraction & f){
int max, i;
int a = below, b = f.below;
max = gcd(below, f.below); //最大公约数 
if(below<f.below){
    below *= b/max;
    above *= b/max;
    f.below *= a/max;
    f.above *= a/max;
}
else{
    below *= b/max;
    above *= b/max;
    f.below *= a/max;
    f.above *= a/max;
}
}

//约分

// 假如分母<0,将负号转移到分子
void fraction::reduction(){
    if(below<0){
        above = -above;
        below = -below; 
    }
    if(above==0){
        below=1;
    }else{
        int d=gcd(abs(above),abs(below));
        above/=d;
        below/=d;
    }
}

//加法

fraction fraction::add(fraction f){
    fraction r(above*f.below + below*f.above,below*f.below);
    r.reduction(); 
    return r;
}

//减法

fraction fraction::sub(fraction f){
    fraction r(above*f.below - below*f.above,below*f.below);
    r.reduction();
    return r;
}

//乘法

fraction fraction::mul(fraction f){
    fraction r(above*f.above,below*f.below);
    r.reduction();
    return r;
}

//除法

fraction fraction::div(fraction f){
    f = f.reciprocal();
    this->mul(f);
    
}

//求倒数

fraction fraction::reciprocal(){
    if(above!=0){
        fraction r(below, above);
        r.reduction();
        return r;   
    //分母为零,报错 
    }else{
        cout<<"Wrong! The below can not be a zero!\nAnd the operation was cancelled!"<<endl;
        fraction r(above, below);
        r.reduction();
        return r;
    }
}

//比较

// 先通分再比较分子  无法通过求分数具体大小比较,因为分子分母是int型 
bool fraction::equal(fraction f){
    this->makeCommond(f);
    if(above==f.above)
        return true;
    return false;
}
bool fraction::greaterThan(fraction f){
    this->makeCommond(f);
    if(above>f.above)
        return true;
    return false;
}
bool fraction::lessThan(fraction f){
    this->makeCommond(f);
    if(above<f.above)
        return true;
    return false;
}

//显示、输出

void fraction::display(){
    cout<<above<<"/"<<below<<endl;
}

//输入、构造一个分数

void fraction::input(){
    int b;
    cout<<"请分别输入分子和分母:"<<endl;
    cin>>above;
    cin>>b;
    if(b==0){
        cout<<"分母不能为0,请重新输入分母。"<<endl;
        do{
        cin>>b;
        }while(!b);
    }
    below=b;
    reduction();
}

//直接传入参数构造分数

void fraction::input(int a, int b){
    above = a;
    below = b;
}

//最大公约数(递归)

int gcd(int a,int b){
    return b>0 ? gcd(b,a%b):a;
}

//主函数

int main(){
    fraction f1, f2, f3, f4, f5, f6, f7, f8;
    f1.input(); 
    cout << "f1 = " ; 
    f1.display();
    f2.input(); 
    cout << "f2 = " ; 
    f2.display();
    cout<<"四则运算结果:"<<endl;
    f3 = f1.add(f2); cout<<"f1 + f2 = ";
    f3.display();
    f4 = f1.sub(f2); cout<<"f1 - f2 = ";
    f4.display();
    f5 = f1.mul(f2); cout<<"f1 * f2 = ";
    f5.display();
    f6 = f1.div(f2); cout<<"f1 / f2 = ";
    f6.display();
    f7 = f1.reciprocal(); cout<<"f1的倒数: ";
    f7.display();

    if(f1.equal(f2))cout<<"f1等于f2"<<endl;
    if(f1.greaterThan(f2))cout<<"f1大于f2"<<endl;
    if(f1.lessThan(f2))cout<<"f1小于f2"<<endl;
    cout << "f1 和 f2 的通分"<<endl ;
    f1.makeCommond(f2);
    cout << "f1 = " ; f1.display();
    cout << "f2 = " ; f2.display(); 
    cout << "f2 的 约分" << endl;
    f2.reduction();
    cout << "f2 = ";
    f2.display(); 
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容