Advertisement
Guest User

Untitled

a guest
Dec 4th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.62 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <map>
  5. #include <stack>
  6. #include <string>
  7. #include <vector>
  8.  
  9. #include <iostream>
  10.  
  11. // Setari
  12. #define __DEBUG__ // Afiseaza informatii despre procesul de rezolvare.
  13. #define __SUPPORT_DOUBLE__ // Activeaza suportul pentru numere reale.
  14.  
  15. // Variabile
  16.  
  17. std::vector<std::string> Infix;
  18. std::stack<std::string> operators;
  19. std::stack<std::string> Rezultat;
  20.  
  21. // Salvez operatorii si nivelul lor de prioritate intr-un dictioar pentru a face procesul de identificare mai rapid
  22. std::map<std::string, short> Operators = { {"+", 1}, {"-", 1}, {"*", 2}, {"/", 2}, {"^", 3}, {"pow", 3}, {"==", 4}, {"=", 5} };
  23.  
  24. std::ifstream fin("eval.in");
  25. std::ofstream fout("eval.out");
  26.  
  27.  
  28. // Functii
  29.  
  30. // Caut in dictionarul 'Operators' operatorul si, daca-l gasesc returnez prioritatea acestuia.
  31. int CheckOperator(const std::string& op){
  32.     const auto& it = Operators.find(op);
  33.  
  34.     if (it != Operators.end())
  35.         return it->second;
  36.  
  37.     return 0;
  38. }
  39.  
  40. int main(){
  41.     std::string expr, token;
  42.     while (std::getline(fin, token))
  43.         expr.append(token);
  44.     fin.close();
  45.    
  46.     // Analizez expresie si sterg caracterele inutile( \ ).
  47.     expr.erase(std::remove_if(expr.begin(), expr.end(), [](const char& c){ return c == '\\'; }), expr.end());
  48.     // Transform expresia in expresie postfix.
  49.    
  50.     for (size_t i = 0; i < expr.length(); ++i){
  51.         char c = expr[i];
  52.         token.clear();
  53.  
  54.         int prioritate = 0;
  55.        
  56.         token += c;
  57.         if (isdigit(c))
  58.         {
  59.            
  60.             for (++i; i < expr.length(); ++i){
  61.                 c = expr[i];
  62. #if defined(__SUPPORT_DOUBLE__)
  63.                 if (!isdigit(c) && c != '.')
  64.                     break;
  65. #else
  66.                 if (!isdigit(c))
  67.                     break;
  68. #endif
  69.                 token += c;
  70.             }
  71.             --i;
  72.             Infix.push_back(token);
  73.         }
  74.         else if(isalpha(c))
  75.         {
  76.             for (++i; i < expr.length(); ++i){
  77.                 c = expr[i];
  78.  
  79.                 if (!isalnum(c))
  80.                     break;
  81.                
  82.                 token += c;
  83.             }
  84.             std::cout << "Verificare: " << token << '\n';
  85.            
  86.             --i;
  87.  
  88.             if (CheckOperator(token))
  89.                 operators.push(token);
  90.             else
  91.                 Infix.push_back(token);
  92.         }
  93.         else if((prioritate = CheckOperator(token))){
  94.             bool numar = false;
  95.             for (++i; i < expr.length(); ++i){
  96.                 c = expr[i];
  97.                 token += c;
  98. #if defined(__SUPPORT_DOUBLE__)
  99.                 if (!CheckOperator(token) && !isdigit(c) && c != '.'){
  100.                     token.erase(token.length() - 1, token.length());
  101.                     break;
  102.                 }
  103. #else
  104.                 if (!CheckOperator(token) && !isdigit(c)){
  105.                     token.erase(token.length() - 1, token.length());
  106.                     break;
  107.                 }
  108. #endif
  109.                 if (isdigit(c))
  110.                     numar = true;
  111.             }
  112.             --i;
  113.             if (!numar){
  114.                 while(!operators.empty() && CheckOperator(operators.top()) >= prioritate)
  115.                 {
  116.                     Infix.push_back(operators.top());
  117.                     operators.pop();
  118.                 }
  119.                 operators.push(token);
  120.             }
  121.             else
  122.             {
  123.                 Infix.push_back(token);
  124.             }
  125.            
  126.         }
  127.         else if (c == '(')
  128.         {
  129.             operators.push("(");
  130.         }
  131.         else if (c == ')')
  132.         {
  133.             while (!operators.empty())
  134.             {
  135.                 if (operators.top() == "("){
  136.                     operators.pop();
  137.                     break;
  138.                 }
  139.                 Infix.push_back(operators.top());
  140.                 operators.pop();
  141.             }
  142.         }
  143.     }
  144.     while (!operators.empty())
  145.     {
  146.         Infix.push_back(operators.top());
  147.         operators.pop();
  148.  
  149.     }
  150.  
  151. #if defined(__DEBUG__)
  152.     for (const auto& ex : Infix)
  153.         fout << ex << ' ';
  154.     fout << '\n';
  155. #endif
  156.  
  157.     auto& it = Infix.begin();
  158.     for(; it != Infix.end(); ++it)
  159.     {
  160.         if (CheckOperator(*it))
  161.         {
  162.             if (*it == "+")
  163.             {
  164.                 long double b = std::stold(Rezultat.top());
  165.                 Rezultat.pop();
  166.                 long double a = std::stold(Rezultat.top());
  167.                 Rezultat.pop();
  168.  
  169.                 Rezultat.push(std::to_string(a + b));
  170.             }
  171.             else if(*it == "-")
  172.             {
  173.                 long double b = std::stold(Rezultat.top());
  174.                 Rezultat.pop();
  175.                 long double a = std::stold(Rezultat.top());
  176.                 Rezultat.pop();
  177.  
  178.                 Rezultat.push(std::to_string(a - b));
  179.             }
  180.             else if(*it == "*")
  181.             {
  182.                 long double b = std::stold(Rezultat.top());
  183.                 Rezultat.pop();
  184.                 long double a = std::stold(Rezultat.top());
  185.                 Rezultat.pop();
  186.  
  187.                 Rezultat.push(std::to_string(a * b));
  188.             }
  189.             else if(*it == "/")
  190.             {
  191.                 long double b = std::stold(Rezultat.top());
  192.                 Rezultat.pop();
  193.                 long double a = std::stold(Rezultat.top());
  194.                 Rezultat.pop();
  195.  
  196.                 Rezultat.push(std::to_string(a / b));
  197.             }
  198.             else if(*it == "^")
  199.             {
  200.                 long double b = std::stold(Rezultat.top());
  201.                 Rezultat.pop();
  202.                 long double a = std::stold(Rezultat.top());
  203.                 Rezultat.pop();
  204.  
  205.                 Rezultat.push(std::to_string(std::powl(a, b)));
  206.             }
  207.             else if(*it == "==")
  208.             {
  209.                 long double b = std::stold(Rezultat.top());
  210.                 Rezultat.pop();
  211.                 long double a = std::stold(Rezultat.top());
  212.                 Rezultat.pop();
  213.  
  214.                 Rezultat.push(std::to_string(a == b));
  215.             }
  216.             else if(*it == "pow")
  217.             {
  218.                 long double b = std::stold(Rezultat.top());
  219.                 Rezultat.pop();
  220.                 long double a = std::stold(Rezultat.top());
  221.                 Rezultat.pop();
  222.  
  223.                 Rezultat.push(std::to_string(std::powl(a, b)));
  224.             }
  225.         }
  226.         else
  227.         {
  228.             Rezultat.push(*it);
  229.         }
  230.     }
  231. #if defined(__SUPPORT_DOUBLE__)
  232.     fout << Rezultat.top() << '\n';
  233. #else
  234.     fout << std::stol(Rezultat.top()) << '\n';
  235. #endif
  236.     return 0;
  237. }
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement