Public feed
#598463
#include <iostream>#include <string>#include <sstream>#include <algorithm>#include <cmath>#include <stdlib.h>#include <list> using namespace std;struct token { token *right, *left ; float 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 floating_point = 4 ; const int EXP = 5 ; const int ERROR = 6 ;//------------------------- void lexer (string::iterator begin, string::iterator end, list<token *> & output) { std::string::iterator i = begin, b, e ; int state = E , count = 0 ; while ( i != end ) { switch (state) { case E : if ( i != end && isalpha(*i) ) { b = i ; state = ID ; } else if ( i != end && isdigit(*i) ) { b = i ; state = V ; } else if ( i != end && ((*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^')) ) { state = O ; } else if ( i != end && ((*i) == ' ') ) { ++i ; state = E ; } else state = ERROR ; break ; case ID : if ( i != end && (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 ( i != end && isdigit(*i) ) { state = V ; ++i ; continue ; } else if ( i != end && (((*i) == ',') || ((*i) == '.')) ) { state = floating_point ; *i = '.' ; ++count ; ++i ; continue ; } else if ( i < end && ((*i == 'E') || (*i == 'e')) ) { state = EXP ; ++i ; continue ; } else if ( i!= end && *i == ' ' ) { token* t = new token ; //++i ; e = i ; string s1 (b,e) ; istringstream stream (s1) ; float d ; stream >> d ; t->value = d ; output.push_back(t) ; count = 0 ; state = E ; } else { token* t = new token ; e = i ; string s1 (b,e) ; istringstream stream (s1) ; float d ; stream >> d ; t->value = d ; output.push_back(t) ; count = 0 ; state = E ; } break ; case O : if ( i != end && ((*i == '+') || (*i == '-') || (*i == '*') || (*i == '/') || (*i == '^')) ) { token* t = new token ; t->operation = *i ; //cout << *i ; output.push_back(t) ; ++i ; state = E ; continue ; } break ; case floating_point : if ( i != end && isdigit(*i) && count < 2 ) { state = V ; ++i ; continue ; } else state = ERROR ; break ; case EXP : if ( i != end && isdigit(*i) || (*i == '+') || (*i == '-') ) { state = V ; ++i ; continue ; } else state = ERROR ; break ; case ERROR : default : exit(0) ; } } if ( i == end && (state != E || state != ERROR) ) { token* t = new token ; e = i ; string s1 (b,e) ; //cout << s1 ; istringstream stream (s1) ; float d ; stream >> d ; //cout << d ; t->value = d ; output.push_back(t) ; count = 0 ; state = E ; } else exit(0) ; } std::ostream & operator << (std::ostream & out,token const & t) { if (t.operation) out << t.operation << endl ; else if (!(t.ID.empty())) out << t.ID << endl ; else out << t.value << endl ; } int main() { int count = 0 ; list<token *>::iterator i ; string str ; list<token *> output ; getline(cin,str) ; lexer(str.begin(),str.end(),output) ; i = output.begin() ; while ( i != output.end() ) { cout << **i ; ++i ; ++count ; } //cout << count ; cin.get() ;}