خرید بک لینک

Vote count: 0

#include "std_lib_facilities"

class Token {
public:
char kind;        // what kind of token
double value;     // for numbers: a value
Token(char ch)    // make a Token from a char
    :kind(ch), value(0) { }
Token(char ch, double val)     // make a Token from a char and a double
    :kind(ch), value(val) { }
};



class Token_stream {
public:
 Token_stream();
 Token get();
 void putback(Token t);
 private:
 bool full;
 Token buffer;


 } ;


 Token_stream::Token_stream()
 :full(false),buffer(0)
 {
 }


  void Token_stream::putback(Token t)
{

if(full) error ("putback() into a full buffer");
buffer=t;
full=true;


} 


Token Token_stream::get()
 {
 if(full){

 full=false;
 return buffer;

 } 


 char ch;
  cin >> ch;    

 switch (ch) {

case 'q':
case ';':

case '(': case ')': case '+': case '-': case '*': case '/':
    return Token(ch);        // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
    {
        cin.putback(ch);         // put digit back into the input stream
        double val;
        cin >> val;              // read a floating-point number
        return Token('8',val);   // let '8' represent "a number"
    }
default:
    error("Bad token");
}
}


Token_stream ts;
double expression();

 double primary()     // read and evaluate a Primary 
 {
  Token t = ts.get();
 switch (t.kind) {
 case '(':    // handle '(' expression ')'
    {
        double d = expression();
          t = ts.get();
         if (t.kind != ')') error("')' expected");
         return d;
     }
 case '8':            // we use '8' to represent a number
    return t.value;  // return the number's value
 default:
    error("primary expected");
 }
 } 



double term()
{
double left = primary();
Token t = ts.get();     // get the next token

while(true) {
    switch (t.kind) {
    case '*':
        left *= primary();
        t = ts.get();
        break;
    case '/':
        {
            double d = primary();
            if (d == 0) error("divide by zero");
            left /= d;
            t = ts.get();
            break;
        }
    default:
        ts.putback(t);
        return left;
    }
}
}


 double expression()
{
double left = term();      // read and evaluate a Term
Token t = ts.get();     // get the next token
while(true) {
    switch(t.kind) {
    case '+':
        left += term();    // evaluate Term and add
        t = ts.get();
        break;
    case '-':
        left -= term();    // evaluate Term and subtract
        t = ts.get();
        break;
    default:
        ts.putback(t);
        return left;       // finally: no more + or -: return the answer
    }
  }
}

//------------------------------------------------------------------------------



int main() {
double val=0;
while (cin){
    Token t=ts.get();

    if (t.kind == 'q') break; // 'q' for "quit"


    if(t.kind==';')
       cout<<"="<<val<<endl;
    else
       ts.putback(t);
       val=expression();

}

}

//-------------------------------------------------------------------

Please anyone tell me how this program works step by step ex - when I put "5" where it checked first like which path it followed and then for next input "+ " and then from next 10 .

I struck with this , when I was studying book written by Bjarne Stroustrup

" Principles and Practice using C++"" in chapter 6 .

asked 28 secs ago

برچسب: programming principles and practice using c, chapter 6, نویسنده: استخدام کار تاريخ: چهارشنبه 31 شهريور 1395 ساعت: 2:09

صفحه بندی