Reelle::Reelle(const Entier& p,const Entier& m){ /*Un réel ne peut pas être construit avec une mantisse nulle*/ if(m.getValeur()==0){LitteraleException("Construction: Mantisse nulle !","Reelle");} else{value=p.getValeur(); double mantisse=(double)m.getValeur(); while(mantisse>=1) mantisse=mantisse/10.0; value+=mantisse;} }
Litterale* Complexe::getFromString(QString s) const{ QStringList Ql = s.split('$'); LitteraleFactory& LF = LitteraleFactory::donnerInstance(); LitteraleNumerique* l1 = estdeType<LitteraleNumerique>(LF.creerInfixLitterale(Ql.at(0))); LitteraleNumerique* l2 = estdeType<LitteraleNumerique>(LF.creerInfixLitterale(Ql.at(1))); if(l1 && l2) return new Complexe(*l1,*l2); else throw LitteraleException(s+" n'est pas un Complexe valide","Complexe"); }
Rationnel::Rationnel(int n, int d){ if (d == 0) throw LitteraleException("Le dénominateur ne peut être nul !","Rationnel"); else { int pg = pgcd(n, d); n = n / pg; d = d / pg; numerateur = n; denominateur = d; } }
Litterale* Reelle::getFromString(QString s) const{ /* QStringList Ql = s.split('.'); LitteraleFactory& LF = LitteraleFactory::donnerInstance(); Entier* l1 = estdeType<Entier>(LF.creerRPNLitterale(Ql.at(0))); Entier* l2 = estdeType<Entier>(LF.creerRPNLitterale(Ql.at(1))); if(l1 && l2) return new Reelle(*l1,*l2); else throw LitteraleException(s+" n'est pas une Réelle valide","Reelle"); */ bool ok; double res= (s.toDouble(&ok)); if(ok) return new Reelle(res); else throw LitteraleException(s+" n'est pas une Réelle valide","Reelle"); }
/*! * \brief Créé une copie de la littérale, c'est à dire une allocation dynamique puis copie des attributs de la source */ Litterale* FactoryLitterale::copie(Litterale* l) { Litterale* copie=nullptr; LitteraleEntiere* lEntiere = dynamic_cast<LitteraleEntiere*>(l); LitteraleReelle* lReelle = dynamic_cast<LitteraleReelle*>(l); LitteraleRationnelle* lRationnelle = dynamic_cast<LitteraleRationnelle*>(l); LitteraleComplexe* lComplexe = dynamic_cast<LitteraleComplexe*>(l); LitteraleExpression* lExpression = dynamic_cast<LitteraleExpression*>(l); LitteraleProgramme* lProgramme = dynamic_cast<LitteraleProgramme*>(l); if(lEntiere) { copie=new LitteraleEntiere(*lEntiere); } else if (lReelle) { copie=new LitteraleReelle(*lReelle); } else if (lRationnelle) { copie=new LitteraleRationnelle(*lRationnelle); } else if (lComplexe) { copie=new LitteraleComplexe(*lComplexe); } else if (lExpression) { copie=new LitteraleExpression(*lExpression); } else if (lProgramme) { copie=new LitteraleProgramme(*lProgramme); } else { throw LitteraleException("Impossible de copier, littérale inconnue"); } tl.push_back(copie); return copie; }
Litterale* Expression::evaluer() const{ QString s = value; //On vire les ' s=s.remove(0,1); s=s.remove(s.length()-1,s.length()); QString::iterator it = s.begin(); //QString contenant le résultat QString postfix; //Stack d'opérations QStack<QString> stack; //Map des opérateurs const QMap<QString, Operateur*> op_map = OperateurFactory::getMap(); //Map de tous les symboles const QMap<QString,WordIdentifier*>& interpretation_map = Controleur::getInterpretationMap(); //Traduction de Infix vers Postfix QString tmp; bool found; while(it!=s.end()){ found=false; if(*it==' ' || *it==','){ it++; continue; } //Verification que ce n'est pas une litterale avec 2 symboles exemple : Expression ou Programme if(tmp == "" && interpretation_map.find(*it)!=interpretation_map.end()){ WordIdentifier *identifier = interpretation_map.find(*it).value(); WordIdentifier *try1; WordIdentifier *try2; try1=dynamic_cast<RecursiveEncapsulatorIdentifier*>(identifier); try2=dynamic_cast<EncapsulatorIdentifier*>(identifier); if(try1 || try2) { throw LitteraleException("Litterale "+ QString(*it) +" non permise","Inconnu"); } } //On tombe dans le cas ou on a une valeur while(it!=s.end() && ((*it<='9' && *it>='0') || (interpretation_map.find(*it)!=interpretation_map.end()))){ tmp+=*it; if(it!=s.end()) it++; } //Pour vider ce qu'on a trouvé if(tmp!=""){ postfix+=tmp+" "; tmp.clear(); found=true; } //On tombe dans le cas d'un morçeau de texte if((*it<='Z' && *it>='A')){ while(it!=s.end() && ((*it<='Z' && *it>='A') || (*it<='9' && *it>='0'))){ tmp+=* it; if(it!=s.end()) it++; } if(isOperator(tmp)){ found=true; while(!stack.empty() && stack.top() != "(" && CompareOperators(stack.top(),tmp)<=0){ postfix += stack.top()+" "; stack.pop(); } stack.push(tmp); tmp.clear(); } else if(isVariable(tmp)){ found=true; Litterale* l = VariablesManager::getVariable(tmp); if(estdeType<Programme>(l)) throw OperateurException("Un programme est dans l'expression !"); QString rep = l->toString(); int length_it = s.indexOf(tmp); s.replace(tmp,rep); it = s.begin(); for(int i=0; i<length_it;i++,it++); tmp.clear(); } else throw(LitteraleException("Le mot "+tmp+" est inconnu","Inconnu")); } if(*it=='('){ stack.push(*it); if(it!=s.end()) it++; found=true; } if(*it==')'){ while(stack.top()!="("){ postfix+= stack.top()+" "; stack.pop(); } stack.pop(); if(it!=s.end()) it++; found=true; } //On tombe dans le cas d'un opérateur à caractère if(!found){ while(it!=s.end() && !op_map.contains(tmp)){ tmp+=*it++; } if(it==s.end() && !op_map.contains(tmp)) throw LitteraleException("Expression non valide dès "+tmp,"Expression"); while(!stack.empty() && stack.top() != "(" && CompareOperators(stack.top(),tmp)<=0){ postfix += stack.top()+" "; stack.pop(); } stack.push(tmp); tmp.clear(); } } while(!stack.empty()){ postfix+= stack.top() + " "; stack.pop(); } MainWindow::getInstanceMainWindow()->getNextCommande(postfix,"INFIX"); return nullptr; }
Atome::Atome(QString n){ if(isValidAtomeName(n)) nom=n; else throw LitteraleException("Nom d'atome invalide","Atome"); }
Litterale* VariablesManager::getVariable(const QString& nom){ if(var_map.contains(nom)) return (var_map.find(nom)).value()->getCopy(); throw LitteraleException(nom + " ne référence pas de variable ou programme","Variable"); }