Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 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 = tokens.begin();
  14. int tokenscount = -1;
  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.         result = parse(parse_primary(), 0);
  33.         cout << result << endl;
  34.         tokens.clear();
  35.         tokenscount = -1;
  36.     }
  37. }
  38.  
  39. void tokenize(string equation)
  40. {
  41.     string cattoken;
  42.     for (int i = 0; i != '\0'; i++)
  43.     {
  44.         if (equation.at(i) == ' ')
  45.             continue;
  46.         else if (isdigit(equation.at(i)))
  47.         {
  48.             cattoken = equation.at(i);
  49.             for (i+= 1; isdigit(equation.at(i)); i++)
  50.             {
  51.                 ;
  52.                 cattoken.append(1, equation.at(i));
  53.             }
  54.             i--;
  55.             createtoken(cattoken, NULL);
  56.         }
  57.         cattoken = equation.at(i);
  58.         if (equation.at(i) == '+')
  59.             createtoken(cattoken, 1);
  60.         if (equation.at(i) == '-')
  61.             createtoken(cattoken, 1);
  62.         if (equation.at(i) == '*')
  63.             createtoken(cattoken, 2);
  64.         if (equation.at(i) == '/')
  65.             createtoken(cattoken, 2);
  66.     }
  67.     createtoken(";", -1);
  68.    
  69. }
  70.  
  71. int parse(int lhs,int min_preclevel)
  72. {
  73.     int rhs;
  74.     int lookahead = conv_char_prec();
  75.     char oper = peek_next_token();
  76.     while (lookahead >= min_preclevel)
  77.     {
  78.         int op = lookahead;
  79.         rhs = parse_primary();
  80.         lookahead = conv_char_prec();
  81.         while (lookahead >= op)
  82.         {
  83.             rhs = parse(rhs, lookahead);
  84.             lookahead = conv_char_prec();
  85.         }
  86.         lhs = domath(oper, lhs, rhs);
  87.        
  88.     }
  89.     return lhs;
  90.    
  91. }
  92.  
  93. void createtoken(string tokenchar,int prec_level)
  94. {
  95.     token * tokenptr = new struct token;
  96.     tokenptr->prec_level = prec_level;
  97.     tokenptr->tokenstr = tokenchar;
  98.     tokens.push_front(tokenptr);
  99. }
  100.  
  101. int parse_primary()
  102. {
  103.     int lhs;
  104.     tokenscount++;
  105.     advance(it, tokenscount);
  106.     if (it != tokens.end())
  107.         cout << "1";
  108.     lhs = stoi((*it)->tokenstr, nullptr, 10);
  109.     return lhs;
  110.    
  111.    
  112. }
  113.  
  114. char peek_next_token()
  115. {
  116.     advance(it, tokenscount);
  117.     return (*it)->tokenstr.at(0);
  118. }
  119.  
  120. int conv_char_prec()
  121. {
  122.     tokenscount++;
  123.     char lookahead = peek_next_token();
  124.     int lookaheadprec;
  125.     if (lookahead == '+')
  126.         lookaheadprec = 1;
  127.     if (lookahead == '-')
  128.         lookaheadprec = 1;
  129.     if (lookahead == '*')
  130.         lookaheadprec = 2;
  131.     if (lookahead == '/')
  132.         lookaheadprec = 2;
  133.     if (lookahead == ';')
  134.         lookaheadprec = -1;
  135.     return lookaheadprec;
  136. }
  137.  
  138. int domath(char oper, int lhs, int rhs)
  139. {
  140.     int result;
  141.     if (oper == '+')
  142.         result = lhs + rhs;
  143.     if (oper == '-')
  144.         result = lhs - rhs;
  145.     if (oper == '*')
  146.         result = lhs * rhs;
  147.     if (oper == '/')
  148.         result = lhs / rhs;
  149.  
  150.     return result;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement