bool operator==(Rational const & a, Rational const & b) { if (a.getNumerator() != b.getNumerator()) return false; if (b.getDenominator() != b.getDenominator()) return false; return true; }
Rational Rational::add(Rational &secondRational) { cout<<numerator<<"vjnfiehb"<<endl; long n=numerator*secondRational.getDenominator()+denominator*secondRational.getNumerator(); long d=denominator*secondRational.getDenominator(); //cout<<"n = "<<n<<' '<<"d = "<<d<<endl; return Rational(n, d); }
Rational operator+(Rational const & a, Rational const & b) { Rational rat; rat.setNumerator((a.getNumerator()*b.getDenominator()) + (b.getNumerator() * a.getDenominator())); rat.setDenominator(a.getDenominator() * b.getDenominator()); return rat; }
Numeric* Integer::operator /(Numeric* c)const{ Integer* entier = dynamic_cast<Integer *>(c); if(entier==NULL) { Real* reel=dynamic_cast<Real*>(c); if(reel==NULL){ Rational *rationnel=dynamic_cast<Rational *>(c); Rational *result=new Rational(Integer(_value*rationnel->getDenominator().getValue()),Integer(rationnel->getNumerator().getValue())); result->simplification(); if(result->getDenominator().getValue()==1 || result->getNumerator().getValue()==0){ Integer* resE = new Integer(result->getNumerator().getValue()); delete result; return resE; } return result; } if(reel->getValue() ==0) throw QString("Can't divide by 0 !"); return new Real(((double)_value)/reel->getValue()); } if(entier->getValue() ==0) throw QString("Can't divide by 0 !"); Rational* res = new Rational(*this,*entier); res->simplification(); if(res->getDenominator().getValue()==1 || res->getNumerator().getValue()==0){ Integer* resE = new Integer(res->getNumerator().getValue()); delete res; return resE; } return res; }
void TheoryArith::printRational(ExprStream& os, const Rational& r, bool printAsReal) { // Print rational if (r.isInteger()) { if (r < 0) { if (os.lang() == SPASS_LANG) { os << "-" << (-r).toString(); if (printAsReal) os << ".0"; } else { os << "(" << push; if (os.lang() == SMTLIB_LANG) { os << "~"; } else { os << "-"; } os << space << (-r).toString(); if (printAsReal) os << ".0"; os << push << ")"; } } else { os << r.toString(); if (printAsReal) os << ".0"; } } else { os << "(" << push << "/ "; Rational tmp = r.getNumerator(); if (tmp < 0) { if (os.lang() == SPASS_LANG) { os << "-" << (-tmp).toString(); if (printAsReal) os << ".0"; } else { os << "(" << push; if (os.lang() == SMTLIB_LANG) { os << "~"; } else { os << "-"; } os << space << (-tmp).toString(); if (printAsReal) os << ".0"; os << push << ")"; } } else { os << tmp.toString(); if (printAsReal) os << ".0"; } os << space; tmp = r.getDenominator(); DebugAssert(tmp > 0 && tmp.isInteger(), "Unexpected rational denominator"); os << tmp.toString(); if (printAsReal) os << ".0"; os << push << ")"; } }
Timestamp::Timestamp(uint s, uint frames, const Rational &newFramerate) { assert(newFramerate > 0); uint fr; if (newFramerate.getDenominator() == 1) { fr = newFramerate.getNumerator(); } else { Rational time = newFramerate.getInverse() * frames; frames = time.getNumerator(); fr = time.getDenominator(); } _secs = s + (frames / fr); _framerateFactor = 1000 / gcd<uint>(1000, fr); _framerate = fr * _framerateFactor; _numFrames = (frames % fr) * _framerateFactor; }
void Expression::simplify(){ this->coefficent->simplify(); Number* common = this->expr[1]->getCoefficient(); int count; int opCount; for (int j = 0; j < this->expr.size() - 1; j++){ count = 0; opCount = 0; Rational* rat = new Rational(common, expr[j]->getCoefficient()); rat->simplify(); if (rat->getDenominator()->getType() == "Integer"){ Integer* den = dynamic_cast<Integer*>(rat->getDenominator()); if (den->getValue() != 1){ break; } } else if (rat->getDenominator()->getType() == "Operator"){ opCount++; } else{ count++; } if (opCount + count == this->expr.size()){ this->coefficent = common; for (int j = 0; j < this->expr.size() - 1; j++){ Rational* rat = new Rational(this->expr[j]->getCoefficient(), common); rat->simplify(); this->expr[j]->setCoefficient(rat); } } } for (int i = 0; i < this->expr.size(); i++){ if (this->expr[i]->getType() == "Operator"){ Operator* opera = dynamic_cast<Operator*>(this->expr[i]); if (opera->getOperator() == "-"){ this->expr[i] = new Operator("+"); Multiply mult = Multiply(); this->expr[i + 1] = mult.evaluate(this->expr[i + 1], new Integer(-1)); } this->expr[i]->simplify(); } } }
int main() { Rational oneEighth(1, 8); Rational oneHalf(1,2); Rational result = oneEighth * oneHalf; cout << result.getNumerator() << " " << result.getDenominator() << endl; result = result * oneEighth; cout << result.getNumerator() << " " << result.getDenominator() << endl; result = oneHalf * 2;//int转换成了Ratinal对象了 cout << result.getNumerator() << " " << result.getDenominator() << endl; result = 2 * oneHalf; cout << result.getNumerator() << " " << result.getDenominator() << endl; return 0; }
const Rational operator +(const Rational& r1, const Rational& r2){ //If the parameters are const, then the get() function must be suffix const BigInt n1(r1.getNumerator()); BigInt n2(r2.getNumerator()); BigInt d1(r1.getDenominator()); BigInt d2(r2.getDenominator()); BigInt gcd(GCD(d1, d2)); gcd.abs(); if(!n1.isNeg() && !n2.isNeg()){ BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd)); BigInt d(d1 * d2 / gcd); return Rational(n, d); } else if(n1.isNeg() && n2.isNeg()){ n1 = -n1; d1 = -d1; n2 = -n2; d2 = -d2; BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd)); BigInt d(d1 * d2 / gcd); return Rational(n, d); } else if(!n1.isNeg() && n2.isNeg()){ n2 = -n2; d2 = -d2; BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd)); BigInt d(d1 * d2 / gcd); return Rational(n, d); } else if(n1.isNeg() && !n2.isNeg()){ n1 = -n1; d1 = -d1; BigInt n(n2 * (d1 / gcd) + n1 * (d2 / gcd)); BigInt d(d1 * d2 / gcd); return Rational(n, d); } }
void NatE::simplify(){ this->exponent->simplify(); this->coefficient->simplify(); if (this->coefficient->getType() == "Rational"){ Rational* newCo = dynamic_cast<Rational*>(this->coefficient); if (newCo->getNumerator()->getType() == "NatE"){ NatE* newPi = dynamic_cast<NatE*>(newCo->getNumerator()); Add* add = new Add(); this->exponent = add->evaluate(this->exponent, newPi->getExponent()); newCo->setNumerator(newPi->getCoefficient()); newCo->simplify(); this->coefficient = newCo; } else if (newCo->getDenominator()->getType() == "NatE"){ NatE* newPi = dynamic_cast<NatE*>(newCo->getDenominator()); Subtract* sub = new Subtract(); this->exponent = sub->evaluate(this->exponent, newPi->getExponent()); newCo->setDenominator(newPi->getCoefficient()); newCo->simplify(); this->coefficient = newCo; } } }
int main() { Rational p; // p uses the default constructor Rational q(1, 2); // q uses the other constructor Rational b(1); // uses constructor to initialize numerator and defaults the denominator to 1 cout << b << endl; p.setNumerator(1); // set p to be 1/4 p.setDenominator(4); p= p + q; p= p * q; //print out p and q p.streamInsert(cout); cout << "p is " << p.getNumerator() << "/" << p.getDenominator() << endl; cout << "q is " << q.getNumerator() << "/" << q.getDenominator() << endl; Rational r; Rational s; cout << "Enter a rational number (a/b): "; cin >> r; cout << "Enter a rational number (a/b): "; cin >> s; cout << "You entered the rational numbers " << r << " and " << s << endl; //Confirm +,-,*, and / works with rationals. Rational sum = r + s; Rational product = r * s; Rational difference = r - s; Rational divide = r/s; // Test greater than, greater than or equal to, and isEqual cout << "Changing value of r and s..." << endl; r.setNumerator(3); r.setDenominator(4); s.setNumerator(1); s. setDenominator(4); if (r >= s) { cout << r << " is greater than or equal to " << s << endl; if (r > s) { cout << "okay..." << r << " is actually greater than " << s << endl; } else if (r== s) { cout << "okay..." << r << " is actually equal to " << s << endl; } } else { cout << "Oops, I enter an incorrect rational to test greater than or equal to!" << endl; } // Test less than, less than or equal to, and isEqual(again) cout << "Changing value of r and s again..." << endl; r.setNumerator(1); r.setDenominator(4); s.setNumerator(3); s. setDenominator(4); if (r <= s) { cout << r << " is less than or equal to " << s << endl; if (r < s) { cout << "okay..." << r << " is actually less than " << s << endl; } else if (r== s) { cout << "okay..." << r << " is actually equal to " << s << endl; } } else { cout << "Oops, I enter an incorrect rational to test less than or equal to!" << endl; } // Test the ability to cout new ADT. cout << r << " + " << s << " = " << sum << endl; cout << r << " * " << s << " = " << product << endl; cout << r << " - " << s << " = " << difference << endl; cout << r << " / " << s << " = " << divide << endl; // Test cloning or coping Rational ADT Rational t(s); double tFloat = t.convertToFloatingPoint(); cout << "s copied the rational " << s << " to t " << t << "." << endl; cout << "t in decimal equals: " << tFloat << endl; // Test reducing rational **extra credit*** r.setNumerator(12); r.setDenominator(4); s.setNumerator(6); s. setDenominator(8); r.reduce(); s.reduce(); cout << "r is now reduced to " << r << endl; cout << "s is now reduced to " << s << endl; cin.ignore(); cin.get(); return 0; }
Rational Rational::subtraction(const Rational& num) { int newDen = lcm(num.getDenominator(), _den); Rational x(_neu * newDen / _den - num.getNumerator() * newDen / num.getDenominator(), newDen); return x; }
const Rational operator*(const Rational & lhs, const Rational & rhs) { Rational ra(lhs.getNumerator()*rhs.getNumerator(), lhs.getDenominator()*rhs.getDenominator()); return ra; }
const Rational operator -(const Rational& r){ BigInt n(r.getNumerator() * BigInt("-1")); return Rational(n, r.getDenominator()); }
Rational Rational::division(const Rational& num) { return Rational(_neu * num.getDenominator(), _den * num.getNumerator()); }
//basic operators bool operator<(Rational const & a, Rational const & b) { return (a.getNumerator() * b.getDenominator()) < (b.getNumerator() * a.getDenominator()); }
void WaveAudioEssenceReader::setEditRate(const Rational& rate) { _editRate = rate; //WaveAudioEssenceDescriptor* waed = // dynamic_cast<WaveAudioEssenceDescriptor*>(_descriptor); //if (waed == 0) //{ // /// \todo report error // return; //} //waed->setSampleRate(rate); // A call to set the edit rate will also set the container duration. // Calculate frame and buffer sizes _baseSampleCount = 0; // reset so we can detect error if (rate == Rational(30000, 1001)) { if (_fmtChunk->nSamplesPerSec == 48000) { _baseSampleCount = 1600; _additionalSampleCounts = 5; _additionalSampleCount[0] = 2; _additionalSampleCount[1] = 1; _additionalSampleCount[2] = 2; _additionalSampleCount[3] = 1; _additionalSampleCount[4] = 2; } /// \todo warn if audio data isn't 48kHz } else { _baseSampleCount = (UInt32)( _fmtChunk->nSamplesPerSec * (UInt64) rate.getDenominator() / (UInt64) rate.getNumerator()); _additionalSampleCounts = 0; } _combinedDataLength = 0; if (_additionalSampleCounts > 0) { // Calculate the average buffer size for (UInt32 i = 0; i < _additionalSampleCounts; ++i) { _combinedDataLength += (_baseSampleCount + _additionalSampleCount[i]) * (_fmtChunk->wBitsPerSample / 8) * _fmtChunk->nChannels; } } else { _combinedDataLength = _baseSampleCount * (_fmtChunk->wBitsPerSample / 8) * _fmtChunk->nChannels; } if (_baseSampleCount == 0 || _combinedDataLength == 0) { // We've failed to determine the size of an edit unit error(ESS_ERROR_FailedToDetermineFrameSize); return; } // Update the duration for the essence descriptor if (_additionalSampleCounts > 0) { Length duration = ((_dataLength * _additionalSampleCounts) / _combinedDataLength); // we have to do the rounding manually as ceil() takes a double // and VC6 can't handle unsigned int64 to double... Length rem = (_dataLength * _additionalSampleCounts) % _combinedDataLength; if (rem != 0) duration++; // round up //waed->setContainerDuration(duration); _containerDuration = duration; } else { Length duration = _dataLength / _combinedDataLength; // we have to do the rounding manually as ceil() takes a double // and VC6 can't handle unsigned int64 to double... Length rem = _dataLength % _combinedDataLength; if (rem != 0) duration++; // round up //waed->setContainerDuration(duration); _containerDuration = duration; } }
Rational Rational::add( const Rational & other ) const{ return Rational((getNumerator() * other.getDenominator() + other.getNumerator() * getDenominator()), (getDenominator() * other.getDenominator())); }
void testSimplify() { Rational r = Rational(2,4); r.simplify(); cout << "Should be (1,2) and was (" << r.getNumerator() << ", " << r.getDenominator() << ")."; }