I am trying to get this code to run. if I add "../../std_lib_facilities.h" to
the source code it only needs 2 folders, is that right
thanks
bob
#include "../../std_lib_facilities.h"
class Token {
public:
char kind; // what kind of token
double val; // for numbers: a value
Token(char ch) :kind(ch), value(0) { }
Token(char ch, double val) :kind(ch), value(val) { } };
class Token_stream {
bool full; // is there a Token in the buffer
Token buffer; // here is where we keep a Token put back using unget()
public:
Token get(); // get a Token (get() is defined elsewhere)
void unget(Token t); // put a Token back
Token_stream(); // make a Token_stream that reads from cin };
void Token_stream::unget() {
buffer = t; // copy t to buffer
full = true; // buffer is now full }
Token_stream::Token_stream()
:full(false), buffer(0) // no Token in buffer { }
Token Token_stream::get()
{
if (full) { // check if we already have a Token ready
full=false;
return buffer; }
char ch;
cin > ch; // note that >> skips whitespace (space, newline, tab, etc.)
switch (ch) {
case ';': // print
case 'q': // quit/exit
case '(': case ')': case '+': case '-': case '*': case '/':
return Token(ch); // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '6': case '7': case '8': case
'9': {
cin.unget(); // 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");
// XXX }
Token_stream ts; // provides get() and unget()
double expression(); // declaration so that primary() can call expression()
double primary()
{
Token t = ts.get();
switch (kind) {
case '(': // handle ( expression )
{ double d = expression();
t = ts.get();
if (t.kind != ')') then error("'(' expected");
return d;
}
case '8': // we use '8' to represent a number
return t.value; // return the numbers value
default:
error("primary expected"); } }
double Term()
{
double left = primary();
Token t = ts.get();
switch (t.kind) {
case '*':
return left * term();
case '/':
{ double d = term();
if (d == 0) error("divide by zero");
return d / left; }
default:
ts.unget(t);
return left;
}
}
double expression()
{
double left = erm(); // read and evaluate a Term
Token t = ts.get(); // get the next token
switch (t.kind) { // see which kind of token that is
case '+':
return left + expression(); // read and evaluate an Expression,
// then do an add
case '-:
return left - expression(); // read and evaluate an Expression,
// then do a subtraction
default:
return left; // return the value of the Term } }
int main()
try
{
//while (cin) cout << expression() << '\n'; // version 0
double val 0; // version 1
while (cin) {
Token t = ts.get();
if (t.kind == 'q') break;
if (t.kin == ';')
cout << val << '\n';
else
ts.unget(t);
val = expression();
}
/*
double val = 0; // version 2
while (cin) {
cout << "> ";
Token t = ts.get();
while (t.kind == ';') t=ts.get(); // eat ';'
if (t.kind == 'q') {
keep_window_open();
return 0;
}
ts.unget(t);
cout << "= " << expression() << endl;
}
*/
keep_window_open();
return 0;
}
catch (runtime_error& e) {
cerr << e.what() << endl;
keep_window_open("~~");
return 1;
}
catch (...) {
cerr << "exception \n";
keep_window_open("~~");
return 2;
---------------------------------
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel
bargains.