Advertisement
Guest User

SS2018 A5 a,b,c fix

a guest
Feb 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. class Exp {
  8. public:
  9.     Exp() {};
  10.  
  11.     virtual string pretty() = 0;
  12.     virtual int eval() = 0;
  13.     virtual Exp* simp() { return this; }
  14. };
  15.  
  16. class IntExp : public Exp {
  17. public:
  18.     int val;
  19. public:
  20.     IntExp(int _val) {
  21.         val = _val;
  22.     }
  23.  
  24.     string pretty() {
  25.         stringstream ss;
  26.         ss << val; // Konvertierung von Integer return ss.str(); // in einen String } };
  27.         return ss.str();
  28.     }
  29.  
  30.     int eval() {
  31.         return this->val;
  32.     }
  33. };
  34.  
  35. class PlusExp : public Exp {
  36. public:
  37.     Exp *e1;
  38.     Exp *e2;
  39. public:
  40.     PlusExp(Exp *_e1, Exp *_e2) {
  41.         e1 = _e1;
  42.         e2 = _e2;
  43.     }
  44.  
  45.     string pretty() {
  46.         return "(" + e1->pretty() + " + " + e2->pretty() + ")"; // 1 + 2 * 3 != (1 + 2) * 3
  47.     }
  48.  
  49.     int eval() {
  50.         return this->e1->eval() + this->e2->eval(); //rekursion
  51.     }
  52.  
  53.     Exp* simp() {
  54.         //teile
  55.         this->e1 = this->e1->simp();
  56.         this->e2 = this->e2->simp();
  57.  
  58.         //0 + e => e
  59.         if (this->e1->eval() == 0)
  60.             return this->e2;
  61.         //e + 0 => e
  62.         if (this->e2->eval() == 0)
  63.             return this->e1;
  64.  
  65.         return this; //nicht einfacher gemacht
  66.     }
  67. };
  68.  
  69. class MultExp : public Exp {
  70. public:
  71.     Exp *e1;
  72.     Exp *e2;
  73. public:
  74.     MultExp(Exp *_e1, Exp *_e2) {
  75.         e1 = _e1;
  76.         e2 = _e2;
  77.     }
  78.  
  79.     string pretty() {
  80.         return e1->pretty() + " * " + e2->pretty(); //kp was hier der Fehler ist
  81.     }
  82.  
  83.     int eval() {
  84.         return this->e1->eval() * this->e2->eval();
  85.     }
  86.  
  87.     Exp* simp() {
  88.         //Teile vereinfachen
  89.         this->e1 = this->e1->simp();
  90.         this->e2 = this->e2->simp();
  91.  
  92.         //1 * e => e
  93.         if (this->e1->eval() == 1)
  94.             return this->e2;
  95.         //e * 1 => e
  96.         if (this->e2->eval() == 1)
  97.             return this->e1;
  98.  
  99.         return this; //nicht einfacher gemacht
  100.     }
  101. };
  102.  
  103. int main() {
  104.     //tests
  105.     Exp *e = new MultExp(new PlusExp(new IntExp(1), new IntExp(2)), new IntExp(3));
  106.  
  107.     cout << e->pretty() << endl;
  108.     cout << e->eval() << endl;
  109.     cout << e->simp()->pretty() << endl;
  110.  
  111.     Exp *f = new MultExp(new PlusExp(new IntExp(0), e), new IntExp(1));
  112.  
  113.     cout << f->pretty() << endl;
  114.     cout << f->eval() << endl;
  115.     cout << f->simp()->pretty() << endl;
  116.  
  117.     return 0x0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement