开发者

Cannot find a solution to K&R exercise 4-6

开发者 https://www.devze.com 2023-01-06 01:46 出处:网络
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

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 p开发者_运维百科rints 10)

and so on, but I'm not so sure about how I want to go about it in C. I just feel stumped.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号