java - 从后缀转换为中缀后如何计算算术转换?
问题描述
我正在编写一个程序,它采用后缀表达式并将其转换为中缀表达式并输出结果。这是我到目前为止编写的代码,但我不知道如何在转换后进行计算。
public String convert(String postfix)
{
Stack<String> variables = new Stack<>(); // declaring a new stack to
hold the variables
for(int i = 0; i < postfix.length(); i++)
{
char c = postfix.charAt(i);
if(isOperator(c))
{
String a = variables.pop();
String b = variables.pop();
variables.push("("+a+c+b+")");
}
else
variables.push(""+c);
}
return variables.pop();
}
我尝试在函数中添加 if/switch 语句,但我得到了“不兼容的堆栈和字符串错误”。我试图做类似的事情:
if (variables == abc++)
{
(a + (b + c));
}
我该怎么做呢?
解决方案
推荐阅读
- python-2.7 - 在另一列pyspark中创建具有字符串长度的列
- c# - 如何将相机添加到 Matrix4x4 旋转
- heroku - 在打开 heroku 的情况下获取多个应用程序的列表
- python - 从 100.21 Hz 到 8 Hz 下采样信号(非整数抽取因子)
- sublimetext3 - Sublime Text 3:主配置?(用于:灾难恢复、移植到新机器等)
- javascript - 比较 2 个数组并分配匹配值
- c++ - C++:使用指向成员的指针访问子结构的数据成员
- ios - 自动布局不适用于 Plus 型号
- ios - 尽管有条件声明,Segue 仍然通过
- css - jquery 使默认导航栏按钮处于活动状态