您好,登錄后才能下訂單哦!
小編給大家分享一下C++有理數(shù)表達(dá)和計(jì)算的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
#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轉(zhuǎn)換為"<<c.intValue()<<endl; cout<<c.toString()<<"的double轉(zhuǎn)換為"<<c.doubleValue()<<endl; cout<<c.toString()<<"和"<<a.toString()<<"比較結(jié)果"<<c.compareTo(a)<<endl; cout<<c.toString()<<"和"<<d.toString()<<"比較結(jié)果"<<c.compareTo(d)<<endl; cout<<b.toString()<<"和"<<a.toString()<<"比較結(jié)果"<<b.compareTo(a)<<endl; return 0; }
測試結(jié)果
以上是“C++有理數(shù)表達(dá)和計(jì)算的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。