Exemplo n.º 1
0
 Rational getSimplifiedCopy(const Rational& rational)
 {
     Rational simplified = rational;
     simplified.simplify();
     
     return simplified;
 }
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();
		}
	}
}
Exemplo n.º 3
0
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;
		}

	}
}
Exemplo n.º 4
0
 void testSimplify()
 {
     Rational r = Rational(2,4);
     r.simplify();
     cout << "Should be (1,2) and was (" << r.getNumerator() << ", " << r.getDenominator() << ").";
 }