我在 Bjarne Stroustrup 的书《使用 c++ 的实践和原则》的第 6 章上停滞不前。基本上,我已经阅读了这一章两遍,并试图理解与制作计算器相关的语法和记号示例。我会说我理解这些示例,但是当它们结合在一起时,我无法调试代码并让 Bjarne 给我们的错误程序正常工作。
我们必须解决 5 个导致程序无法编译的错误和 3 个逻辑错误。我解决了 3 个编译错误,但留下了 2 个,因为我不确定我修复它们的方式是否真的有助于破坏程序。我会用
//语法错误“原因”
在代码上。之后,将出现 3 个逻辑错误,如果您能帮我解决这些问题,我将不胜感激。对于逻辑问题,您不必给我实际的答案,但至少会有一些线索会受到赞赏,甚至可能比答案更重要。但如果你给我答案,那也很好。
我一直在网上寻找是否有其他人发布了有关演习的问题,但到目前为止我还没有遇到任何问题。感谢所有帮助。谢谢。
这一章一直让我头疼,我真的很想过去!
补充资料:
我所做的是将 full 声明为 bool 和 true 并制作缓冲区和字符。它位于 get() 函数中。
在主函数中,我将 val 声明为双精度。
之后,程序会告诉我 get() 函数和 primary() 函数不会在所有路径上返回,所以我让 get() 默认返回一个令牌,而 primary() 默认返回 ts.value。
在此之后,编译器不会显示任何错误但不会运行。
下面的代码没有这些更改,因为我相信我的更改有助于破坏程序。
// The code
#include "../../../std_lib_facilities.h"
//------------------------------------------------------------------------------
class Token {
public:
char kind; // what kind of token
double value; // for numbers: a value
Token(char ch) // make a Token from a char
:kind(ch), value(0) { }
Token(char ch, double val) // make a Token from a char and a double
:kind(ch), value(val) { }
};
//------------------------------------------------------------------------------
class Token_stream {
public:
Token_stream(); // make a Token_stream that reads from cin
Token get(); // get a Token (get() is defined elsewhere)
void putback(Token t); // put a Token back
private:
bool full; // is there a Token in the buffer?
Token buffer; // here is where we keep a Token put back using putback()
};
//------------------------------------------------------------------------------
// The constructor just sets full to indicate that the buffer is empty:
Token_stream::Token_stream()
:full(false), buffer(0) // no Token in buffer
{
}
//------------------------------------------------------------------------------
// The putback() member function puts its argument back into the Token_stream's buffer:
void Token_stream::putback(Token t)
{
if (full) error("putback() into a full buffer");
buffer = t; // copy t to buffer
full = true; // buffer is now full
}
//------------------------------------------------------------------------------
Token get()
{
if (full) { // do we already have a Token ready? //Syntax error "full" and "buffer" not declared
// remove token from buffer
full=false;
return buffer;
}
char ch;
cin >> ch; // note that >> skips whitespace (space, newline, tab, etc.)
switch (ch) {
case ';': // for "print"
case 'q': // for "quit"
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 '9':
{
cin.putback(ch); // 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");
}
}
//------------------------------------------------------------------------------
Token_stream ts; // provides get() and putback()
//------------------------------------------------------------------------------
double expression(); // declaration so that primary() can call expression()
//------------------------------------------------------------------------------
// deal with numbers and parentheses
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");
}
}
//------------------------------------------------------------------------------
// deal with *, /, and %
double term()
{
double left = primary();
Token t = ts.get(); // get the next token from token stream
while(true) {
switch (t.kind) {
case '*':
left *= primary();
t = ts.get();
case '/':
{
double d = primary();
if (d == 0) error("divide by zero");
left /= d;
t = ts.get();
break;
}
default:
ts.putback(t); // put t back into the token stream
return left;
}
}
}
//------------------------------------------------------------------------------
// deal with + and -
double expression()
{
double left = term(); // read and evaluate a Term
Token t = ts.get(); // get the next token from token stream
while(true) {
switch(t.kind) {
case '+':
left += term(); // evaluate Term and add
t = ts.get();
break;
case '-':
left += term(); // evaluate Term and subtract
t = ts.get();
break;
default:
ts.putback(t); // put t back into the token stream
return left; // finally: no more + or -: return the answer
}
}
}
//------------------------------------------------------------------------------
int main()
try
{
while (cin) {
Token t = ts.get();
if (t.kind == 'q') break; // 'q' for quit
if (t.kind == ';') // ';' for "print now"
cout << "=" << val << '\n'; //Syntax error "val" not declared
else
ts.putback(t);
val = expression(); //Syntax error "val" not declared
}
keep_window_open();
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
keep_window_open();
return 2;
}
//------------------------------------------------------------------------------