게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
자바에서 출력하면 ㅁㅁㅁㅁㅁㅁ가 계속떠요!!! 이거 도와주실 분 계시나요
게시물ID : programmer_2281짧은주소 복사하기
작성자 : mo190
추천 : 0
조회수 : 532회
댓글수 : 9개
등록시간 : 2014/04/01 06:15:47
public static void main(String[] args)
{
int i=0;
int j=0;
char[] str= new char[100]; // array where we can store infix character.
char[] str1= new char[100]; // used in main function for storing prefix character.
char[] stack=new char[100]; // array where we can store operand.
 
while(true){
top=0;
Scanner mo = new Scanner(System.in);
System.out.print("type the notation : ");
String strr = mo.nextLine();
strr.getChars(0, strr.length(), str, 0);

System.out.println("what you typed : " + strr);
System.out.print("result of the prefix : ");
for(i=0,j=0 ; i<str.length ; i++)
{
if( Character.isDigit(str[i]) ) //if component will digit.
{
str1[j++]=str[i]; //store print value to str1 array for calculating.
System.out.print(str[i]);
 
if ( i==(str.length-1) ) //if 'i'value would go end of the array.
{
while( top>-1 ) //pop every component from operand stack.
{    
pop(stack);
if(temp!=0) //if result of the pop will '(' or ')', we don't store result to str1 array.
{
str1[j++]=temp;
}
}
}
}
else //if component will operand
{
if ( str[i] == '(' || str[i] == ')' )  // if component will '(' or ')'
{
push(stack, str[i]);
if ( str[i] == ')' )
{
while ( stack[top] != '(' )
{
pop(stack);
if(temp!=0) //if result of the pop will '(' or ')', we don't store result to str1 array.
{
str1[j++]=temp;
}
}
pop(stack); // pop '('
if(temp!=0)
{
str1[j++]=temp;
}
if ( i==(str.length-1) )  //if 'i'value would go end of the array.
{
while( top!=0 ) //pop every component from operand stack. 
{  
pop(stack);
str1[j++]=temp;
}
}
}
}
else if( top > 0 && prior(str[i]) <= prior(stack[top]) ) //if new operand's priority will lower or same with stack's operand's.
{
while(top > 0 && prior(str[i]) <= prior(stack[top])) //pop up to new operand will replace the stack's operand.
{
pop(stack);
str1[j++]=temp;
}
push(stack,str[i]); //push new operand.
}
else //if new operand's priority will higher than stack's
{
push(stack, str[i]);
}
}
}
System.out.print("\n");
// System.out.print("result : ");
//calculate(str1); //calculate function; calculate prefix string 
}
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호