📄 eval.how
字号:
Basically, EVAL.C converts infix notation to postfix notation. If you'renot familiar with these terms, infix notation is standard human-readableequations such as you write in a C program. Postfix notation is most familiaras the "reverse Polish" notation used in Hewlett Packard calculators and inthe Forth language. Internally, all languages work by converting to postfixevaluation. Only Forth makes it obvious to the programmer. EVAL.C performs this conversion by maintaining 2 stacks, an operand stackand an operator stack. As it scans the input, each time it comes across anumerical value, it pushes it onto the operand stack. Whenever it comesacross an operator, it pushes it onto the operator stack. Once the inputexpression has been scanned, it evaluates it by popping operands off theoperand stack and applying the operators to them. For example the simple expression "2+3-7" would push the values 2, 3, and 7onto the operand stack and the operators "+" and "-" onto the operator stack.When evaluating the expression, it would first pop 3 and 7 off the operandstack and then pop the "-" operator off the operator stack and apply it. Thiswould leave a value of -4 on the stack. Next the value of 2 would be poppedfrom the operand stack and the remaining operator off of the operator stack.Applying the "+" operator to the values 2 and -4 would leave the result ofthe evaluation, -2, on the stack to be returned. The only complication of this in EVAL.C is that instead of raw operators(which would all have to be 1 character long), I use operator tokens whichallow multicharacter operators and precedence specification. What I push onthe operator stack is still a single character token, but its the operatortoken which is defined in the 'verbs' array of valid tokens. Multicharactertokens are always assumed to include any leading parentheses. For example, inthe expression "SQRT(37)", the token is "SQRT(". Using parentheses forces evaluation to be performed out of the normalsequence. I use the same sort of mechanism to implement precedence rules.Unary negation is yet another feature which takes some explicit exceptionprocessing to process. Other than these exceptions, it's prettystraightforward stuff.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -