Public feed
#600117
#include <iostream>#include <string>#include <sstream>#include <algorithm>#include <cmath>#include <stdlib.h>#include <list> using namespace std;namespace state { const int control = 0 ; const int id = 1 ; const int value = 2 ; const int operation = 3 ; const int floating_point = 4 ; const int exp = 5 ; const int empty = 6 ; const int ERROR = 7 ; }struct node { node *right, *left ; float value ; string token ; int type ; node (std::string::iterator b,std::string::iterator e,int type) : token (b,e), value (0.0f), type (type), right (NULL), left (NULL) { if (type == state::value) { std::stringstream ss (token) ; ss >> value ; } } } ; void lexer (string::iterator begin, string::iterator end, list<node *> & output) { std::string::iterator i = begin, b, e ; int state = state::control , count = 0 ; while ( i != end ) { switch (state) { case state::control : if ( i != end && isalpha(*i) ) { b = i ; state = state::id ; } else if ( i != end && isdigit(*i) ) { b = i ; state = state::value ; } else if ( i != end && ((*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^') || (*i == '(') || (*i == ')')) ) { b = i ; state = state::operation ; } else if ( i != end && (*i == ' ') ) { state = state::empty ; } else state = state::ERROR ; break ; case state::id : if ( i != end && (isalpha(*i) || isdigit(*i)) ) { state = state::id ; ++i ; } else { output.push_back(new node(b,i,state::id)) ; state = state::control ; } break ; case state::value : if ( i != end && isdigit(*i) ) { state = state::value ; ++i ; continue ; } else if ( i != end && (((*i) == ',') || ((*i) == '.')) ) { state = state::floating_point ; *i = '.' ; ++count ; ++i ; continue ; } else if ( i < end && ((*i == 'E') || (*i == 'e')) ) { state = state::exp ; ++i ; continue ; } else if ( i!= end && *i == ' ' ) { state = state::control ; } else { output.push_back(new node(b,i,state::value)) ; count = 0 ; state = state::control ; } break ; case state::operation : if ( i != end && ((*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^') || (*i == '(') || (*i == ')')) ) { ++i ; output.push_back(new node(b,i,state::operation)) ; state = state::control ; continue ; } break ; case state::floating_point : if ( i != end && isdigit(*i) && count < 2 ) { state = state::value ; ++i ; continue ; } else state = state::ERROR ; break ; case state::exp : if ( i != end && isdigit(*i) || (*i == '+') || (*i == '-') ) { state = state::value ; ++i ; continue ; } else state = state::ERROR ; break ; case state::empty : if ( i != end && *i == ' ' ) { state = state::empty ; ++i ; continue ; } else state = state::control ; break ; case state::ERROR : default : exit(0) ; } } if ( i == end && state != state::ERROR && state != state::control && state != state::empty ) { if ( state == state::id ) { output.push_back(new node(b,i,state::id)) ; } else if ( state == state::operation ) { output.push_back(new node(b,i,state::operation)) ; } else if ( state == state::value ) { output.push_back(new node(b,i,state::value)) ; } } } /*void parser ( list<token *> & input ) { list<token *>::iterator i ; while ( i != input.end() ) { */ std::ostream & operator << (std::ostream & out,node const & n) { if (n.type == state::value) out << n.value << endl ; else out << n.token << endl ; } int main() { list<node *>::iterator i ; string str ; list<node *> output ; getline(cin,str) ; lexer(str.begin(),str.end(),output) ; i = output.begin() ; while ( i != output.end() ) { cout << **i ; ++i ; } cin.get() ;}