Example Analysis of C++ rational number expression and calculation
The editor would like to share with you the example analysis of C++ rational number expression and calculation. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's take a look at it!
# 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 npenint 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