作业帮 > 综合 > 作业

C语言编一个计算的CODE

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/28 16:05:27
C语言编一个计算的CODE
比如 (25 - 3) / 11
After processing 25 the stack would have 25 on it.
After processing 3 the stack would have 25 and 3 on it (with 3 on top).
After processing - the stack would have 22 on it:The subtract operation pops the top two items from the stack,and then pushes the result of their subtraction onto the stack.Note the order of the operands for subtraction.
After processing 11 the stack would have 22 and 11 on it (with 11 on top).
After processing / the stack would have 2 on it.
Now that the input has all been processed,you would print out 2 followed by a newline
计算器包含+-*/
You will read input from stdin ; you will have a main function .Each line of input will be a separate expression which you should evaluate in the manner described above.You may use the function int atoi (const char * str); which is provided in stdlib.h in order to convert a string into an integer .
如果出现错误printf("error\n"); and then process the next line错误有以下三种
There are not enough items on the stack to process the operation (eg.+ with only one item).
There are too many items on the stack at the end of the expression (eg.1 2 3 +).
Division by 0.
那个例子 (25 - 3) / 11应该转换为25 3 - 11 /的形式
更多例子
input
16 2 2 + /
3 3 3 3 * * *
5 +
3 2 3 + + 2 3 + *
output
4
81
error
40
逆波兰式的表达式计算么,和中缀表达式方式类似的,因为不需要处理括号和优先级,直接
一个堆栈就搞定了,
分析表达式 碰到数字 进栈 碰到符号 出栈2个数字 根据符号进行运算
运算结果再进栈 直到表达式分析结束, 最后栈顶就是结果,
碰到除0 直接放弃运算
如果分析结束 栈中数据不是1个那么就出错
再问: 能帮写下么 太小白了
再答: 自己写,都是基本的东西