Example #1
10
bool operator <= (Fraction const &a, int f)
{
	Fraction tmp(f);
    Fraction r;
    r = a - tmp;
    r.smart();
    if(r.getU() <= 0)
        return true;
    else
        return false;
}
TEST(FractionTest, Can_Set_Denominator) {
    // Arrange
    Fraction z;
    int d = 26;

    // Act
    z.setDenom(d);

    // Assert
    EXPECT_EQ(d, z.getDenom());
}
Example #3
0
Fraction Tremolo::tremoloLen() const
      {
      Fraction f;
      switch(lines()) {
            case 1: f.set(1,8); break;
            case 2: f.set(1,16); break;
            case 3: f.set(1,32); break;
            case 4: f.set(1,64); break;
            }
      return f;
      }
Example #4
0
int main(void)
{
	Fraction myFract;

	myFract.setNumerator(1);
	myFract.setDenominator(3);

	myFract.print();

	return 0;
}
Example #5
0
bool operator < (Fraction const &a, int f)
{
    Fraction r;
    r.u = a.u - f * a.l;
    r.l = a.l;
    r.smart();
    if(r.getU() < 0)
        return true;
    else
        return false;
}
Example #6
0
void fractin::on_simplify_clicked()
{
    int d=0,c=0;
    if(text.isEmpty()){
        text="格式不支持!";
        ui->show->setText(text);
        return;
    }
    QChar ch=text[0];
    text.remove(0,1);
    if(!ch.isDigit()){
        text="格式不支持!";
        ui->show->setText(text);
        return;
    }
    while(ch.isDigit()&&!text.isEmpty()){
        d=d*10+ch.unicode()-48;
        ch=text[0];
        text.remove(0,1);

    }
    if(text.isEmpty()||ch!='|'){
        text="格式不支持!";
        ui->show->setText(text);
        return;
    }
    ch=text[0];
    text.remove(0,1);
    if(!ch.isDigit()){
        text="格式不支持!";
        ui->show->setText(text);
        return;
    }
    while(ch.isDigit()&&!text.isEmpty()){
        c=c*10+ch.unicode()-48;
        ch=text[0];
        text.remove(0,1);
    }
    if(ch.isDigit()){
        c=c*10+ch.unicode()-48;
        Fraction f;
        f.Sim(d,c);
        text=QString::number(f.getnumer(),10)+"|"+QString::number(f.getdenom(),10);
        ui->show->setText(text);
        return;
    }
    else{
        text="格式不支持!";
        ui->show->setText(text);
        return;
    }

}
Example #7
0
	// return the inverse (reciprocal) of this rational object
	Rational Rational::inv() const {
		Fraction fraction = *this;

		// fraction's method to invert;
		fraction = fraction.inv();

		// reduce to lowest terms
		Fraction::toLowestTerms(fraction);

		// fraction is already inverted
		return Rational(fraction);
	}
Example #8
0
int main() {
   Fraction test;

   test.setNum(20);
   test.setDen(60);

   std::cout << "The ratio of the two numbers is: " << test.getRatio() << std::endl;
   std::cout << "The simplified fraction is: ";
   test.getFraction();

   return 0;
}
Fraction ArrayOfFractions::sum()const {
	Fraction fracSum = A[0];

	for (int idx=1; idx<frac_count; idx++) {
		fracSum.print(); cout<< " + "; A[idx].print(); cout<< " = ";
		fracSum = fracSum.add(A[idx]);
		fracSum.print();
		cout<< "\n";
	}

	return fracSum;
}
Example #10
0
Fraction Fraction::operator/(Fraction &pFraction) {
    Fraction tmp;
    // validation of fraction in constructor and in method setDenominator()
    tmp.counter = (counter * pFraction.denominator);
    tmp.denominator = (denominator * pFraction.counter);
    try {
        tmp.reduce();
    } catch (const char *e) {
        std::cerr << e << std::endl;
    }
    return tmp;
}
Example #11
0
Fraction& Fraction::operator-(Fraction& one)
{
	Fraction tmp = Fraction(0, 0);
	int first, second;
	first = numerator*one.denominator;
	second = one.numerator*denominator;
	tmp.numerator = first - second;
	tmp.denominator = denominator*one.denominator;
	tmp.Neprav();
	tmp.Print();
	return tmp;
}
Example #12
0
TEST_FIXTURE(FractionTestFixture, MultFractions)
{
    fract.set(1, 2);

    Fraction fract2;
    fract2.set(1,2);

    Fraction result = fract.mult(fract2);

    CHECK_EQUAL(1, result.get_num());
    CHECK_EQUAL(4, result.get_den());
}
Example #13
0
TEST_FIXTURE(FractionTestFixture, SubtractFractionsWithDifferentDenoms)
{
    fract.set(3, 4);

    Fraction fract2;
    fract2.set(1, 3);

    Fraction result = fract.sub(fract2);

    CHECK_EQUAL(5, result.get_num());
    CHECK_EQUAL(12, result.get_den());
}
Example #14
0
TEST_FIXTURE(FractionTestFixture, AddFractionsWithDifferentDenom)
{
    fract.set(2, 3);

    Fraction fract2;
    fract2.set(1,5);

    Fraction result = fract.add(fract2);

    CHECK_EQUAL(13, result.get_num());
    CHECK_EQUAL(15, result.get_den());
}
Example #15
0
TEST_FIXTURE(FractionTestFixture, SubtractFractionsWithSameDenom)
{
    fract.set(1, 2);

    Fraction fract2;
    fract2.set(1, 2);

    Fraction result = fract.sub(fract2);

    CHECK_EQUAL(0, result.get_num());
    CHECK_EQUAL(1, result.get_den());
}
Example #16
0
void main() {
    Fraction f = Fraction(1);

    Fraction f2 = Fraction(1,2);

    try {
        Fraction f3 = Fraction(1,0);
    }
    catch(exception& e)
    {
        cout<< e.what() << endl;
    }
    Fraction f4  = f.addition(f2);
    cout << f4.evaluer() << endl;

    Fraction f5  = f2.soustraction(f);
    cout << f5.evaluer() << endl;

    Fraction f6  = f2.soustraction(f2);
    cout << f6.evaluer() << endl;

    Fraction f7  = f2.multiplication(f);
    cout << f7.evaluer() << endl;

    Fraction f8  = f4.multiplication(f2);
    cout << f8.evaluer() << endl;

    Fraction f9  = f.division(f2);
    cout << f9.evaluer() << endl;

    try {
        Fraction f10  = f.division(Fraction(0,1));
        cout << f10.evaluer() << endl;
    }
    catch(exception& e)
    {
        cout<< e.what() << endl;
    }

    Fraction f10 = Fraction(INT_MAX,1);

    try {
        Fraction f11  = f10.addition(f10);
        cout << f11.evaluer() << endl;
    }
    catch(exception& e)
    {
        cout<< e.what() << endl;
    }


    cout << "finf" <<endl;
}
Example #17
0
TEST_FIXTURE(FractionTestFixture, DivideFractions)
{
    fract.set(2, 7);

    Fraction fract2;
    fract2.set(3, 4);

    Fraction result = fract.div(fract2);

    CHECK_EQUAL(8, result.get_num());
    CHECK_EQUAL(21, result.get_den());
}
Example #18
0
TEST_FIXTURE(FractionTestFixture, SubtractFractionsWithNegativeResult)
{
    fract.set(1, 3);

    Fraction fract2;
    fract2.set(3, 4);

    Fraction result = fract.sub(fract2);

    CHECK_EQUAL(-5, result.get_num());
    CHECK_EQUAL(12, result.get_den());
}
Example #19
0
const Fraction operator*(const Fraction& term1, const Fraction& term2) {
	//Turned into improper fractions and then mulitplied through
	Fraction result;
	result.SetNumerator(((term1.GetWhole() * term1.GetDenominator()) + term1.GetNumerator()) * ((term2.GetWhole() * term2.GetDenominator()) + term2.GetNumerator()));
	result.SetDenominator(term1.GetDenominator() * term2.GetDenominator());
	result.Normalize();
	
	return result;
}
Example #20
0
Fraction Fraction::operator*(Fraction &pFraction) {
    Fraction tmp;
    
    // crosswise multiply
    tmp.counter = (counter * pFraction.counter);
    tmp.denominator = (denominator * pFraction.denominator);
    try {
        tmp.reduce();
    } catch (const char *e) {
        std::cerr << e << std::endl;
    }
    return tmp;
}
Example #21
0
//returns a reduced fraction object
Fraction Fraction::reduce(){
    Fraction reduced;
    int gcd = GCD(this->numerator,this->denominator);
    if(gcd!=1)
    {
        reduced.setNumerator(this->getNumerator()/gcd);
        reduced.setDenominator(this->getDenominator()/gcd);

        return reduced;
    }
    
    return *this;
}
Example #22
0
Number* Fraction::multiply(Number* F1){

	Fraction* f1 = dynamic_cast<Fraction*>(F1);
	Fraction* f2 = dynamic_cast<Fraction*>(this);

	if(f1 && f2){
		Number* n1 = f1->getNumerator();
		Number* n2 = f2->getNumerator();
		Number* d1 = f1->getDenominator();
		Number* d2 = f2->getDenominator();

		Integer* i1 = dynamic_cast<Integer*>(n1);
		Integer* i2 = dynamic_cast<Integer*>(n2);
		Integer* i3 = dynamic_cast<Integer*>(d1);
		Integer* i4 = dynamic_cast<Integer*>(d2);

		Number* newNum;
		Number* newDen;

		if(i3 && i4){
			int t3 = i3->getInt();
			int t4 = i4->getInt();

			newNum = new Integer(t3*t4);

		}

		else{

			newNum = n1->multiply(n2);
		}

		if(i1 && i2){
			int t1 = i1->getInt();
			int t2 = i2->getInt();

			newDen = new Integer(t1*t2);
		}

		else{
			newDen = d1->multiply(d2);
		}

		Fraction* Ans = new Fraction(newNum, newDen);

		Number* simpleans = Ans->simplify();

		return simpleans;
}
	else return F1;
}
Example #23
0
Number* Fraction::subtract(Number* F1){

	Fraction* f1 = dynamic_cast<Fraction*>(F1);
	Fraction* f2 = dynamic_cast<Fraction*>(this);

	if(f1 && f2){
		Number* n1 = f1->getNumerator();
		Number* n2 = f2->getNumerator();
		Number* d1 = f1->getDenominator();
		Number* d2 = f2->getDenominator();

		Integer* i1 = dynamic_cast<Integer*>(n1);
		Integer* i2 = dynamic_cast<Integer*>(n2);
		Integer* i3 = dynamic_cast<Integer*>(d1);
		Integer* i4 = dynamic_cast<Integer*>(d2);

		if(i1 && i2 && i3 && i4){
			Number* lcm = f2->lcd(f1);
			Integer* lcd = dynamic_cast<Integer*>(lcm);

			int t1 = i1->getInt();
			int t2 = i2->getInt();
			int t3 = i3->getInt();
			int t4 = i4->getInt();
			int lcmint = lcd->getInt();

			int fact1 = lcmint / t1;
			int fact2 = lcmint / t2;

			t1 *= fact1;
			t3 *= fact1;
			t2 *= fact2;
			t4 *= fact2;

			int diff = t3 - t4;
			Integer* newNum = new Integer(diff);
			Integer* newDen = new Integer(lcmint);

			Fraction* ans = new Fraction(newNum, newDen);

			Number* simpleAns = ans->simplify();

			return simpleAns;
		}

		else return F1;
	}

	else return F1;

}
bool testReducedFraction()
{
  typedef typename SB::Integer Integer;
  typedef typename SB::Quotient Quotient;
  typedef typename SB::Fraction Fraction;
  unsigned int nbok = 0;
  unsigned int nb = 0;
  Integer p = random() / 10000;
  Integer q = random() / 10000;
  trace.beginBlock ( "Testing block: reduced fraction." );
  IntegerComputer<Integer> ic;
  Integer g = ic.gcd( p, q );
  p /= g;
  q /= g;
  IntegerComputer<Quotient> ics;
  Quotient sp = NumberTraits<Integer>::castToInt64_t( p );
  Quotient sq = NumberTraits<Integer>::castToInt64_t( q );
  std::vector<Quotient> cf1;
  ics.getCFrac( cf1, sp, sq );
  Fraction f1 = SB::fraction( p, q );
  std::vector<Quotient> cf1_bis;
  f1.getCFrac( cf1_bis );
  bool ok = equalCFrac<Quotient>( cf1, cf1_bis );
  trace.info() << "  - p / q = " << p << " / " << q << std::endl;
  trace.info() << "  - f1 = ";
  SB::display( trace.info(), f1 );
  trace.info() << std::endl;
  ++nb, nbok += ok ? 1 : 0;
  trace.info() << "(" << nbok << "/" << nb << ") " 
               << " cfrac"
               << std::endl;
  unsigned int depth = cf1.size();
  for ( unsigned int k = 1; k < depth; ++k )
    {
      std::vector<Quotient> cf1_red;
      Fraction fr = f1.reduced( k );
      fr.getCFrac( cf1_red );
      cf1.resize( depth - k );
      ok = equalCFrac<Quotient>( cf1, cf1_red );
      ++nb, nbok += ok ? 1 : 0;
      trace.info() << "(" << nbok << "/" << nb << ") " 
                   << "reduced(" << k << ")=";
      SB::display( trace.info(), fr );
      std::cerr << std::endl;
    }

  //trace.info() << "- nbFractions = " << SB::instance().nbFractions << std::endl;
  trace.endBlock();
  return nbok == nb;
}
Example #25
0
	Fraction getRationalized()
	{
		Fraction f;
		// intermediate
		int f_dn = (dsr.getn() - ( dn * dn)) / nn;
		SquareRoot f_nsr( dsr.getn());
		int f_nn = (dn * -1);
		int nearby = getnearby( dsr);
	
		// set final
		int f_n = (int)((nearby + f_nn) / f_dn);
		f.set( NULL, f_n, NULL, f_dn, f_nsr, f_nn - (f_n * f_dn));
		return f;
	}
Example #26
0
int main() {

	// greeting
	cout << "Enter a fraction and this program will reduce it to its lowest terms\n"
		 << "enter 0 for the denominator to quit";

	Fraction set;

	do {

		// the fraction is gotten from the user
		set.setNum();
		set.setDen();

		// the values for the fraction is gotten for the output
		set.getNum();
		set.getDen();

		// the reduced fraction is outputted
		if (set.getDen() > 0)
			set.lcdOutput();
		else
			cout << "\nTerminating the program\n\n";

	} while (set.getDen() != 0);
}
Example #27
0
void options(int &opcion, Fraction & a, Fraction & b){
	int tempa, tempb; //Used to store first fraction
	int tempx,tempy; //Used to store second fraction

	//Ask user for an option
	cout << "\nQue desea hacer a continuacion?"
		<< "\n\t1) Sumar \n\t2) Restar"
		<< "\n\t3) Multiplicar \n\t4) Dividir"
		<< "\n\t5) Comparar \n\t6) Ordenar lista"
		<< "\n\t7) Calcular sumatoria"
		<< endl << "Usted escogio la opcion: ";
        cin >> opcion; //Stores the option

        //Input validation
        while (opcion < 1 || opcion > 7){
		cout << "Error! Seleccione una opcion correcta: ";
		cin >> opcion;
        }

        //If the option requires two values, asks for which two of them
        if (opcion < 6){
		cout << "Someta los siguientes datos para las "
		     << "fraciones." << endl;

		cout << "Fraccion 1, numerador: ";
		cin >> tempa;
		cout << "Fraccion 1, denominador: ";
		cin >> tempb;
		//Input validation
		while (tempb == 0){
			cout << "El denominador no puede ser 0: ";
			cin >> tempb;
		}
		cout << "Fraccion 2, numerador: ";
		cin >> tempx;
		cout << "Fraccion 2, denominador: ";
		cin >> tempy;
		//Input validation
		while (tempy == 0){
                        cout << "El denominador no puede ser 0";
                        cin >> tempy;
                }

		//Sets the values
		a.setNum(tempa);
		a.setDenom(tempb);
		b.setNum(tempx);
		b.setDenom(tempy);

        }
Fraction MxmlSupport::durationAsFraction(const int divisions, const QDomElement e)
      {
      Fraction f;
      if (e.tagName() == "duration") {
            bool ok;
            int val = MxmlSupport::stringToInt(e.text(), &ok);
            f = Fraction(val, 4 * divisions); // note divisions = ticks / quarter note
            f.reduce();
            }
      else {
            qDebug() << "durationAsFraction tagname error" << f.print();
            }
      return f;
      }
Example #29
0
  int monthlyOrder(vector <int> sales, vector <int> daysAvailable) {
    int n=sz(sales), d=0;
    Fraction sum;
    rep(i,n) {
      //if (sales[i]==0 || daysAvailable[i]==0) continue;
      if (daysAvailable[i]==0) continue;

      Fraction expected(sales[i]*30, daysAvailable[i]);
      sum += expected;
      d++;
    }
    sum /= d;
    return (int)ceil( sum.value() );
  }
Example #30
0
Fraction Fraction::subtract(const Fraction& other) {
	Fraction result;
	int result_Numerator, result_Denominator;

	// Result denominator is Least Common Denominator
	result_Denominator = Fraction::LCD(m_Denominator, other.m_Denominator);

	// Use LCD to get numerators
	result_Numerator = (result_Denominator / m_Denominator) * m_Numerator
					 - (result_Denominator / other.m_Denominator) * other.m_Numerator;

	result.set(result_Numerator, result_Denominator);
	
	return result;
}