如何在Squeak4.1中解析文件中的字符? [英] How can I parse character in a File in Squeak4.1?

查看:109
本文介绍了如何在Squeak4.1中解析文件中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,假设我有一个文件test.txt,文件的内容为"1 + 2 * 3",如果直接在Squeak的Workspace中表达该格式,则将其打印为9,我想得到的是7 然后我从文件中读取文件内容1+2*3.这样的代码,效果很好

friend, suppose I have a file test.txt, the content of file is "1+2*3", if the fomular directly expressed in Squeak's Workspace, print it will result 9, What I want to get is 7 then I read the file content 1+2*3 from a file. code like this and it works well

ReadFrom
"read the equation from  ./formular.txt"

| fileContents |
fileContents := FileStream 
               readOnlyFileNamed: 'test.txt' 
               do: [:f | f contents ].
^fileContents.

但是如何将字符串"1 + 2 * 3"的5个字符存储到集合中,又可以使用二叉树计算方程式呢?有人可以给我一些提示吗?首先谢谢:)

but how can I store the 5 caracters of string "1+2*3" into a collection , further I can use binary tree to calculate the equation? Do somebody can give me some hints? thanks first :)

推荐答案

SmaCC教程最终构建出您想要的东西.

The SmaCC tutorial ends up building pretty much what you want.

上述教程中的语录:

我们在语法顶部添加的两行表示"+"和-"是从左到右求值的,并且具有相同的优先级,低于"*"和"/"./p>

The two lines that we added to the top of the grammar mean that "+" and "-" are evaluated left-to-right and have the same precedence, which is lower than "*" and "/".

SmaCC是用于Smalltalk的成熟的解析器生成器,根据您的需要,此生成器可能会过大.

SmaCC is a full-blown parser generator for Smalltalk which may be overkill depending on your needs.

如果您只想构建一个简单的计算器,则可以使用调车码算法将中缀数学表达式转换为RPN并轻松进行评估.

If you just want to build a simple calculator you can use the Shunting-yard algorithm to convert an infix mathematical expression into RPN and evaluate it easily.

这篇关于如何在Squeak4.1中解析文件中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆