Beispiel #1
0
const OperateurDen& OperateurDen::evaluate(Pile& pile) const
{
    if (pile.taille() >= getArite()) {
        Rationnel* rationnel = dynamic_cast<Rationnel*>(&pile.top());
        Entier* entier = dynamic_cast<Entier*>(&pile.top());

        if(rationnel == nullptr && entier == nullptr) throw ComputerException("Erreur : mauvais arguments");

        Number& N1 = dynamic_cast<Number&>(pile.top());
        pile.pop();

        QString res = N1.getDenominateur().toString();
        Expression& e = ExpressionManager::getInstance().addExpression(res);

        //ajout sur la pile
        pile.push(e);
        QString message;
        message = this->getSymbole() + " " + N1.toString() + " = " + pile.top().toString();
        pile.setMessage(message);

        //sauvegarde des arguments
        QVector<QString> args;
        args.push_back(N1.toString());
        pile.setLastArgs(args);
    }
    else
    {
        throw ComputerException("Erreur : pas assez d'arguments");
    }
    return *this;
}
Beispiel #2
0
const OperateurAND& OperateurAND::evaluate(Pile& pile) const
{
    if (pile.taille() >= getArite()) {
        try
        {
            dynamic_cast<Entier&>(pile.top());
        }
        catch(exception&)
        {
            throw ComputerException("Erreur : mauvais arguments");
        }
        Entier& N2 = dynamic_cast<Entier&>(pile.top());
        pile.pop();
        try
        {
            dynamic_cast<Entier&>(pile.top());
        }
        catch(exception&)
        {
            pile.push(N2);
            throw ComputerException("Erreur : mauvais arguments");
        }
        Entier& N1 = dynamic_cast<Entier&>(pile.top());
        pile.pop();

        QString res;
        if(static_cast<int>(N1.getValue()) != 0 && static_cast<int>(N2.getValue()) != 0)
        {
            res = "1";
        }
        else
        {
            res = "0";
        }
        Expression& e = ExpressionManager::getInstance().addExpression(res);

        //ajout sur la pile
        pile.push(e);
        QString message;
        message = N1.toString() + " " + this->getSymbole() + " " + N2.toString() + " = " + pile.top().toString();
        pile.setMessage(message);

        //sauvegarde des arguments
        QVector<QString> args;
        args.push_back(N1.toString());
        args.push_back(N2.toString());
        pile.setLastArgs(args);
    }
    else
    {
        throw ComputerException("Erreur : pas assez d'arguments");
    }
    return *this;
}
Beispiel #3
0
const OperateurComplexe& OperateurComplexe::evaluate(Pile& pile) const
{
    if (pile.taille() >= getArite()) {
        try
        {
            dynamic_cast<Number&>(pile.top());
        }
        catch(exception&)
        {
            throw ComputerException("Erreur : mauvais arguments");
        }
        Number& N2 = dynamic_cast<Number&>(pile.top());
        pile.pop();
        try
        {
            dynamic_cast<Number&>(pile.top());
        }
        catch(exception&)
        {
            pile.push(N2);
            throw ComputerException("Erreur : mauvais arguments");
        }
        Number& N1 = dynamic_cast<Number&>(pile.top());
        pile.pop();
        double numRe,numIm,denRe,denIm;
        //calcul de la partie réelle
        numRe = N1.getRe().getNumerateur().getValue()*N2.getIm().getDenominateur().getValue()-N2.getIm().getNumerateur().getValue()*N1.getRe().getDenominateur().getValue();
        denRe = N1.getRe().getDenominateur().getValue()*N2.getIm().getDenominateur().getValue();
        //calcul de la partie réelle
        numIm = N1.getIm().getNumerateur().getValue()*N2.getRe().getDenominateur().getValue()+N2.getRe().getNumerateur().getValue()*N1.getIm().getDenominateur().getValue();
        denIm = N1.getIm().getDenominateur().getValue()*N2.getRe().getDenominateur().getValue();

        QString res = QString::number(numRe)+"/"+ QString::number(denRe)+"$"+QString::number(numIm)+"/"+QString::number(denIm);
        Expression& e = ExpressionManager::getInstance().addExpression(res);

        //ajout sur la pile
        pile.push(e);
        QString message;
        message = N1.toString() + " " + this->getSymbole() + " " + N2.toString() + " = " + pile.top().toString();
        pile.setMessage(message);
        

        //sauvegarde des arguments
        QVector<QString> args;
        args.push_back(N1.toString());
        args.push_back(N2.toString());
        pile.setLastArgs(args);
    }
    else
    {
        throw ComputerException("Erreur : pas assez d'arguments");
    }
    return *this;
}
Beispiel #4
0
Nombre& Rationnel::operator/(Nombre& a)
{
    Rationnel* a1=dynamic_cast<Rationnel*>(&a);
    if(a1!=nullptr)
    {
        Entier& ent1=dynamic_cast<Entier&>(num->operator *(*(a1->den)));
        Entier& ent2=dynamic_cast<Entier&>(den->operator*(*(a1->num)));
        Rationnel* r= new Rationnel(ent1,ent2);
        return r->simplificationExt();
    }
    Complexe* a2=dynamic_cast<Complexe*>(&a);
    if(a2!=nullptr)return (a2->operator /(*this));

    Entier* a3=dynamic_cast<Entier*>(&a);
    if(a3!=nullptr)
    {
        Entier& ent3=dynamic_cast<Entier&>(den->operator*(*a3));
        Rationnel* r= new Rationnel(*num,ent3);
        return r->simplificationExt();
    }

    Reel* a4=dynamic_cast<Reel*>(&a);
    if(a4!=nullptr)
    {
        double re1=num->val;
        double re2=den->val;
        Reel* re= new Reel(re1/re2/a4->val);
        return *re;
    }
    else throw ComputerException("");
}
Beispiel #5
0
Nombre& Complexe::operator*(Nombre& a)
{
    Complexe* a1=dynamic_cast<Complexe*>(&a);
    if(a1!=nullptr)
    {
        //numerateur de la partie reelle
        Nombre& nb1=re->operator*(*(a1->re));
        Nombre& nb2=(a1->im)->operator*(*im);
        Nombre& nb3=nb1.operator -(nb2);


        //numerateur de la partie immaginaire
        nb1=(a1->re)->operator*(*im);
        nb2=re->operator*(*(a1->im));
        Nombre& nb4=nb2.operator +(nb1);


        Complexe* c= new Complexe(nb3, nb4);
        return c->simplification();
    }

    Entier* a2=dynamic_cast<Entier*>(&a);
    Reel *a3=dynamic_cast<Reel*>(&a);
    Rationnel* a4=dynamic_cast<Rationnel*>(&a);
    Nombre* a5=dynamic_cast<Nombre*>(&a);
    if (a2!=nullptr || a3!=nullptr || a4!=nullptr)
    {
        Complexe* c= new Complexe((a5->operator*(*re)),(a5->operator*(*im)));
        return c->simplification();
    }
    else throw ComputerException("type inconnu");

}
Beispiel #6
0
Entiere* mod(Litterale& l1, Litterale& l2) {
	if (l1.getType() == tEntiere && l2.getType() == tEntiere) {
		Entiere* pt1 = dynamic_cast<Entiere*>(&l1);
		Entiere* pt2 = dynamic_cast<Entiere*>(&l2);
		return (new Entiere(pt1->getVal() - ((pt1->getVal() / pt2->getVal()) * pt2->getVal())));
	}
	else
		throw ComputerException("Impossible d'effectuer MOD sur des litterales non entieres.\n");
}
Beispiel #7
0
Complexe* dollar(Litterale& l1, Litterale& l2) {
	if (l1.getType() != tComplexe && l2.getType() != tComplexe) {
		Numerique* pt1 = dynamic_cast<Numerique*>(&l1);
		Numerique* pt2 = dynamic_cast<Numerique*>(&l2);
		return (new Complexe(pt1->clone(), pt2->clone()));
	}
	else
		throw ComputerException("Impossible d'effectuer $ sur une litterale complexe.\n");
}
Litterale* OpDol::fonctionNum(Nombres *arg1, Litterale *arg2) {
    LitNumerique* conv1 = dynamic_cast<LitNumerique*>(arg1);
    LitNumerique* conv2 = dynamic_cast<LitNumerique*>(arg2);
    if (conv1 == nullptr || conv2 == nullptr)
        throw ComputerException("Arguments invalides");

    Complexe* c = new Complexe(conv1,conv2);
    return c;
}
Beispiel #9
0
bool Litterale::operator>=(Litterale * l)
{
	if ((l->getType() || this->getType()) == tExpression || (l->getType() || this->getType()) == tProgramme || (l->getType() || this->getType()) == tAtome || (l->getType() || this->getType()) == tComplexe)
		throw ComputerException("Impossible d'effectuer une comparaison sur ce type de Litterale\n");
	else {
		Numerique* pt1 = dynamic_cast<Numerique*>(this);
		Numerique* pt2 = dynamic_cast<Numerique*>(l);
		return (pt1->getComp() >= pt2->getComp());
	}
}
Beispiel #10
0
Entiere* den(Litterale& l) {
	if (l.getType() == tRationnelle) {
		Rationnelle* pt1 = dynamic_cast<Rationnelle*>(&l);
		return (new Entiere(pt1->getDen()));
	}
	else if (l.getType() == tEntiere) {
		return (new Entiere(1));
	}
	else
		throw ComputerException("Impossible d'effectuer DEN sur une litterale reelle ou complexe.\n");
}
Litterale* OpMod::fonctionNum(Nombres *arg1, Litterale *arg2){
    Entier* n1 = dynamic_cast<Entier*>(arg1);
    Entier* n2 = dynamic_cast<Entier*>(arg2);
    if (n1 == nullptr || n2 == nullptr)
        throw ComputerException("Arguments invalides");

    int val = (int)n1->getValue()%(int)n2->getValue();
    Entier* res = new Entier(val);
    if (n1->getNeg() || n2->getNeg())
        res->setNeg(true);

    return res;
}
Beispiel #12
0
Entiere* num(Litterale& l) {
	if (l.getType() == tRationnelle) {
		Rationnelle* pt1 = dynamic_cast<Rationnelle*>(&l);
		return (new Entiere(pt1->getNum()));
	}
	else if (l.getType() == tEntiere) {
		Entiere* pt1 = dynamic_cast<Entiere*>(&l);
		return pt1->clone();
	}
	else {
		throw ComputerException("Impossible d'effectuer NUM sur une litterale reelle ou complexe.\n");
	}
}
Beispiel #13
0
/**
 * \fn void Numerique::setRationnelIm(int n,int d)
 * \brief Accesseur en ecriture pour la partie imaginaire de type rationnel d'un numerique
 */
void Numerique::setRationnelIm(int n,int d)
{
    if(d==0)
    {
        throw ComputerException("fraction exception division par 0");
    }

    else
    {
        numIm=n;
        denomIm=d;
    }
    simplificationIm();
    if (denomIm==1)
        setTypeIm("entier");
}
Beispiel #14
0
/**
 * \fn void Numerique::setRationnelRe(int n,int d)
 * \brief Accesseur en ecriture pour la partie reelle de type rationnel d'un numerique
 */
void Numerique::setRationnelRe(int n,int d)
{
    if(d==0)
    {
        throw ComputerException("fraction exception division par 0");
    }
    else
    {
        numReel=n;
        denomReel=d;
    }
    simplificationRe();
    if (denomReel==1)
        setTypeRe("entier");

}
Beispiel #15
0
const OperateurClear& OperateurClear::evaluate(Pile& pile) const
{
    if(pile.estVide()) throw ComputerException("Erreur : la pile est vide");
    while(!pile.estVide())
    {
        pile.pop();
    }
    //ajout sur la pile
    QString message;
    message = this->getSymbole();
    pile.setMessage(message);

    //sauvegarde des arguments
    QVector<QString> args;
    pile.setLastArgs(args);

    return *this;
}
Beispiel #16
0
Nombre& Complexe::operator-(Nombre& a)
{
    Complexe* a1=dynamic_cast<Complexe*>(&a);
    if(a1!=nullptr)
    {
        Complexe* c= new Complexe(((a1->re)->operator-(*re)), (a1->im)->operator-(*im));
        return *c;
    }

    Entier* a2=dynamic_cast<Entier*>(&a);
    Reel* a3=dynamic_cast<Reel*>(&a);
    Rationnel* a4=dynamic_cast<Rationnel*>(&a);
    Nombre* a5=dynamic_cast<Nombre*>(&a);
    if (a2!=nullptr || a3!=nullptr || a4!=nullptr)
    {
        Complexe* c= new Complexe((a5->operator -(*re)),(*im));
        return c->simplification();
    }
    else throw ComputerException("type inconnu");
}
Beispiel #17
0
Nombre& Rationnel::operator+(Nombre& a) // ent1,ent2, r // on ne détruit pas les opérandes initiales a et this car il seront détruit après l'appel
{
    Rationnel* a1=dynamic_cast<Rationnel*>(&a);
    if(a1!=nullptr)
    {
        Entier& ent1=dynamic_cast<Entier&>(num->operator*(*(a1->den)));     // l'operateur appelé crée un nb non libere dans le controleur. a suppr apres
        Entier& ent2=dynamic_cast<Entier&>((a1->num)->operator*(*den));     // allocation ent2
        Entier& ent3=dynamic_cast<Entier&>(ent1.operator+(ent2));           // allocation ent3 ne pas les detruire ils font parti du Rationnel final
        Entier& ent4=dynamic_cast<Entier&>((den->operator*(*(a1->den))));   // allocation ent4 ne pas les detruire ils font parti du Rationnel final
        Rationnel* r=new Rationnel(ent3,ent4);                             // allocation r
        return r->simplificationExt();
    }

    Complexe* a2=dynamic_cast<Complexe*>(&a);
    if(a2!=nullptr) return (a2->operator +(*this));


    Entier* a3=dynamic_cast<Entier*>(&a);
    if(a3!=nullptr)
    {
        Entier& ent5=dynamic_cast<Entier&>(a3->operator*(*den));
        Entier& ent6=dynamic_cast<Entier&>(num->operator+(ent5));
        Rationnel* r= new Rationnel(ent6,(*den));

        return r->simplificationExt();
    }

    Reel* a4=dynamic_cast<Reel*>(&a);
    if(a4!=nullptr)
    {
        double re1=num->val;
        double re2=den->val;
        Reel* re= new Reel(re1/re2+a4->val);
        return *re;
    }
    else throw ComputerException("");
}
Beispiel #18
0
const OperateurDiff& OperateurDiff::evaluate(Pile& pile) const
{
    if (pile.taille() >= getArite()) {
        try
        {
            dynamic_cast<Number&>(pile.top());
        }
        catch(exception&)
        {
            throw ComputerException("Erreur : mauvais arguments");
        }
        Number& N2 = dynamic_cast<Number&>(pile.top());
        pile.pop();
        try
        {
            dynamic_cast<Number&>(pile.top());
        }
        catch(exception&)
        {
            pile.push(N2);
            throw ComputerException("Erreur : mauvais arguments");
        }
        Number& N1 = dynamic_cast<Number&>(pile.top());
        pile.pop();
        double Re1, Re2, Im1, Im2;
        bool bRe, bIm;
        //calcul de la partie réelle
        Re1 = N1.getRe().getNumerateur().getValue()/N1.getRe().getDenominateur().getValue();
        Re2 = N2.getRe().getNumerateur().getValue()/N2.getRe().getDenominateur().getValue();
        //calcul de la partie réelle
        Im1 = N1.getIm().getNumerateur().getValue()/N1.getIm().getDenominateur().getValue();
        Im2 = N2.getIm().getNumerateur().getValue()/N2.getIm().getDenominateur().getValue();

        bRe = Re1 != Re2;
        bIm = Im1 != Im2;

        QString res;
        if(bRe || bIm)
        {
            res = "1";
        }
        else
        {
            res = "0";
        }
        Expression& e = ExpressionManager::getInstance().addExpression(res);

        //ajout sur la pile
        pile.push(e);
        QString message;
        message = N1.toString() + " " + this->getSymbole() + " " + N2.toString() + " : " + res;
        pile.setMessage(message);
        

        //sauvegarde des arguments
        QVector<QString> args;
        args.push_back(N1.toString());
        args.push_back(N2.toString());
        pile.setLastArgs(args);
    }
    else
    {
        throw ComputerException("Erreur : pas assez d'arguments");
    }
    return *this;
}
Beispiel #19
0
Litteral* Pile::top() const {
    if(stack.length() == 0)
        throw ComputerException("Erreur : Pile vide");
    else
        return stack.top();
}
Litterale* OpMod::fonctionExpression(LitExpression *arg1, Litterale *arg2) {
    throw ComputerException("Arguments invalides");
}
Beispiel #21
0
Expression& Pile::top() const {

    if (estVide()) throw ComputerException("Aucune expression sur la pile");
    return *items.top();
}