欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

浅谈C++有理数的表达和计算

时间:2022-09-26 09:35:02|栏目:C代码|点击:
#ifndef Rational_hpp
#define Rational_hpp
#include <stdio.h>
#include <string>
using namespace std;
class Radtional{
public:
    Radtional();
    Radtional(int numerator,int denominator);
    int getNumerator()const;
    int getDemominator()const;
    Radtional add(const Radtional& secondRational);
    Radtional sub(const Radtional& secondRational);
    Radtional mult(const Radtional& secondRational);
    Radtional div(const Radtional& secondRational);
    int compareTo(const Radtional& secondRational);
    bool equals(const Radtional& secondRational);
    int intValue();
    double doubleValue();
    string toString();
private:
    int numerator,denominator;
    static int gcd(int n,int d);
};
#endif /* Rational_hpp */
#include "Rational.hpp"
#include <sstream>
#include <cstdlib>
Radtional::Radtional(){
    numerator = 0;
    denominator = 1;
}
Radtional::Radtional(int numerator,int denominator)
{
    int factor = gcd(numerator, denominator);
    this->numerator = ((numerator>0) ? 1: -1)*abs(numerator)/factor;
    this->denominator = abs(denominator)/factor;
}
int Radtional::getNumerator()const{
    return numerator;
}
int Radtional::getDemominator()const{
    return denominator;
}
int Radtional::gcd(int n, int d){
    int n1 = abs(n);
    int n2 = abs(d);
    int gcd =1;
    for (int i = 1; i<=n1&&i<=n2; i++) {
        if (n1%i==0&&n2%i==0) {
            gcd = i;
        }
    }
    return gcd;
}
Radtional Radtional::add(const Radtional& secondRational){
    int n = numerator*secondRational.getDemominator()+denominator*secondRational.getNumerator();
    int d = denominator * secondRational.getDemominator();
    return Radtional(n,d);
}
Radtional Radtional::sub(const Radtional &secondRational){
    int n = numerator * secondRational.getDemominator() - denominator* secondRational.getNumerator();
    int d = denominator * secondRational.getDemominator();
    return Radtional(n,d);
}
Radtional Radtional::mult(const Radtional &secondRational){
    int n = numerator * secondRational.getNumerator();
    int d = denominator * secondRational.getDemominator();
    return Radtional(n,d);
}
Radtional Radtional::div(const Radtional &secondRational){
    int n =numerator * secondRational.getDemominator();
    int d =denominator * secondRational.getNumerator();
    return Radtional(n,d);
}
int Radtional::compareTo(const Radtional &secondRational){
    Radtional temp =sub(secondRational);//两值相减
    if (temp.getNumerator()<0) {
        return -1;
    }
    else if (temp.getNumerator()==0){
        return 0;
    }
    else
        return 1;
}
bool Radtional::equals(const Radtional &secondRational){
    if (compareTo(secondRational)==0) {
        return true;
    }
    else
        return false;
}
int Radtional::intValue(){
    return getNumerator()/getDemominator();
}
double Radtional::doubleValue(){
    return 1.0*getNumerator()/getDemominator();
}
string Radtional::toString(){
    stringstream ss;
    ss<<numerator;
    if (denominator>1) {
        ss<<"/"<<denominator;
    }
    return ss.str();
}

简单测试

#include <iostream>
#include "Rational.hpp"
int main(int argc, const char * argv[]) {
    // insert code here...
    Radtional a(3,2);
    Radtional b(2,5);
    Radtional c(1,3);
    Radtional d(2,6);
    cout<<a.toString()<<" + "<<b.toString()<<"="<<(a.add(b)).toString()<<endl;
    cout<<a.toString()<<" - "<<b.toString()<<"="<<(a.sub(b)).toString()<<endl;
    cout<<a.toString()<<" * "<<b.toString()<<"="<<(a.mult(b)).toString()<<endl;
    cout<<a.toString()<<" / "<<b.toString()<<"="<<(a.div(b)).toString()<<endl;
    cout<<c.toString()<<" - "<<a.toString()<<"="<<(c.sub(a)).toString()<<endl;
    cout<<c.toString()<<"的int转换为"<<c.intValue()<<endl;
    cout<<c.toString()<<"的double转换为"<<c.doubleValue()<<endl;
    cout<<c.toString()<<"和"<<a.toString()<<"比较结果"<<c.compareTo(a)<<endl;
    cout<<c.toString()<<"和"<<d.toString()<<"比较结果"<<c.compareTo(d)<<endl;
    cout<<b.toString()<<"和"<<a.toString()<<"比较结果"<<b.compareTo(a)<<endl;
    return 0;
}

测试结果

总结

上一篇:C语言超详细讲解数据结构中的线性表

栏    目:C代码

下一篇:Qt模仿实现文字浮动字母的效果

本文标题:浅谈C++有理数的表达和计算

本文地址:http://www.codeinn.net/misctech/214725.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有