无法找到 K&R 练习 4-6 的解决方案

发布于 2024-09-08 14:35:25 字数 356 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-09-15 14:35:25

创建第 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.

辞旧 2024-09-15 14:35:25

我想这是构建命令行计算器的第一步。

解析输入字符串的操作数和运算符。将运算符映射到枚举

enum operator { TYPE_ADD, TYPE_SUBTRACT,TYPE_MAX);

调用函数来计算结果

  int    calculate(int i_op1, int i_op2, operator e_operator)
           {
              /*Use switch case to calculate result*/
           }

将此结果保存到变量中。输入中的字符串等于“_”,然后将其用作函数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

enum operator { TYPE_ADD, TYPE_SUBTRACT,TYPE_MAX);

Call function to compute result

  int    calculate(int i_op1, int i_op2, operator e_operator)
           {
              /*Use switch case to calculate 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文