且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么这个程序不适用于负数的电源?

更新时间:2023-02-10 19:33:18

使用调试器。

调试器允许您逐行执行,检查变量,您将看到有一个点它停止做你期望的事。

调试器 - ***,免费的百科全书 [ ^ ]

在Visual Studio 2010中进行调试 - 初学者指南 [ ^ ]



使用调试器,确保代码真正符合你的想法。



尽量减少代码足以显​​示问题。
Use the debugger.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

With the debugger, make sure what the code really do match what you think it should.

Try to reduce the code to just enough to exhibit the problem.


我没有最终的解决方案,因为仅通过检查代码来执行程序就太难了。但我有一些提示:



不要使用全局变量。在函数中使用局部变量。这使得更容易阅读和调试代码。



似乎没有代码可以处理减法操作。当解析器遇到' - '字符时会发生什么?它将它视为数字并减去48,这将是-3!



您传递值[value_size] 到你的评估函数,在那里用作 num1 。但是在调用函数时,该数组成员未初始化。之后将使用返回值进行初始化。为数组项分配值后,您将递增索引,使其指向下一个尚未初始化的项。
I have no final solution because it would be too hard to follow the program execution by just examining the code. But I have a few tips:

Don't use global variables. Use local variables in your functions. This makes it much easier to read and debug the code.

It seems that there is no code that handles the subtraction operation. What happens when your parser encounters the '-' character? It treats it as digit and subtracts 48 which will be -3!

You are passing values[value_size] to your evaluate function which is used there as num1. But that array member is not initialised when calling the function. It will be initialised with the return value afterwards. Once you have assigned a value to an array item, you are incrementing the index so that it points to the next not yet initialised item.