At 17:17 2007-02-19, Robert Ryan wrote:
I got it solved after a lot of confusion.

#include "std_lib_facilities.h"

you realize that this is a non-standard include file..... the rest of us don't have it


class Token {
public:
 char kind;   // what kind of token
 double value;   // 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(Token t) // unget takes one argument
 {
  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 'x': // quit/exit
 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.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
 } // add (
}

Token_stream ts;  // provides get() and unget()
double expression();  // declaration so that primary() can call expression()
double 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();
 switch (t.kind) {
 case '*':
   return left *term();
 case '/':
  { double d = term();
   if (d == 0) error("divide by zero");
   return left / d;
  }
 case '%':
  { int i1 = int(left);
   int i2 = int(term());
   return i1%i2;
  }
 default:
  ts.unget(t);
  return left;
 }
}
double expression()
{
 double left = term(); // 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:
   ts.unget(t);
  return left;  // return the value of the Term
 }
}

int main()
try
{
 //while (cin) cout << expression() << '\n'; // version 0
 cout <<"Welcome to our simple calculator\n";
 cout << "Please enter-expression using floating-point expression\n";
 cout << "type = to print and x to exit\n";

 double val=  0; // version 1
 while (cin) {
  Token t = ts.get();
  if (t.kind == 'x') break;
  if (t.kind == '=')
   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;
}


"Victor A. Wagner Jr." <[EMAIL PROTECTED]> wrote:
At 15:06 2007-02-19, Robert Ryan wrote:
does anyone have an idea

/* "be-bugged" calculator: many syntax and type errors, just three logic
errors (we hope).
Our aim waw to be "medium devious"; that is, to provide errors that
are not impossible to find, easy to fix once found, but still tricky
enough to make you scratch your head once or twice.
Be warned that what we can deliberately provide is far less outrageous
than what any beginner can accidentally type in on a bad day.
Happy bug hunting! */
#include "std_lib_facilities.h"

c:\projects\programming\personal\cprog_testing\robertryan2\main.cpp(9) : fatal error C1083: Cannot open include file: 'std_lib_facilities.h': No such file or directory

why should I bother with any of the rest of it??

[deleted to save bandwidth]
Victor A. Wagner Jr.      http://rudbek.com
The five most dangerous words in the English language:
              "There oughta be a law"



<http://answers.yahoo.com/dir/index;_ylc=X3oDMTFvbGNhMGE3BF9TAzM5NjU0NTEwOARfcwMzOTY1NDUxMDMEc2VjA21haWxfdGFnbGluZQRzbGsDbWFpbF90YWcx?link=ask&sid=396545367>Food fight? Enjoy some healthy debate in the <http://answers.yahoo.com/dir/index;_ylc=X3oDMTFvbGNhMGE3BF9TAzM5NjU0NTEwOARfcwMzOTY1NDUxMDMEc2VjA21haWxfdGFnbGluZQRzbGsDbWFpbF90YWcx?link=ask&sid=396545367>Yahoo! Answers Food & Drink Q&A.

Victor A. Wagner Jr.      http://rudbek.com
The five most dangerous words in the English language:
              "There oughta be a law" 

Reply via email to