diff --git a/projects/05/calculator02buggy.cpp b/projects/05/calculator.cpp similarity index 86% rename from projects/05/calculator02buggy.cpp rename to projects/05/calculator.cpp index 48497e5..06c259e 100644 --- a/projects/05/calculator02buggy.cpp +++ b/projects/05/calculator.cpp @@ -3,24 +3,9 @@ // "Software - Principles and Practice using C++" by Bjarne Stroustrup // -/* - This file is known as calculator02buggy.cpp - - I have inserted 5 errors that should cause this not to compile - I have inserted 3 logic errors that should cause the program to give wrong - results - - First try to find and remove the bugs without looking in the book. - If that gets tedious, compare the code to that in the book (or posted - source code) - - Happy hunting! - -*/ - #include -lass Token +class Token { public: char kind; /// what kind of token @@ -57,7 +42,7 @@ void Token_stream::putback(Token t) full = true; } -Token get () +Token Token_stream::get() { if (full) // do we already have a Token ready? { @@ -89,6 +74,7 @@ Token get () case '5': case '6': case '7': + case '8': case '9': { cin.putback(ch); // put digit back into the input stream @@ -117,7 +103,7 @@ double primary () double d = expression(); t = ts.get(); if (t.kind != ')') - error("')' expected); + error("')' expected"); return d; } @@ -142,6 +128,7 @@ double term () case '*': left *= primary(); t = ts.get(); + break; case '/': { @@ -163,7 +150,7 @@ double term () /// deal with + and - double expression () { - double left = term(; // read and evaluate a Term + double left = term(); // read and evaluate a Term Token t = ts.get(); // get the next token from token stream while (true) @@ -176,7 +163,7 @@ double expression () break; case '-': - left += term(); // evaluate Term and subtract + left -= term(); // evaluate Term and subtract t = ts.get(); break; @@ -190,6 +177,8 @@ double expression () int main () try { + double val{}; + while (cin) { Token t = ts.get();