xc
ArbolExpr.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. Pérez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //ArbolExpr.h
22 
23 #ifndef ARBOLEXPR_H
24 #define ARBOLEXPR_H
25 
26 #include "Rama.h"
27 #include <iostream>
28 #include <stack>
29 #include <vector>
30 #include "ExprBase.h"
31 #include "PilaPunteros.h"
32 
33 class Operador;
34 class ConjVariables;
35 class ExprPostfija;
36 class VarExpr;
37 class ExprFunctor;
38 class MapValores;
39 
41 class ArbolExpr : public ExprBase
42  {
43  private:
44  friend class Lexico;
45 
46  Rama *raiz;
47 
48  typedef PilaPunteros<Rama> Pila;
49  Rama *NuevoNodo(const Operando *s, Rama *izdo= NULL, Rama *dcho= NULL)
50  {
51  Rama *nuevo_nodo= new Rama(s,izdo,dcho);
52  return nuevo_nodo;
53  }
54  inline void InsertaOperando(Pila &pila,const Operando *s)
55  {
56  Rama *opndo= NuevoNodo(s,NULL,NULL);
57  pila.push(opndo);
58  }
59  inline void InsertaOpUnario(Pila &pila,const Operando *s)
60  {
61  Rama *opndo= pila.Pop();
62  Rama *opuni= NuevoNodo(s,NULL,opndo);
63  pila.push(opuni);
64  }
65  inline void InsertaOpBinario(Pila &pila,const Operando *s)
66  {
67  Rama *opndo2= pila.Pop();
68  Rama *opndo1= pila.Pop();
69  Rama *opbin= NuevoNodo(s,opndo1,opndo2);
70  pila.push(opbin);
71  }
72  inline Rama *FinExpresion(Pila &pila)
73  {
74  Rama *retval= pila.Pop();
75  if (pila.size() > 0) std::cerr << "Error en la expresion" << std::endl;
76  return retval;
77  }
78  protected:
79  Rama *Traduce(const ExprPostfija &e);
80  Rama *Traduce(const std::string &cadena_entrada);
81  void InicFromStr(const std::string &str);
82  void Opera(void);
83  void OperaToNum(void);
84  inline const double &GetValor(void) const
85  { return raiz->GetValor(); }
86  ArbolExpr &AplicaOperador(const Operador *op,const ArbolExpr &a2);
87  void Neg(void);
88  void Abs(void);
89  void Sqrt(void);
90  void Dif(const std::string &var);
91  inline void Simplifica(void)
92  { if(raiz) raiz= simplifica(raiz); }
93  inline void Distrib(void)
94  { if(raiz) raiz= distrib(raiz); }
95  public:
96  ArbolExpr(void);
97  ArbolExpr(const std::string &str);
98  ArbolExpr(const ArbolExpr &otro);
99  explicit ArbolExpr(const double &d);
100  virtual bool operator==(const ArbolExpr &) const;
101  ArbolExpr &operator=(const ArbolExpr &otro);
102  ArbolExpr &operator+=(const ArbolExpr &a2);
103  ArbolExpr &operator-=(const ArbolExpr &a2);
104  ArbolExpr &operator*=(const ArbolExpr &a2);
105  ArbolExpr &operator/=(const ArbolExpr &a2);
106  inline ArbolExpr operator-(void)
107  {
108  ArbolExpr retval(*this);
109  retval.Neg();
110  return retval;
111  }
112  ArbolExpr &pow(const ArbolExpr &a2);
113  const double &ToNum(void) const;
115  const double &ToNum(const std::string &palabra,const double &d) const;
116  const double &ToNum(const MapValores &) const;
117  inline virtual bool Evaluable(void) const
118  {
119  bool retval= false;
120  if(raiz) retval= raiz->Evaluable();
121  return retval;
122  }
123  void RecorrePreorden(const ExprFunctor &f);
124  void RecorreEnorden(const ExprFunctor &f);
125  void RecorrePostorden(const ExprFunctor &f);
126  void RecorrePreorden(const ExprFunctor &f) const;
127  void RecorreEnorden(const ExprFunctor &f) const;
128  void RecorrePostorden(const ExprFunctor &f) const;
129  void Asigna(const std::string &palabra,const ArbolExpr &a);
130  static void Asigna(const std::string &palabra,const double &d)
131  { ExprBase::Asigna(palabra,d); }
132  void DesAsigna(const std::string &palabra)
133  { ExprBase::DesAsigna(palabra); }
134 
135  ConjVariables Variables(void) const;
136  bool TieneVariable(const std::string &palabra) const;
137  bool TieneVariable(const VarExpr &var) const;
138  bool TieneVariable(const Variable &var) const;
139  int NumVariables(void) const;
140  std::vector<std::string> getNamesOfVariables(void) const;
141 
142  std::string GetString(void) const;
143  const std::string &GetFullString(void) const;
144 
145  void Print(std::ostream &os) const;
146  friend std::ostream &operator<<(std::ostream &stream,const ArbolExpr &a);
147  friend ArbolExpr abs(const ArbolExpr &);
148  friend ArbolExpr sqrt(const ArbolExpr &);
149  virtual ~ArbolExpr(void);
150  };
151 
152 std::ostream &operator<<(std::ostream &stream,const ArbolExpr &a);
153 std::istream &operator>>(std::istream &stream,ArbolExpr &a);
154 
155 ArbolExpr abs(const ArbolExpr &);
156 ArbolExpr sqrt(const ArbolExpr &);
157 
158 #endif
159 
160 
161 
162 
void Opera(void)
Opera todo el árbol.
Definition: ArbolExpr.cc:185
void Print(std::ostream &os) const
Imprime la expresión en el stream que se pasa como parámetro.
Definition: ArbolExpr.cc:322
Árbol que representa una expresión matemática.
Definition: ArbolExpr.h:41
Clase base de los operadores unarios y binarios.
Definition: Operador.h:30
ArbolExpr(void)
Constructor por defecto.
Definition: ArbolExpr.cc:34
void Sqrt(void)
Aplica el operador "raíz cuadrada" a la expresión contenida en el árbol.
Definition: ArbolExpr.cc:240
Definition: Variable.h:31
Especifica valores de variables.
Definition: MapValores.h:32
Definition: ConjVariables.h:30
Definition: ExprPostfija.h:33
friend ArbolExpr sqrt(const ArbolExpr &)
Devuelve la raíz cuadrada del argumento.
Definition: ArbolExpr.cc:382
virtual bool operator==(const ArbolExpr &) const
Comparison operator.
Definition: ArbolExpr.cc:63
int NumVariables(void) const
Devuelve el número de variables que intervienen en la expresión.
Definition: ArbolExpr.cc:314
void Dif(const std::string &var)
Diferencia la expresión respecto a la variable cuyo identificador se pasa como parámetro.
Definition: ArbolExpr.cc:248
Operando.
Definition: Operando.h:42
std::string GetString(void) const
Devuelve una text string con el contenido de la expresión.
Definition: ArbolExpr.cc:326
ConjVariables Variables(void) const
Devuelve el conjunto de variables que aparecen en la expresión.
Definition: ArbolExpr.cc:280
void Abs(void)
Aplica el operador "valor absoluto" a la expresión contenida en el árbol.
Definition: ArbolExpr.cc:233
std::vector< std::string > getNamesOfVariables(void) const
Return the names of the variables.
Definition: ArbolExpr.cc:318
friend std::ostream & operator<<(std::ostream &stream, const ArbolExpr &a)
Inserta en stream de salida.
Definition: ArbolExpr.cc:342
Definition: Rama.h:33
void OperaToNum(void)
Procesa los operadores to_num que haya en la expresión.
Definition: ArbolExpr.cc:192
static void Asigna(const std::string &palabra, const double &d)
Assigns to the variable named &#39;palabra&#39; the value being passed as parameter.
Definition: ProtoExpr.cc:72
bool TieneVariable(const std::string &palabra) const
Return true if the expression depends on the variable name being passed as parameter.
Definition: ArbolExpr.cc:293
Definition: PilaPunteros.h:29
ArbolExpr & operator=(const ArbolExpr &otro)
Operador asignación.
Definition: ArbolExpr.cc:82
Definition: Lexico.h:42
Definition: ExprFunctor.h:28
Definition: ExprBase.h:28
bool Evaluable(void) const
Devuelve verdadero si la rama es evaluable.
Definition: Rama.cc:166
static void DesAsigna(const std::string &palabra)
Removes the assignment to the variable which name is being passed as parameter.
Definition: ProtoExpr.cc:76
friend ArbolExpr abs(const ArbolExpr &)
Devuelve el valor absoluto del argumento.
Definition: ArbolExpr.cc:374
void Neg(void)
Cambia de signo la expresión contenida en el árbol.
Definition: ArbolExpr.cc:226
Definition: VarExpr.h:28
const std::string & GetFullString(void) const
Devuelve una text string con el contenido de la expresión representando los valores reales con toda l...
Definition: ArbolExpr.cc:334