简体   繁体   English

在Java中评估数学表达式

[英]Evaluating math expressions in java

I'm trying to learn java in my spare time. 我想在业余时间学习Java。 Thus far I love it. 到目前为止,我喜欢它。 I am currently working on a more advanced version of a simple 4-function calculator. 我目前正在开发一个简单的4功能计算器的更高级版本。 In essence I would like to enter an expression, say "4 + 5" and then split it apart into a first term, a second term, and an operator, then calculate the answer based on the operator symbol detected and output the answer. 本质上,我想输入一个表达式,说“ 4 + 5”,然后将其拆分为第一项,第二项和一个运算符,然后根据检测到的运算符来计算答案并输出答案。 My issue has been that I'm storing the expression in a char array and that works fine, unless the first term is a double-digit number, or the user adds in another operator. 我的问题是,我将表达式存储在char数组中,并且工作正常,除非第一个术语是两位数,或者用户添加了另一个运算符。 How can I keep this dynamic enough to allow for many forms of simple expressions? 如何保持足够的动态性以允许多种形式的简单表达式?

I'd try working with the StringTokenizer class. 我会尝试使用StringTokenizer类。

Capture the input as a string, then make the StringTokenizer like this: 将输入捕获为字符串,然后使StringTokenizer如下所示:

String delimiter =" ";
StringTokenizer st = new StringTokenizer(inputString, delimiter);

The delimiter is string which you have between the interesting pieces of the string, so you can set it to be whitespace. 分隔符是字符串,您可以在字符串的各个有趣部分之间使用它,因此可以将其设置为空格。

After that, you can extract parts of the input by calling nextToken method. 之后,您可以通过调用nextToken方法提取部分输入。

For example: 例如:

String firstTerm=st.nextToken();
String operator=st.nextToken();
String secondTerm=st.nextToken();

double ft=Double.parseDouble(firstTerm);
double st=Double.parseDouble(secondTerm);

Then you can make a decision which operation to do based on the input operator using for example a switch statement. 然后,您可以使用输入语句(例如switch语句)基于输入运算符来决定要执行的操作。

You may need to do some exception handling here, since StringTokenizer can throw exceptions. 您可能需要在此处进行一些异常处理,因为StringTokenizer会引发异常。

If you so wish, you can remain on a lower level and do all this by hand. 如果您愿意,可以保持较低级别,并手动完成所有这些操作。 Simply make a string and then read through your character array character by character until you reach a delimiter. 只需创建一个字符串,然后逐个字符地读取字符数组,直到到达定界符。 You could do that in a loop which would end when you reach a delimiter. 您可以在到达定界符的循环中完成此操作。 Then you could use the class to parse number from the string. 然后,您可以使用该类来解析字符串中的数字。 Did you notice the capital D in Double? 您是否注意到Double中的大写字母D? It means that we're calling function of a class called Double which will parse the number from the string. 这意味着我们正在调用名为Double的类的函数,该类将从字符串中解析数字。 There are also classes Integer for int, Float for single precision floating point numbers and so on. 还有用于Int的Integer类,用于单精度浮点数的Float类,等等。

If the expression is longer, you can still use StringTokenizer, since there's a method called hasMoreElements which you can use to check if you're at the end of input. 如果表达式较长,则仍然可以使用StringTokenizer,因为有一个名为hasMoreElements的方法可用于检查是否在输入末尾。

This might be a total overkill if you are not going to extend your program and want to keep it as simple as possible. 如果您不打算扩展程序并希望使其尽可能简单,那么这可能是完全的矫kill过正。 (We had this method in our first semester of Java programming so it should be a good exercise) You could use the Reverse Polish Notation ( Idea: Wikipedia ) using a stack for your numbers. (我们在Java编程的第一学期就采用了这种方法,因此这应该是一个很好的练习。)您可以使用带有数字堆栈的反向波兰符号( Idea:Wikipedia )。 This would enable you to use parentheses and perform multiple operations in the right order (*,/ before +,-). 这将使您能够使用括号并以正确的顺序(*,/在+,-之前)执行多项操作。 On the other hand you would need the parentheses around each basic operation (eg " ((A+B)+C) "). 另一方面,您将需要在每个基本操作周围加上括号(例如“ (((A + B)+ C) “)。

When I implemented a calculator this way I went character by character through the string adding up numbers with two digits or digits after the comma. 当我以这种方式实现计算器时,我逐个字符地将字符串与两个数字或逗号后面的数字相加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM