Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <forward_list>
  4. using namespace std;
  5.  
  6. struct token
  7. {
  8.     int prec_level;
  9.     string tokenstr;
  10. };
  11.  
  12. std::forward_list<token*> tokens;
  13. std::forward_list<token*>::iterator it;
  14. int tokenscount = 0;
  15. void tokenize(string);
  16. void createtoken(string,int);
  17. int parse(int,int);
  18. int parse_primary();
  19. char peek_next_token();
  20. int domath(char, int, int);
  21. int conv_char_prec();
  22.  
  23. int main()
  24. {
  25.     cout << "Enter an equation with the operators /,*,+,-" << endl;
  26.     string equation;
  27.     int result;
  28.     while (true)
  29.     {
  30.         getline(cin, equation);
  31.         tokenize(equation);
  32.         it = tokens.begin();
  33.         result = parse(parse_primary(), 0);
  34.         cout << result << endl;
  35.         tokens.clear();
  36.         tokenscount = 0;
  37.     }
  38. }
  39.  
  40. void tokenize(string equation)
  41. {
  42.     string cattoken;
  43.     for (int i = 0; i < equation.length(); i++)
  44.     {
  45.         if (equation.at(i) == ' ')
  46.             continue;
  47.         else if (isdigit(equation.at(i)))
  48.         {
  49.             cattoken = equation.at(i);
  50.             for (i+= 1; isdigit(equation.at(i)); i++)
  51.             {
  52.                 ;
  53.                 cattoken.append(1, equation.at(i));
  54.             }
  55.             i--;
  56.             createtoken(cattoken, NULL);
  57.         }
  58.         cattoken = equation.at(i);
  59.         if (equation.at(i) == '+')
  60.             createtoken(cattoken, 1);
  61.         if (equation.at(i) == '-')
  62.             createtoken(cattoken, 1);
  63.         if (equation.at(i) == '*')
  64.             createtoken(cattoken, 2);
  65.         if (equation.at(i) == '/')
  66.             createtoken(cattoken, 2);
  67.     }
  68.     createtoken(";", -1);
  69.    
  70. }
  71.  
  72. int parse(int lhs,int min_preclevel)
  73. {
  74.     int rhs;
  75.     int lookahead = conv_char_prec();
  76.     char oper = peek_next_token();
  77.     while (lookahead >= min_preclevel)
  78.     {
  79.         int op = lookahead;
  80.         rhs = parse_primary();
  81.         lookahead = conv_char_prec();
  82.         while (lookahead >= op)
  83.         {
  84.             rhs = parse(rhs, lookahead);
  85.             lookahead = conv_char_prec();
  86.         }
  87.         lhs = domath(oper, lhs, rhs);
  88.        
  89.     }
  90.     return lhs;
  91.    
  92. }
  93.  
  94. void createtoken(string tokenchar,int prec_level)
  95. {
  96.     token * tokenptr = new struct token;
  97.     tokenptr->prec_level = prec_level;
  98.     tokenptr->tokenstr = tokenchar;
  99.     tokens.push_front(tokenptr);
  100. }
  101.  
  102. int parse_primary()
  103. {
  104.     int lhs;
  105.     advance(it, tokenscount);
  106.     tokenscount = 1;
  107.     lhs = stoi((*it)->tokenstr, nullptr, 10);
  108.     return lhs;
  109.    
  110.    
  111. }
  112.  
  113. char peek_next_token()
  114. {
  115.     advance(it, tokenscount);
  116.     return (*it)->tokenstr.at(0);
  117. }
  118.  
  119. int conv_char_prec()
  120. {
  121.     char lookahead = peek_next_token();
  122.     int lookaheadprec;
  123.     if (lookahead == '+')
  124.         lookaheadprec = 1;
  125.     if (lookahead == '-')
  126.         lookaheadprec = 1;
  127.     if (lookahead == '*')
  128.         lookaheadprec = 2;
  129.     if (lookahead == '/')
  130.         lookaheadprec = 2;
  131.     if (lookahead == ';')
  132.         lookaheadprec = -1;
  133.     return lookaheadprec;
  134. }
  135.  
  136. int domath(char oper, int lhs, int rhs)
  137. {
  138.     int result;
  139.     if (oper == '+')
  140.         result = lhs + rhs;
  141.     if (oper == '-')
  142.         result = lhs - rhs;
  143.     if (oper == '*')
  144.         result = lhs * rhs;
  145.     if (oper == '/')
  146.         result = lhs / rhs;
  147.  
  148.     return result;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement