数据结构(9.13)

1.求算术表达式的值

others' mentality

first, find out the suffix expression.

put the string in an array and named as exp, and define another array named postexp, which is to put the suffix expression. The two arraies are typed of char.  In order to make the trance come true, a operation stack is necessary. A stack is named as op to stor the operations in the original expression. To follow the rule of caculation, we need to define the primary of the opertaions as the following table. ps.the lpri means the char from the exp in the process of traversal. the rpri is the char in the op.

if the leftpri's primary is higher than the right one, which means that the operation should be poped from the op and putted into the postexp; else if the leftpri's primaty is lower than the right one, which means that the operation was put into op and the traversal goes on. else, get out all the left operations in the op in order and put them in the postexp. The postexp is the suffix expersion of the orgiainal expression.

Second, do operation with the postexp.

A number stack is necessary, which is to put the result of each stp of operation.

Traversal op and when meet numbers get out and get it stored in the stack and when meet an operation, get out two numbers from the number stack and do the operation with the numbers that you get out from the stack and put the result back to the stack untile the postexp is traversaled over.

Code written with the help of book


5.Summary

stray \241  or  \243 in program means that there are Chinese input

When I get a problem, I should be clam down and try to find the solution with the knowledge that I have learnt, just to try it.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,811评论 0 23
  • 她在傍晚的冷風裡等105路公交車。路上行人稀少,她裹緊了衣服在風中打了個冷戰。已是三月末的天氣,不該這樣冷。她無聊...
    连生阅读 266评论 0 0
  • 离家的公交车上,不断摇曳,抖抖停停,摇出了一些记忆片段,那些消逝的,即将消失的斑驳的旧时光。望着车窗外变迁的田野,...
    辰北阅读 304评论 0 0
  • 字符串转data型 data转string型 变化的我都懵逼了,有兴趣的可以记一下
    宁静1致远阅读 24,369评论 0 6