#include <iostream>
using namespace std;
/*
Syntax Darstellung
Beispiel arithmetischer Ausdruck
"1 + (2 * 3)"
Struktur des Ausdrucks explizit darstellen
Plus
/ \
1 Mult
/ \
2 3
Wie koennen wir obige Baumstruktur in C++ darstellen?
Wir verwenden eine Klassenhierarchie. Siehe unten.
*/
class Exp {};
class Num : public Exp {
int i;
public:
Num(int x) { i = x; }
};
class Plus : public Exp {
Exp l, r;
public:
Plus(Exp x, Exp y) { l = x; r = y; }
};
class Mult : public Exp {
Exp l, r;
public:
Mult(Exp x, Exp y) { l = x; r = y; }
};
void test() {
Exp e;
e = Num(1);
Exp e2;
e2 = Mult(Num(2),Num(3));
Exp e3;
e3 = Plus(e,e2);
Exp e4 = Plus(Num(1),Mult(Num(2),Num(3))); // 1 + (2 * 3)
}
int main()
{
test();
return 0;
}