Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <exception>
- #include <stdexcept>
- #include <cctype>
- #include <cmath>
- enum AnalysType // lul c tellement trop genial
- {
- PLUS,
- MOINS,
- FOIX,
- DIV,
- LPARENT,
- RPARENT,
- NUM,
- POW
- };
- double process(std::vector<double> listNbr, std::vector<AnalysType> listType)
- {
- bool thereAreOne{false}; //for the parentesis
- if([](std::vector<AnalysType> listType) -> bool
- {
- int nbrOpenNonClose = 0;
- for(int i = 0; i < listType.size() ; i++)
- {
- if(listType[i] == LPARENT)
- {
- nbrOpenNonClose++;
- thereAreOne = true;
- }
- if(listType[i] == RPARENT)
- {
- nbrOpenNonClose--;
- }
- if(nbrOpenNonClose < 0)
- {
- return false;
- }
- }
- if(nbrOpenNonClose == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- })
- {
- throw std::runtime_error("Why god send to me someone who don't know how to count parentesis ?\nI really hate my life.");
- }
- if(thereAreOne)
- {
- std::vecor<AnalysType> a {std::find(std::begin(listType), std::end(listType), LPARENT)++,
- std::find(std::cbegin(listType), std::cend(listType), RPARENT)--};
- int nbr {std::count(std::begin, std::find(std::begin(listType), std::end(listType), LPARENT), NUM)};
- }
- }
- double analyseEntry(std::string entry)
- {
- std::vector<double> listNbr;
- std::vector<AnalysType> listType;
- for(std::string::iterator i{std::begin(entry)} ; i != std::end(entry) ; ++i )
- {
- if(isdigit(*i))
- {
- std::string::iterator it_first_space {std::find(i, std::end(entry),
- [](char a) ->bool{return !isdigit(a);})};
- std::string mot {i, it_first_space};
- std::string::size_type sz;
- double nbr {stod(mot, &sz )};
- listNbr.push_back(nbr);
- listType.push_back(NUM);
- i = --it_first_space;
- }
- else if(ispunct(*i))
- {
- switch(*i)
- {
- case '+':
- listType.push_back(PLUS);
- break;
- case '-':
- listType.push_back(MOINS);
- break;
- case '*':
- listType.push_back(FOIX);
- break;
- case '/':
- listType.push_back(DIV);
- break;
- case '(':
- listType.push_back(LPARENT);
- break;
- case ')':
- listType.push_back(RPARENT);
- break;
- case '^':
- listType.push_back(POW);
- break;
- default:
- throw std::runtime_error("Your operator is just some boolshit. thanks for coming.");
- break;
- }
- }
- else if(isspace(*i))
- {
- i = std::find(i, std::end(entry), [](char a) -> bool
- {return !isspace(a)});
- i--;
- }
- else if(isalpha(*i))
- {
- throw std::runtime_error("DON'T PUT LETTERS. Thanks.");
- }
- return process(listNbr, listType);
- }
- return listNbr.top();
- }
- int main()
- {
- bool continuer {true};
- while(continuer)
- {
- std::string entry;
- std::cout << "> ";
- std::getline(std::cin, entry);
- try
- {
- std::cout << analyseEntry(entry) << std::endl;
- }
- catch (const std::exception& e)
- {
- std::cout << "error : " << e.what() << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement