All pastes #594320 Raw Edit

piromanas

public cpp v1 · immutable
#594320 ·published 2007-06-28 21:34 UTC
rendered paste body
#include <iostream>#include <string>#include <sstream>#include <algorithm>#include <cmath>#include <stdlib.h>#include <list> using namespace std;struct token {    token *right, *left ;    double value ;    char operation ;    string ID ;    token() : right(NULL), left(NULL), value(0), operation(0) {} ;    } ;    // the tabe of states     const int E = 0 ;  const int ID = 1 ; const int V = 2 ; const int O = 3 ; const int ERROR = 4 ;//-------------------------   template<typename iterator>void parse (iterator begin,iterator end,list<token *> & output) {        iterator i, b, e ;    int state = E ;    while ( i < end ) {        switch (state) {                            case    E :                             if ( isalpha(*i) ) {                                b = i ;                                state = ID ;                            }                            else if ( isdigit(*i) ) {                                     b = i ;                                     state = V ;                            }                            else if ( (*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^') ) {                                    state = O ;                            }                            else if ( *i == " " ) {                                    state = E ;                            }                            else state = ERROR ;                            break ;                                case    ID :                                            if ( isalpha(*i) || isdigit(*i) ) {                                state = ID ;                                ++i ;                            }                                                else {                                    token* t = new token ;                                    e = i ;                                    string s(b,e) ;                                    t->ID = s ;                                    output.push_back(t) ;                                    state = E ;                            }                            break ;                case    V :                                             if ( isdigit(*i) ) {                                                                state = V ;                                ++i ;                            }                            else {                                    token* t = new token ;                                    e = i ;                                    string s1 (b,e) ;                                    istringstream stream (s1) ;                                    double d ;                                    stream >> d ;                                    t->value = d ;                                    output.push_back(t) ;                                    state = E ;                            }                            break ;                case    O :                             if ( (*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^') ) {                                token* t = new token ;                                t->operation = *i ;                                output.push_back(t) ;                                ++i ;                                state = E ;                            }                            break ;                                case    ERROR :                                 default :                                             exit ;                                }        }    }