다음은 중위어를 posfix 표현식으로 변환하는 코드입니다. 문제는 변환 방법입니다. 내가 스레드에서 줄에 21JAVA - 왜 중첩 부분을 수정 표현식으로 변환하려고 시도하는 동안 배열 인덱스가 바운드 오류가 발생합니까?
오류-AB는 + 예외 오류가 발생하고있다 "기본"java.lang.ArrayIndexOutOfBoundsException : 3> = 0 java.util.Vector.elementAt (알 수없는 소스)에서
infixpostfix.conversion (infixpostfix.java:22) infixpostfix.main에서
(infixpostfix.java:77) 현재 입력
에서
.
내 문자열의 길이를 초과하면 i
에 액세스하려고하기 때문에이 오류가 나타나는 것으로 생각합니다. 하지만 내 가치가 for 루프에 의해 관리 될 때 어떻게 가능할까요?
Code:-
import java.util.Stack;
public class infixpostfix {
Stack<Character> st = new Stack<Character>();
void conversion(String e){
for(int i = 0 ; i < e.length() ; i++){
if(isOp(e.charAt(i))){
if(st.isEmpty()){
st.push(e.charAt(i));
}
else{
while(!st.isEmpty() && checkPrec(st.peek())<=checkPrec(e.charAt(i))){
System.out.println(st.pop());
}
st.push(st.elementAt(i));
}
}
else{
System.out.print(e.charAt(i));
}
}
while(!st.isEmpty()){
System.out.print(st.pop());
}
}
int checkPrec(char o){
switch(o){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return -1;
}
}
boolean isOp(char c){
if(c=='+' || c=='-' || c=='/' || c=='*'){
return true;
}
else{
return false;
}
}
public static void main(String args[]){
infixpostfix obj = new infixpostfix();
obj.conversion("a+b-c/d*f");
}
}
'st.push (st.elementAt (i));'- 여기서 무엇을하려고합니까? – GurV