#include #include #include // ================================================================================ // Class TStringExpr is designed to contain a string with expression to evaluate. // An element of this class allows to // 1. read an expression from an input stream (e.g. cin>>expr; ) // 2. print the expression to an output stream (e.g. cout<>ch; ) // 4. return the read character to the expression (e.g. expr.unget(); ) // 5. read an integer from the expression {e.g. expr>>num; } // -------------------------------------------------------------------------------- class TStringExpr { private: string expr; unsigned int cur; public: // constructors TStringExpr() { expr=""; cur=0; }; TStringExpr(char *str) { expr=str; cur=0; }; TStringExpr(const string &str) { expr=str; cur=0; }; // assignmnet operators string operator=(char *str); string operator=(const string &str); // some useful methods void putback(char ch) { if(cur>0) expr[--cur]=ch; }; char get() { return cur0) cur--; }; void print_wrong_char(char *fname); // friend operators friend istream& operator>>(istream &inp, TStringExpr &e); friend ostream& operator<<(ostream &out, const TStringExpr &e); friend char operator>>(TStringExpr &e, char &ch); friend TStringExpr& operator>>(TStringExpr &e, int &value); }; string TStringExpr::operator=(char *str) { expr = str; cur = 0; return expr; } string TStringExpr::operator=(const string &str) { expr = str; cur = 0; return expr; } void TStringExpr::print_wrong_char(char *fname) { cerr<>(istream &inp, TStringExpr &e) { char ch; e.expr = ""; ch = inp.get(); while( !inp.eof() && ch!='\n' ){ if( ! isspace(ch) ) e.expr += ch; ch = inp.get(); } e.cur = 0; return inp; } ostream& operator<<(ostream &out, const TStringExpr &e) { out<>(TStringExpr &e, char &ch) { if( e.cur < e.expr.length() ) return ch=e.expr[e.cur++]; else return 0; } TStringExpr& operator>>(TStringExpr &e, int &value) { char ch; value = 0; while( ch=e.get() ) if( isdigit(ch) ) value = 10*value + (ch-'0'); else{ e.unget(); break; } return e; } // ================================================================================