前置表达式中的负操作数 JAVA RPn解析器
前置表达式中的负操作数 JAVA RPn解析器,java,parsing,calculator,Java,Parsing,Calculator,else if(token.equals(“-”)){ op2=stack.pop(); op1=stack.pop(); stack.push(op2*-1);} 如果我输入的是“-4”或数字前面带负号的东西 它会带来错误 有什么想法吗?如果您想在RPN(反向波兰符号)中解析负数,您需要在将负数推送到堆栈之前在解析器中进行检测 如果你正在读一个减号,而下一个符号(空格前)是一个数字,它将是数字的一部分。看一看: import java.util.*; public class RPNPars
else if(token.equals(“-”)){
op2=stack.pop();
op1=stack.pop();
stack.push(op2*-1);}
如果我输入的是“-4”或数字前面带负号的东西
它会带来错误
有什么想法吗?如果您想在RPN(反向波兰符号)中解析负数,您需要在将负数推送到堆栈之前在解析器中进行检测 如果你正在读一个减号,而下一个符号(空格前)是一个数字,它将是数字的一部分。看一看:
import java.util.*;
public class RPNParser {
public static void main( String[] args ) {
String rpnExp = "-4 3 + 5 * 3 - -61 *";
String[] tokens = rpnExp.split( " " );
Stack<Integer> stack = new Stack<Integer>();
Integer op1 = null;
Integer op2 = null;
Integer result = null;
for ( String token : tokens ) {
if ( token.equals( "+" ) ) {
op2 = stack.pop();
op1 = stack.pop();
stack.push( op1 + op2 );
} else if ( token.equals( "-" ) ) {
op2 = stack.pop();
op1 = stack.pop();
stack.push( op1 - op2 );
} else if ( token.equals( "*" ) ) {
op2 = stack.pop();
op1 = stack.pop();
stack.push( op1 * op2 );
} else if ( token.equals( "/" ) ) {
op2 = stack.pop();
op1 = stack.pop();
stack.push( op1 / op2 );
} else {
stack.push( Integer.valueOf( token ) );
}
}
result = stack.pop();
System.out.printf( "%s = %d\n", rpnExp, result );
}
}
如果您想手工解析字符串(不使用split),您将需要做更多的工作,但这非常简单
String rpnExp = "-4 3 + 5 * 3 - -61 *";
StringBuilder newToken = new StringBuilder();
List<String> tokens = new ArrayList<String>();
for ( char c : rpnExp.toCharArray() ) {
// the current char is a space?
if ( c == ' ' ) {
// yes, it is.
// the new token has something?
if ( newToken.length() > 0 ) {
// yes, it has
// add its value to the token list
tokens.add( newToken.toString() );
// resets new token
newToken = new StringBuilder();
}
} else { // c is not a space
// yes, so add it to the newToken
newToken.append( c );
}
}
// needs to process the last value of newToken
// so, if it has something
if ( newToken.length() > 0 ) {
// add the value to the token list
tokens.add( newToken.toString() );
}
Stack<Integer> stack = new Stack<Integer>();
// the rest of the code here ...
String rpnExp=“-43+5*3--61*”;
StringBuilder newToken=新StringBuilder();
List tokens=new ArrayList();
for(字符c:rpnExp.toCharArray()){
//当前字符是一个空格吗?
如果(c=''){
//是的。
//新的代币有东西吗?
如果(newToken.length()>0){
//是的,有
//将其值添加到令牌列表
add(newToken.toString());
//重置新令牌
newToken=新的StringBuilder();
}
}否则{//c不是一个空格
//是的,所以把它加到纽顿
新增(c);
}
}
//需要处理newToken的最后一个值
//所以,如果它有什么
如果(newToken.length()>0){
//将该值添加到令牌列表中
add(newToken.toString());
}
堆栈=新堆栈();
//这里剩下的代码。。。
如果要在RPN(反向波兰语表示法)中解析负数,则需要先在解析器中检测负数,然后再将负数推送到堆栈中
如果你正在读一个减号,而下一个符号(空格前)是一个数字,它将是数字的一部分。看一看:
import java.util.*;
public class RPNParser {
public static void main( String[] args ) {
String rpnExp = "-4 3 + 5 * 3 - -61 *";
String[] tokens = rpnExp.split( " " );
Stack<Integer> stack = new Stack<Integer>();
Integer op1 = null;
Integer op2 = null;
Integer result = null;
for ( String token : tokens ) {
if ( token.equals( "+" ) ) {
op2 = stack.pop();
op1 = stack.pop();
stack.push( op1 + op2 );
} else if ( token.equals( "-" ) ) {
op2 = stack.pop();
op1 = stack.pop();
stack.push( op1 - op2 );
} else if ( token.equals( "*" ) ) {
op2 = stack.pop();
op1 = stack.pop();
stack.push( op1 * op2 );
} else if ( token.equals( "/" ) ) {
op2 = stack.pop();
op1 = stack.pop();
stack.push( op1 / op2 );
} else {
stack.push( Integer.valueOf( token ) );
}
}
result = stack.pop();
System.out.printf( "%s = %d\n", rpnExp, result );
}
}
如果您想手工解析字符串(不使用split),您将需要做更多的工作,但这非常简单
String rpnExp = "-4 3 + 5 * 3 - -61 *";
StringBuilder newToken = new StringBuilder();
List<String> tokens = new ArrayList<String>();
for ( char c : rpnExp.toCharArray() ) {
// the current char is a space?
if ( c == ' ' ) {
// yes, it is.
// the new token has something?
if ( newToken.length() > 0 ) {
// yes, it has
// add its value to the token list
tokens.add( newToken.toString() );
// resets new token
newToken = new StringBuilder();
}
} else { // c is not a space
// yes, so add it to the newToken
newToken.append( c );
}
}
// needs to process the last value of newToken
// so, if it has something
if ( newToken.length() > 0 ) {
// add the value to the token list
tokens.add( newToken.toString() );
}
Stack<Integer> stack = new Stack<Integer>();
// the rest of the code here ...
String rpnExp=“-43+5*3--61*”;
StringBuilder newToken=新StringBuilder();
List tokens=new ArrayList();
for(字符c:rpnExp.toCharArray()){
//当前字符是一个空格吗?
如果(c=''){
//是的。
//新的代币有东西吗?
如果(newToken.length()>0){
//是的,有
//将其值添加到令牌列表
add(newToken.toString());
//重置新令牌
newToken=新的StringBuilder();
}
}否则{//c不是一个空格
//是的,所以把它加到纽顿
新增(c);
}
}
//需要处理newToken的最后一个值
//所以,如果它有什么
如果(newToken.length()>0){
//将该值添加到令牌列表中
add(newToken.toString());
}
堆栈=新堆栈();
//这里剩下的代码。。。
您想要什么?一个简单的解析器,它使用一个堆栈来处理postfix表达式?发布更多的代码,并详细解释您面临的问题。您想要什么?一个简单的语法分析器,它使用一个堆栈来表示postfix表达式?发布更多的代码,并详细解释您面临的问题。