简体   繁体   English

C ++计算器不适用于*运算符

[英]C++ calculator doesn't work for * operator

I am learning c++ using Stroustrup's Programming Principles and Practice and there is this example code that one is to debug and make it work. 我正在使用Stroustrup的《编程原理和实践》学习c ++,并且有一个示例代码可以调试并使其正常工作。 I have made the corrections and when you enter an arithmetic expression that uses the + or - operator, it works. 我已经进行了更正,当您输入使用+或-运算符的算术表达式时,它可以工作。 However when I enter an expression with the * operator, nothing happens. 但是,当我使用*运算符输入表达式时,什么也没有发生。 I have used debuggers to help me walk through the code but I've not been able to figure it out. 我已经使用调试器来帮助我遍历代码,但是我仍然无法弄清楚。 The book uses a non standard include file std_lib_facilities.h 本书使用了非标准的包含文件std_lib_facilities.h

The actual calculator program is here . 实际的计算器程序在这里 Can someone be kind enough to help me figure out why an expression like 2*3; 有人可以帮助我找出为什么2 * 3这样的表情吗? produce no result while 2+3; 2 + 3时不产生结果; works? 作品?

PS: as per the program a valid expression must have ; PS:根据程序,有效表达式必须具有; at the end to trigger a print. 最后触发打印。 So 2+3; 所以2 + 3; is correct and will trigger and print but 2+3 without a ; 是正确的,将触发并打印,但2 + 3则不带; will just cause the cursor to just keep blinking. 只会导致光标一直闪烁。 Please also not that, its the first attempt at producing a calculator program so it is missing a lot of features. 也请不要这样,它是产生计算器程序的首次尝试,因此它缺少许多功能。 My concern at the moment is to work out why a simple arithmetic expression involving the * operator does not work. 目前,我关心的是弄清楚为什么包含*运算符的简单算术表达式不起作用。 Thanks. 谢谢。

[EDIT] [编辑]

Thanks @KonradRudolph for your answer. 感谢@KonradRudolph的回答。 One thing that stumped me though was, when I used the gdb debugger (I'm on linux), the debugger will not step into term when I enter an expression with a *. 让我感到困扰的一件事是,当我使用gdb调试器(我在linux上)时,当我输入带有*的表达式时,调试器将不会进入条件。 Now I know there was an error but, I was expecting it to at least step into the function and hang somewhere in there. 现在,我知道这里有一个错误,但是我希望它至少能进入该函数并挂在其中。 Why won't the debugger step into a function that has an error? 调试器为什么不进入有错误的函数? That will be more helpful. 那会更有帮助。

Well you simply didn't make all necessary corrections. 好吧,您根本没有进行所有必要的更正。

In term : term

    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;
    }

case '*' is missing a break statement. case '*'缺少break语句。

There may be more errors. 可能会有更多错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM