Advertisement
Tucancitto

Lab3 - Pb4

Apr 9th, 2021 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <vector>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. constexpr auto SPATIU = 5;
  9.  
  10. void citire(vector<char>& expresie)
  11. {
  12.     std::ifstream fin("expresie.in");
  13.     if (fin.peek() == EOF) {
  14.         cout << "Expresia este vida. \n";
  15.         return;
  16.     }
  17.  
  18.     char c;
  19.     while (fin >> c)
  20.         if (c == ' ')
  21.             continue;
  22.         else
  23.             expresie.push_back(c);
  24.     fin.close();
  25. }
  26.  
  27. int precedenta(char op)
  28. {
  29.     if (op == '(')
  30.         return 0;
  31.     if (op == '+' or op == '-')
  32.         return 1;
  33.     if (op == '*' or op == '/')
  34.         return 2;
  35.     return 3;
  36. }
  37.  
  38. void constructie(vector<char> expresie, vector<char>& formaPoloneza)
  39. {
  40.     std::stack<char> S;
  41.  
  42.     for (auto c : expresie)
  43.     {
  44.         if (isdigit(c))
  45.             formaPoloneza.push_back(c);
  46.         else
  47.             if (c == '(')
  48.                 S.push(c);
  49.             else
  50.                 if (c == ')')
  51.                 {
  52.                     while (!S.empty() && S.top() != '(')
  53.                     {
  54.                         char op = S.top();
  55.                         S.pop();
  56.                         formaPoloneza.push_back(op);
  57.                     }
  58.  
  59.                     if (!S.empty() && S.top() == '(')
  60.                         S.pop();
  61.                 }
  62.                 else
  63.                 {
  64.                     while (!S.empty() && precedenta(c) <= precedenta(S.top()))
  65.                     {
  66.                         char op = S.top();
  67.                         S.pop();
  68.                         formaPoloneza.push_back(op);
  69.                     }
  70.                     S.push(c);
  71.                 }
  72.     }
  73.  
  74.     while (!S.empty())
  75.     {
  76.         char op = S.top();
  77.         S.pop();
  78.         formaPoloneza.push_back(op);
  79.     }
  80. }
  81.  
  82. float efectuareOperatie(float x, float y, char op)
  83. {
  84.     if (op == '+')
  85.         return x + y;
  86.     if (op == '-')
  87.         return x - y;
  88.     if (op == '*')
  89.         return x * y;
  90.     if (op == '/')
  91.     {
  92.         if (y != 0)
  93.             return (float)x / y;
  94.         else
  95.             return FLT_MIN;
  96.     }
  97. }
  98.  
  99. struct NOD
  100. {
  101.     char info = 'N';
  102.     NOD* st = NULL, * dr = NULL;
  103. };
  104.  
  105. struct ARBORE
  106. {
  107.     NOD* radacina = new NOD;
  108.  
  109.     bool empty()
  110.     {
  111.         if (radacina == NULL)
  112.             return true;
  113.         return false;
  114.     }
  115.  
  116.     void formareArbore(vector<char> formaPoloneza)
  117.     {
  118.         stack<NOD*> stiva;
  119.  
  120.         for (auto c : formaPoloneza)
  121.             if (isdigit(c))
  122.             {
  123.                 NOD* nou = new NOD;
  124.                 nou->info = c;
  125.                 stiva.push(nou);
  126.             }
  127.             else
  128.             {
  129.                 NOD* y = stiva.top();
  130.                 stiva.pop();
  131.                 NOD* x = stiva.top();
  132.                 stiva.pop();
  133.  
  134.                 NOD* op = new NOD;
  135.                 op->info = c;
  136.                 op->st = x, op->dr = y;
  137.                 stiva.push(op);
  138.             }
  139.  
  140.         radacina = stiva.top();
  141.         stiva.pop();
  142.     }
  143.  
  144.     float evaluare(NOD* rez)
  145.     {
  146.         if (rez == NULL)
  147.             return 0;
  148.  
  149.         if (rez->st == NULL && rez->dr == NULL)
  150.             return (rez->info - '0');
  151.  
  152.         float x = evaluare(rez->st);
  153.         float y = evaluare(rez->dr);
  154.         return efectuareOperatie(x, y, rez->info);
  155.     }
  156.  
  157.     int inaltime(NOD* rad)
  158.     {
  159.         if (rad == NULL)
  160.             return -1;
  161.  
  162.         int inaltimeSt = inaltime(rad->st);
  163.         int inaltimeDr = inaltime(rad->dr);
  164.  
  165.         return max(inaltimeSt, inaltimeDr) + 1;
  166.     }
  167.  
  168.     void afisareNivel(NOD* rad, int nivel)
  169.     {
  170.         if (rad == NULL)
  171.             return;
  172.  
  173.         if (nivel == 0)
  174.             cout << rad->info << ' ';
  175.         else
  176.         {
  177.             afisareNivel(rad->st, nivel - 1);
  178.             afisareNivel(rad->dr, nivel - 1);
  179.         }
  180.     }
  181.  
  182.     void BFS()
  183.     {
  184.         int h = inaltime(radacina);
  185.         for (int nivel = 0; nivel <= h; ++nivel)
  186.             afisareNivel(radacina, nivel);
  187.         cout << endl;
  188.     }
  189.  
  190.     void afisareArbore(NOD* rad, int spatiu)
  191.     {
  192.         if (rad == NULL)
  193.             return;
  194.         spatiu += SPATIU;
  195.  
  196.         afisareArbore(rad->dr, spatiu);
  197.         cout << endl;
  198.  
  199.         for (int index = SPATIU; index < spatiu; cout << ' ', ++index);
  200.  
  201.         cout << rad->info;
  202.         afisareArbore(rad->st, spatiu);
  203.     }
  204.  
  205.     void stergereArbore(NOD*& rad)
  206.     {
  207.         if (rad == NULL)
  208.             return;
  209.  
  210.         stergereArbore(rad->st);
  211.         stergereArbore(rad->dr);
  212.  
  213.         delete rad;
  214.         rad = nullptr;
  215.     }
  216. };
  217.  
  218. void afisare(vector<char> sir)
  219. {
  220.     for (auto caract : sir)
  221.         cout << caract;
  222.     cout << endl;
  223. }
  224.  
  225. void eliberareMemorie(vector<char>& sir)
  226. {
  227.     sir.clear();
  228.     sir.shrink_to_fit();
  229. }
  230.  
  231. int main()
  232. {
  233.     vector<char> expresie, formaPoloneza;
  234.     citire(expresie);
  235.     if (expresie.empty()) {
  236.         return 0;
  237.     }
  238.  
  239.     cout << "Expresia: ";
  240.     afisare(expresie);
  241.  
  242.     constructie(expresie, formaPoloneza);
  243.     cout << "Forma poloneza postfixata: ";
  244.     afisare(formaPoloneza);
  245.  
  246.     ARBORE arbore;
  247.     arbore.formareArbore(formaPoloneza);
  248.  
  249.     cout << "Arborele sintactic: \n";
  250.     arbore.afisareArbore(arbore.radacina, 0);
  251.     cout << endl << endl;
  252.  
  253.     cout << "Parcurgerea in latime: ";
  254.     arbore.BFS();
  255.  
  256.     float rezultat = arbore.evaluare(arbore.radacina);
  257.     cout << "Evaluarea expresiei: ";
  258.     if (rezultat != FLT_MIN)
  259.         cout << rezultat << endl;
  260.     else
  261.         cout << "nu se poate imparti la 0. \n";
  262.  
  263.     eliberareMemorie(expresie);
  264.     eliberareMemorie(formaPoloneza);
  265.     arbore.stergereArbore(arbore.radacina);
  266.  
  267.     return 0;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement