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"
class Token {
public:
char kind; // what kind of token
double val; // for numbers: a value
Token(char ch) :kind(ch), val() { }
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;
}
1>.\error02.cpp(15) : error C2864: 'Token::val' : only static const integral
data members can be initialized within a class
1>.\error02.cpp(17) : error C2614: 'Token' : illegal member initialization:
'value' is not a base or member
1>.\error02.cpp(33) : error C2511: 'void Token_stream::unget(void)' :
overloaded member function not found in 'Token_stream'
1> .\error02.cpp(21) : see declaration of 'Token_stream'
1>.\error02.cpp(51) : error C2679: binary '>' : no operator found which takes
a right-hand operand of type 'char' (or there is no acceptable conversion)
1> could be 'built-in C++ operator>(void *, void *)'
1> while trying to match the argument list '(std::istream, char)'
1>.\error02.cpp(75) : error C2601: 'primary' : local function definitions are
illegal
1> .\error02.cpp(44): this line contains a '{' which has not yet been matched
1>.\error02.cpp(92) : error C2601: 'Term' : local function definitions are
illegal
1> .\error02.cpp(44): this line contains a '{' which has not yet been matched
1>.\error02.cpp(110) : error C2601: 'expression' : local function definitions
are illegal
1> .\error02.cpp(44): this line contains a '{' which has not yet been matched
1>.\error02.cpp(117) : error C2001: newline in constant
1>.\error02.cpp(127) : error C2601: 'main' : local function definitions are
illegal
1>.\error02.cpp(170) : fatal error C1075: end of file found before the left
brace '{' at '.\error02.cpp(44)' was matched
---------------------------------
The fish are biting.
Get more visitors on your site using Yahoo! Search Marketing.