Java 将某些字符推送到某些字符串。中缀到后缀
我想制作一个计算器,只使用字符串和字符将中缀转换为后缀 以下是要求: 对于i=1到m: 如果c_i是操作数:将c_i传输到输出。 -如果c_i是左括号:将c_i推到tmp。 -如果c_i是右括号:来自tmp和transfer的Pop元素 -将它们输出到左括号中 -是的。弹出左括号。 如果c_i是一个操作符:让顶部的tmp元素为t。流行音乐 -将元件从tmp传输到输出 -直至: -p(t)Java 将某些字符推送到某些字符串。中缀到后缀,java,calculator,infix-notation,Java,Calculator,Infix Notation,我想制作一个计算器,只使用字符串和字符将中缀转换为后缀 以下是要求: 对于i=1到m: 如果c_i是操作数:将c_i传输到输出。 -如果c_i是左括号:将c_i推到tmp。 -如果c_i是右括号:来自tmp和transfer的Pop元素 -将它们输出到左括号中 -是的。弹出左括号。 如果c_i是一个操作符:让顶部的tmp元素为t。流行音乐 -将元件从tmp传输到输出 -直至: -p(t)
静态字符串infixToPostfix(字符串中缀){
字符串后缀=”;
中缀=读线();
字符串temp=“”;
字符串输出=”;
对于(int i=0;i您的代码似乎不完整。下面是我自己将中缀字符串转换为后缀的方法。希望对您有所帮助
boolean chk = true;
for(int i=0; i<expression.length(); i++) {
char ch = expression.charAt(i);
//Conditions for generating postfix String
if(ch=='(') {
operator.push(ch);
chk=false;
}
else if(isDigit(ch)) {
if(!chk) {
postfix = postfix + " " + ch;
}
else {
postfix = postfix + ch;
}
chk=true;
}
else if(ch == '+' || ch=='-') {
if(operator.isEmpty()) {
operator.push(ch);
}
else if(operator.peep()=='/'|| perator.peep()=='*'||operator.peep()=='-') {
postfix = postfix + " " + operator.pop();
operator.push(ch);
}
else {
operator.push(ch);
}
chk = false;
}
else if(ch == '*' || ch == '/') {
if(operator.isEmpty()) {
operator.push(ch);
}
else if(operator.peep()=='+' || operator.peep()=='-') {
operator.push(ch);
}
else {
postfix = postfix + " " + operator.pop();
operator.push(ch);
}
chk = false;
}
else if(ch == ')') {
while(operator.peep()!='(') {
postfix = postfix + " " + operator.pop();
}
operator.pop();
}
}
while(!operator.isEmpty()) {
postfix = postfix + " " + operator.pop();
}
布尔值chk=true;
对于(int i=0;i
boolean chk = true;
for(int i=0; i<expression.length(); i++) {
char ch = expression.charAt(i);
//Conditions for generating postfix String
if(ch=='(') {
operator.push(ch);
chk=false;
}
else if(isDigit(ch)) {
if(!chk) {
postfix = postfix + " " + ch;
}
else {
postfix = postfix + ch;
}
chk=true;
}
else if(ch == '+' || ch=='-') {
if(operator.isEmpty()) {
operator.push(ch);
}
else if(operator.peep()=='/'|| perator.peep()=='*'||operator.peep()=='-') {
postfix = postfix + " " + operator.pop();
operator.push(ch);
}
else {
operator.push(ch);
}
chk = false;
}
else if(ch == '*' || ch == '/') {
if(operator.isEmpty()) {
operator.push(ch);
}
else if(operator.peep()=='+' || operator.peep()=='-') {
operator.push(ch);
}
else {
postfix = postfix + " " + operator.pop();
operator.push(ch);
}
chk = false;
}
else if(ch == ')') {
while(operator.peep()!='(') {
postfix = postfix + " " + operator.pop();
}
operator.pop();
}
}
while(!operator.isEmpty()) {
postfix = postfix + " " + operator.pop();
}