
//
// This is example code from Chapter 6.3.4 "Using tokens" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#include "std_lib_facilities.h"

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

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) /**initiator list */ { }
    Token(char ch, double val)     // make a Token from a char and a double
        :kind(ch), value(val) { }
    void printToken() {
        cout << '(' << kind << ',' << value << ')';
    }
};

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

Token get_token() {
    Token t{'+'};
    double n;
    char c;
    cin >> c;
    if(c>='0' && c<='9') {
        cin.putback(c);
        cin >> n;
        t.kind ='8';
        t.value = n;
    }
    if(c=='*') t.kind='*';
    return t;
}    // read a token from cin

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

vector<Token> tok;    // we'll put the tokens here

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

int main()
{
    while (cin) {
        Token t = get_token();
        tok.push_back(t);
    }
    /// ...
    ///Now we could find the multiply operation by a simple loop:
    cout << "\nHi mom!";
    cout << "\ntok.size() = " << tok.size();
    for(int i = 0; i < tok.size(); ++i) {
        tok[i].printToken();
        cout << " ";
    }
    for (int i = 0; i<tok.size(); ++i) {
        if (tok[i].kind=='*') {    // we found a multiply!
            double d = tok[i-1].value*tok[i+1].value;
            cout << "\nd = " << d;/// now what?
        }
    }

}

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