无法找到 K&R 练习 4-6 的解决方案
在 K&R 中,我们成功创建了一个 RPN。
现在的练习是:
添加用于处理变量的命令,(很容易提供二十六个 具有单字母名称的变量。)为最多添加一个变量 最近打印的值。
所以这有点像 Python 解释器,我们可以这样做:
>>>5
>>>_ (where _ prints 5)
>>>_ + 5 (which prints 10)
或者 A = 5 _ + A(打印 10)
等等,但我不太确定我想如何在 C 中实现它。我只是感到困惑。
In K&R we have managed to create an RPN.
The exercise now is to:
Add commands for handling variables, (It's easy to provide twenty-six
variables with single letter names.) Add a variable for the most
recently printed value.
So this is meant to act somewhat like the Python interpreter, where we can do:
>>>5
>>>_ (where _ prints 5)
>>>_ + 5 (which prints 10)
or
A = 5
_ + A (which prints 10)
and so on, but I'm not so sure about how I want to go about it in C. I just feel stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建第 26 个变量。每当您打印某些内容时,请将该值写入第 26 个变量。当他们使用
_
(或您选择的任何名称)时,从该变量中读取。Create a 26th variable. Any time you print something, write that value into the 26th variable. When they use
_
(or whatever name you choose) read from that variable.我想这是构建命令行计算器的第一步。
解析输入字符串的操作数和运算符。将运算符映射到枚举
调用函数来计算结果
将此结果保存到变量中。输入中的字符串等于“_”,然后将其用作函数
calculate
的第一个输入。This is first step of building a command line calculator I guess.
Parse the input string for operands and operator. Map the operator to a enum
Call function to compute result
Save this result into a variable. In input in the string is equal to "_" then use this as first input to the function
calculate
.