千家信息网

C++有理数表达和计算的示例分析

发表于:2024-12-13 作者:千家信息网编辑
千家信息网最后更新 2024年12月13日,小编给大家分享一下C++有理数表达和计算的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!#ifndef Ratio
千家信息网最后更新 2024年12月13日C++有理数表达和计算的示例分析

小编给大家分享一下C++有理数表达和计算的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

#ifndef Rational_hpp#define Rational_hpp#include #include 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 #include 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<1) {        ss<<"/"<

简单测试

#include #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<

测试结果

以上是"C++有理数表达和计算的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0