Advertisement
Guest User

calculator

a guest
Dec 3rd, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <exception>
  6. #include <stdexcept>
  7. #include <cctype>
  8. #include <cmath>
  9.  
  10. enum AnalysType // lul c tellement trop genial
  11. {
  12.     PLUS,
  13.     MOINS,
  14.     FOIX,
  15.     DIV,
  16.     LPARENT,
  17.     RPARENT,
  18.     NUM,
  19.     POW
  20. };
  21.  
  22. double process(std::vector<double> listNbr, std::vector<AnalysType> listType)
  23. {
  24.     bool thereAreOne{false}; //for the parentesis
  25.     if([](std::vector<AnalysType> listType) -> bool
  26.     {
  27.         int nbrOpenNonClose = 0;
  28.         for(int i = 0; i < listType.size() ; i++)
  29.         {
  30.             if(listType[i] == LPARENT)
  31.             {
  32.                 nbrOpenNonClose++;
  33.                 thereAreOne = true;
  34.             }
  35.             if(listType[i] == RPARENT)
  36.             {
  37.                 nbrOpenNonClose--;
  38.             }
  39.             if(nbrOpenNonClose < 0)
  40.             {
  41.                 return false;
  42.             }
  43.         }
  44.         if(nbrOpenNonClose == 0)
  45.         {
  46.             return true;
  47.         }
  48.         else
  49.         {
  50.             return false;
  51.         }
  52.     })
  53.     {
  54.         throw std::runtime_error("Why god send to me someone who don't know how to count parentesis ?\nI really hate my life.");
  55.     }
  56.    
  57.     if(thereAreOne)
  58.     {
  59.         std::vecor<AnalysType> a {std::find(std::begin(listType), std::end(listType), LPARENT)++,
  60.                                   std::find(std::cbegin(listType), std::cend(listType), RPARENT)--};
  61.        
  62.         int nbr {std::count(std::begin, std::find(std::begin(listType), std::end(listType), LPARENT), NUM)};
  63.     }
  64. }
  65.  
  66. double analyseEntry(std::string entry)
  67. {
  68.     std::vector<double> listNbr;
  69.     std::vector<AnalysType> listType;
  70.    
  71.     for(std::string::iterator i{std::begin(entry)} ; i != std::end(entry) ; ++i )
  72.     {
  73.         if(isdigit(*i))
  74.         {
  75.             std::string::iterator it_first_space {std::find(i, std::end(entry),
  76.                 [](char a) ->bool{return !isdigit(a);})};
  77.                
  78.             std::string mot {i, it_first_space};
  79.            
  80.             std::string::size_type sz;
  81.             double nbr {stod(mot, &sz )};
  82.            
  83.             listNbr.push_back(nbr);
  84.            
  85.             listType.push_back(NUM);
  86.            
  87.             i = --it_first_space;
  88.         }
  89.         else if(ispunct(*i))
  90.         {
  91.             switch(*i)
  92.             {
  93.             case '+':
  94.                 listType.push_back(PLUS);
  95.                 break;
  96.             case '-':
  97.                 listType.push_back(MOINS);
  98.                 break;
  99.             case '*':
  100.                 listType.push_back(FOIX);
  101.                 break;
  102.             case '/':
  103.                 listType.push_back(DIV);
  104.                 break;
  105.             case '(':
  106.                 listType.push_back(LPARENT);
  107.                 break;
  108.             case ')':
  109.                 listType.push_back(RPARENT);
  110.                 break;
  111.             case '^':
  112.                 listType.push_back(POW);
  113.                 break;
  114.             default:
  115.                 throw std::runtime_error("Your operator is just some boolshit. thanks for coming.");
  116.                 break;
  117.             }
  118.         }
  119.         else if(isspace(*i))
  120.         {
  121.             i = std::find(i, std::end(entry), [](char a) -> bool
  122.                 {return !isspace(a)});
  123.                
  124.             i--;
  125.         }
  126.         else if(isalpha(*i))
  127.         {
  128.             throw std::runtime_error("DON'T PUT LETTERS. Thanks.");
  129.         }
  130.        
  131.         return process(listNbr, listType);
  132.     }
  133.        
  134.     return listNbr.top();
  135. }
  136.  
  137. int main()
  138. {
  139.     bool continuer {true};
  140.    
  141.    
  142.     while(continuer)
  143.     {
  144.         std::string entry;
  145.        
  146.         std::cout << "> ";
  147.        
  148.         std::getline(std::cin, entry);
  149.        
  150.         try
  151.         {
  152.             std::cout << analyseEntry(entry) << std::endl;
  153.         }
  154.         catch (const std::exception& e)
  155.         {
  156.             std::cout << "error : " << e.what() << std::endl;
  157.         }
  158.     }
  159.    
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement