数学运算符为Integer的字符串 [英] String with math operators to Integer

查看:206
本文介绍了数学运算符为Integer的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带数学运算符的字符串,我需要将其转换为 int (答案)。

I have a string with math operators which I need to turn into an int (the answer).

下面的代码不起作用,但我不确定如何让答案变量起作用。

The below code does not work, but I'm not sure how I can get the answer variable to work.

String question;
int answer;

question = "7/7+9-9*5/5";
answer = Integer.parseInt(question);


推荐答案

Integer.parseInt(问题); ...如果只是这么简单...

Integer.parseInt(question); ... If only it was so simple ...

你有一个数学表达式7 中缀符号中的/ 7 + 9-9 * 5/5您想要评估并将结果打印给用户。这样做的方法是使用<将其转换为后缀表示法,也称为反向波兰表示法。 a href =http://en.wikipedia.org/wiki/Shunting-yard_algorithm =nofollow> Shunting-yard算法。将表达式转换为postfix后,使用此算法非常简单

You have there a mathematical expression "7/7+9-9*5/5" in infix notation which you want to evalutate and print the result to the user. The way to do that is to convert it to postfix notation also known as Reverse Polish notation using the Shunting-yard algorithm. Once you have converted the expression to postfix evaluating is pretty easy using this algorithm

这篇关于数学运算符为Integer的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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