xc
Rama.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 //Rama.h
22 
23 #ifndef RAMA_H
24 #define RAMA_H
25 
26 #include "ProtoExpr.h"
27 #include "Segnal.h"
28 #include "Operando.h"
29 
30 class ExprFunctor;
31 class ConjVariables;
32 
33 class Rama : public ProtoExpresion
34  {
35  private:
36  static const double &no_numero;
37 
38  inline void BorraIzdo(void)
39  {
40  if(izdo)
41  {
42  izdo->BorraHijos();
43  delete izdo;
44  izdo= NULL;
45  }
46  }
47  inline void BorraDcho(void)
48  {
49  if(dcho)
50  {
51  dcho->BorraHijos();
52  delete dcho;
53  dcho= NULL;
54  }
55  }
56  inline void BorraHijos(void)
57  {
58  BorraIzdo();
59  BorraDcho();
60  }
61  friend class ArbolExpr;
62  friend class OpUnario;
63  friend class OpBinario;
64  Rama(const Operando *s,Rama *i,Rama *j= NULL);
65 
66  friend Rama *ContraeIzdo(Rama *);
67  friend Rama *ContraeDcho(Rama *);
68  protected:
69 
70  const Operando *data;
71 
72  Rama *izdo;
73  Rama *dcho;
74 
75  public:
76  friend class OpProd;
77  friend class OpSuma;
78 
80  Rama(void)
81  : data(NULL), izdo(NULL), dcho(NULL) {}
82 
83  explicit Rama(const double &d);
84  Rama(const Rama &);
85  Rama &operator=(const Rama &);
86  bool operator==(const Rama &r2) const;
87  bool operator==(const double &d) const;
88 
89  Rama *getCopy(void) const
90  {
91  if(izdo)
92  {
93  if(dcho) //Izquierdo y derecho.
94  return new Rama(data,izdo->getCopy(),dcho->getCopy());
95  else //Sólo izquierdo.
96  return new Rama(data,izdo->getCopy(),NULL);
97  }
98  else
99  {
100  if(dcho) //Sólo derecho.
101  return new Rama(data,NULL,dcho->getCopy());
102  else //Ni derecho ni izquierdo.
103  return new Rama(data,NULL,NULL);
104  }
105  }
106  virtual ~Rama(void);
107  inline void PutIzdo(Rama *i)
108  {
109  BorraIzdo();
110  if(i) izdo= i->getCopy();
111  }
112  inline void PutDcho(Rama *j)
113  {
114  BorraDcho();
115  if(j) dcho= j->getCopy();
116  }
117 /* inline void Put(const Operando *s,Rama *i,Rama *j) */
118 /* { */
119 /* data= s; */
120 /* izdo= i; */
121 /* PutDcho(j); */
122 /* } */
123  inline const Operando *GetData(void) const
124  { return data; }
125  inline Rama *getLeft(void) const
126  { return izdo; }
127  inline Rama *getRight(void) const
128  { return dcho; }
129  Clase GetClase(void) const;
130  const std::string &StrClase(void) const;
131  inline const Operando *DatoIzdo(void)
132  { return izdo->data; }
133  inline const Operando *DatoDcho(void)
134  { return dcho->data; }
135  inline int GetPrioridad(void) const;
136  bool Evaluable(void) const;
137  bool EsToNum(void) const;
138  const Operando &Opera(void);
139  static void Opera(Rama *raiz);
140  inline const double &GetValor(void) const
141  { return ((GetClase() == operando) ? data->GetValor() : no_numero); }
142  void Asigna(Operando *po,const Rama *p)
143  //Sustituye todas las ocurrencias de po por p en el arbol.
144  {
145  if(dcho)
146  if(dcho->data == po)
147  {
148  BorraDcho();
149  if(p) dcho= p->getCopy();
150  }
151  if(izdo)
152  if(izdo->data == po)
153  {
154  BorraIzdo();
155  if(p) izdo= p->getCopy();
156  }
157  }
158  void Borra(void)
159  {
160  BorraHijos();
161  data= NULL;
162  }
163  void GetVariables(ConjVariables &cv) const;
164  const std::string &GetFullString(void) const;
165  friend Rama *distrib(Rama *raiz);
166  friend std::ostream &operator <<(std::ostream &stream,const Rama &e);
167  };
168 
169 Rama *ContraeIzdo(Rama *raiz);
170 Rama *ContraeDcho(Rama *raiz);
171 
172 Rama diferencia(const Rama &raiz,const Variable &v);
173 Rama *simplifica(Rama *raiz);
174 Rama *distrib(Rama *raiz);
175 void Escribe(const Rama *raiz,const int &prior, std::ostream &stream);
176 void RecorrePreorden(Rama *raiz,const ExprFunctor &f);
177 void RecorreEnorden(Rama *raiz,const ExprFunctor &f);
178 void RecorrePostorden(Rama *raiz,const ExprFunctor &f);
179 
180 inline bool operator==(const double &d,const Rama &r)
181  { return (r==d); }
182 
183 #endif
184 
185 
186 
Árbol que representa una expresión matemática.
Definition: ArbolExpr.h:41
Definition: ProtoExpr.h:35
Definition: OpUnario.h:29
Definition: Variable.h:31
Definition: ConjVariables.h:30
Operando.
Definition: Operando.h:42
Definition: OpBinario.h:50
Definition: Rama.h:33
friend Rama * distrib(Rama *raiz)
Aplica la propiedad distributiva a través del arbol.
Definition: Rama.cc:208
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
const Operando & Opera(void)
Ejecuta el operador de éste objeto.
Definition: Rama.cc:145
const std::string & GetFullString(void) const
Devuelve una cadena de caracteres que representa a la expresión representando los literales con la má...
Definition: Rama.cc:277
virtual ~Rama(void)
Destructor.
Definition: Rama.cc:104
Operador binario.
Definition: OpBinario.h:32
Rama(void)
Constructor por defecto.
Definition: Rama.h:80
Definition: ExprFunctor.h:28
bool Evaluable(void) const
Devuelve verdadero si la rama es evaluable.
Definition: Rama.cc:166
Rama & operator=(const Rama &)
Operador asignación.
Definition: Rama.cc:53
Definition: OpBinario.h:77
bool operator==(const Rama &r2) const
Comparison operator.
Definition: Rama.cc:64