From e5a5be8c73444fcf5c77fda92517c632d2759df4 Mon Sep 17 00:00:00 2001 From: Vladimir Podaruev Date: Mon, 18 Sep 2023 22:44:24 +0300 Subject: [PATCH 1/2] Fix compiler errors --- .../{calculator02buggy.cpp => calculator.cpp} | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) rename projects/05/{calculator02buggy.cpp => calculator.cpp} (87%) diff --git a/projects/05/calculator02buggy.cpp b/projects/05/calculator.cpp similarity index 87% rename from projects/05/calculator02buggy.cpp rename to projects/05/calculator.cpp index 48497e5..cca2853 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? { @@ -117,7 +102,7 @@ double primary () double d = expression(); t = ts.get(); if (t.kind != ')') - error("')' expected); + error("')' expected"); return d; } @@ -163,7 +148,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) @@ -190,6 +175,8 @@ double expression () int main () try { + double val{}; + while (cin) { Token t = ts.get(); From fd9eb895b6c7c70e9fa7bb674ec95185df71e193 Mon Sep 17 00:00:00 2001 From: Vladimir Podaruev Date: Mon, 18 Sep 2023 22:53:36 +0300 Subject: [PATCH 2/2] Fix logic errors --- projects/05/calculator.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/projects/05/calculator.cpp b/projects/05/calculator.cpp index cca2853..06c259e 100644 --- a/projects/05/calculator.cpp +++ b/projects/05/calculator.cpp @@ -74,6 +74,7 @@ Token Token_stream::get() case '5': case '6': case '7': + case '8': case '9': { cin.putback(ch); // put digit back into the input stream @@ -127,6 +128,7 @@ double term () case '*': left *= primary(); t = ts.get(); + break; case '/': { @@ -161,7 +163,7 @@ double expression () break; case '-': - left += term(); // evaluate Term and subtract + left -= term(); // evaluate Term and subtract t = ts.get(); break;