Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. //Using c++11 regex to tokenize input string
  2. //[0-9]+ = 1 or many digits
  3. //Or [\-\+\\(\)\/\*] = "-" or "+" or "/" or "*" or "(" or ")"
  4. std::regex e ( "[0-9]+|[\-\+\\(\)\/\*]");
  5. std::sregex_iterator rend;
  6. std::sregex_iterator a( infixExpression.begin(), infixExpression.end(), e );
  7.  
  8. queue<string> infixQueue;
  9. while (a!=rend) {
  10. infixQueue.push(a->str());
  11. ++a;
  12. }
  13. return infixQueue;
  14.  
  15. tring infix; //The string to be parse (the arithmetic operation if you will)
  16. vector<string> untokenize;
  17. std::regex words_regex("[0-9]?([0-9]*[.])?[0-9]+|[\-\+\\(\)\/\*]");
  18. auto words_begin = std::sregex_iterator(infix.begin(), infix.end(), words_regex);
  19. auto words_end = std::sregex_iterator();
  20.  
  21. for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
  22. cout << (*i).str() << endl;
  23. untokenize.push_back((*i).str());
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement