목차
1. 분석
1) 문제
2) 문법 (V)
3) 가정 (V)
4) 설계
1-4-1. 전체구조
1-4-2. 함수별기능
1-4-3. 알고니즘
2. 코딩
* (V)표시되어 있는건 프로그래밍 언어론을 배우시는 분들에게 필요한 내용이므로 단순히 JAVA프로그래밍을 들으시는 분들에게는 필요 없는 내용입니다.
1) 문제
2) 문법 (V)
3) 가정 (V)
4) 설계
1-4-1. 전체구조
1-4-2. 함수별기능
1-4-3. 알고니즘
2. 코딩
* (V)표시되어 있는건 프로그래밍 언어론을 배우시는 분들에게 필요한 내용이므로 단순히 JAVA프로그래밍을 들으시는 분들에게는 필요 없는 내용입니다.
본문내용
se '(' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = -1; break;
case ')' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 0; break;
case '+' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 5; break;
case '-' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 4; break;
case '*' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 3; break;
case '/' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 2; break;
case '^' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 1; break;
}
}
/* 층번호 구하기 */
sta[0][0] = 1;
for (int i=1; i < abeq.length(); i++)
{
if (abeq.charAt(i) == ')')
{
if (abeq.charAt(i-1) == '(')
sta[0][i] = sta[0][i-1];
else sta[0][i] = sta[0][i-1]-1;
}
else
{
if (abeq.charAt(i-1) == '(')
sta[0][i] = sta[0][i-1]+1;
else sta[0][i] = sta[0][i-1];
}
}
return sta ; // str을 return한다
}
/* 배열의 i-번째 원소 앞에 그 i-번째 원소값보다 작은 값을 갖는
원소의 수를 구하는 메쏘드 */
private static int howMany(int[][] st0, int i)
{
int count=0;
for (int j=0; j < i; j++)
{
if (st0[j][2]
}
return count ;
}
/* 구조배열로부터 계산순서배열 구하는 메쏘드 */
private static int[][] getStep(int[][] sta)
{
int dim = sta[0].length;
int[][] st0 = new int[dim][3];
int[][] st = new int[dim][2];
// st0 구하기
for (int i=0; i
{
st0[i][0] = sta[0][i] ;
st0[i][1] = sta[1][i] ;
st0[i][2] = i ;
}
int[] temp = new int[3];
// step 구하기
for (int i=1; i
{
int j=i;
while (st0[j][0] > st0[j-1][0] | (st0[j][0] == st0[j-1][0] & st0[j][1] < st0[j-1][1]) )
{
temp = st0[j-1];
st0[j-1]=st0[j];
st0[j]=temp;
if (j>1) j--;
}
}
// st 배열 구하기
for (int i=0; i
{
st[i][0] = st0[i][1] ;
st[i][1] = st0[i][2] - howMany(st0, i);
}
return st; // st값 반환
}
public String calculate(String eq)
{
String comeq = compactify(eq) ; // 수식의 공백문자 제거
int[][] sta = getStructureArray(comeq) ; // 구조배열 구하기
if (sta == null)
return eq; // 연산자가 없을 때
int[][] st = getStep(sta) ; // 계산순서배열 구하기
/* 피연산자 배열 정의 */
int dim = st.length;
String[][] opnd = new String[dim+1][];
for (int d = 0; d < dim+1 ; d++)
{
opnd[d] = new String[dim+1-d];
}
/* 계산과정 */
/* 1. 피연산자 초기화 */
opnd[0][0]=comeq.substring(0,sta[2][0]) ;
for (int i=1; i < dim; i++)
{
opnd[0][i]=comeq.substring(sta[2][i-1]+1,sta[2][i]) ;
}
opnd[0][dim]=comeq.substring(sta[2][dim-1]+1) ;
/* 2. 계산 수행 */
for (int i=0; i
{
if (st[i][0] == -1)
{
opnd[i+1][st[i][1]] = Calculator_1.apply(opnd[i][st[i][1]+1],opnd[i][st[i][1]],st[i][1]) ;
}
else if (st[i][0] == 0 )
{
opnd[i+1][st[i][1]] = opnd[i][st[i][1]] ;
}
else if (st[i][0] != -1 & st[i][0] != 0)
opnd[i+1][st[i][1]] = Calculator_1.apply(opnd[i][st[i][1]],opnd[i][st[i][1]+1],st[i][0]) ;
for (int j=0; j
{
opnd[i+1][j] = opnd[i][j] ;
}
for (int k=st[i][1]+1; k
{
opnd[i+1][k] = opnd[i][k+1] ;
}
}
/* 3. 최종 결과 */
return opnd[dim][0] ; // opnd[dim][0]값 반환
}
}
sta[2][j] = i; sta[1][j] = -1; break;
case ')' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 0; break;
case '+' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 5; break;
case '-' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 4; break;
case '*' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 3; break;
case '/' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 2; break;
case '^' : j++ ; abeq.append(comeq.charAt(i)) ;
sta[2][j] = i; sta[1][j] = 1; break;
}
}
/* 층번호 구하기 */
sta[0][0] = 1;
for (int i=1; i < abeq.length(); i++)
{
if (abeq.charAt(i) == ')')
{
if (abeq.charAt(i-1) == '(')
sta[0][i] = sta[0][i-1];
else sta[0][i] = sta[0][i-1]-1;
}
else
{
if (abeq.charAt(i-1) == '(')
sta[0][i] = sta[0][i-1]+1;
else sta[0][i] = sta[0][i-1];
}
}
return sta ; // str을 return한다
}
/* 배열의 i-번째 원소 앞에 그 i-번째 원소값보다 작은 값을 갖는
원소의 수를 구하는 메쏘드 */
private static int howMany(int[][] st0, int i)
{
int count=0;
for (int j=0; j < i; j++)
{
if (st0[j][2]
return count ;
}
/* 구조배열로부터 계산순서배열 구하는 메쏘드 */
private static int[][] getStep(int[][] sta)
{
int dim = sta[0].length;
int[][] st0 = new int[dim][3];
int[][] st = new int[dim][2];
// st0 구하기
for (int i=0; i
st0[i][0] = sta[0][i] ;
st0[i][1] = sta[1][i] ;
st0[i][2] = i ;
}
int[] temp = new int[3];
// step 구하기
for (int i=1; i
int j=i;
while (st0[j][0] > st0[j-1][0] | (st0[j][0] == st0[j-1][0] & st0[j][1] < st0[j-1][1]) )
{
temp = st0[j-1];
st0[j-1]=st0[j];
st0[j]=temp;
if (j>1) j--;
}
}
// st 배열 구하기
for (int i=0; i
st[i][0] = st0[i][1] ;
st[i][1] = st0[i][2] - howMany(st0, i);
}
return st; // st값 반환
}
public String calculate(String eq)
{
String comeq = compactify(eq) ; // 수식의 공백문자 제거
int[][] sta = getStructureArray(comeq) ; // 구조배열 구하기
if (sta == null)
return eq; // 연산자가 없을 때
int[][] st = getStep(sta) ; // 계산순서배열 구하기
/* 피연산자 배열 정의 */
int dim = st.length;
String[][] opnd = new String[dim+1][];
for (int d = 0; d < dim+1 ; d++)
{
opnd[d] = new String[dim+1-d];
}
/* 계산과정 */
/* 1. 피연산자 초기화 */
opnd[0][0]=comeq.substring(0,sta[2][0]) ;
for (int i=1; i < dim; i++)
{
opnd[0][i]=comeq.substring(sta[2][i-1]+1,sta[2][i]) ;
}
opnd[0][dim]=comeq.substring(sta[2][dim-1]+1) ;
/* 2. 계산 수행 */
for (int i=0; i
if (st[i][0] == -1)
{
opnd[i+1][st[i][1]] = Calculator_1.apply(opnd[i][st[i][1]+1],opnd[i][st[i][1]],st[i][1]) ;
}
else if (st[i][0] == 0 )
{
opnd[i+1][st[i][1]] = opnd[i][st[i][1]] ;
}
else if (st[i][0] != -1 & st[i][0] != 0)
opnd[i+1][st[i][1]] = Calculator_1.apply(opnd[i][st[i][1]],opnd[i][st[i][1]+1],st[i][0]) ;
for (int j=0; j
opnd[i+1][j] = opnd[i][j] ;
}
for (int k=st[i][1]+1; k
opnd[i+1][k] = opnd[i][k+1] ;
}
}
/* 3. 최종 결과 */
return opnd[dim][0] ; // opnd[dim][0]값 반환
}
}
추천자료
자바와 VRML의 활용
자바 애플릿을 이용한 이진 트리 그래픽 구현
[자바][JAVA][자바스크립트]자바(JAVA)의 정의, 자바(JAVA)의 특징, 자바(JAVA)의 역사, 자바...
[JAVA][자바][문자출력][Class선언][CGI연계][프로그래밍언어]JAVA(자바)의 의미, JAVA(자바)...
[JSP][자바 애플릿][JSP의 이해][자바 애플릿의 이해][JSP의 문법][자바 애플릿의 문법]JSP(...
[자바][JAVA][자바스크립트]자바(JAVA)의 의미, 자바(JAVA)의 특징, 자바(JAVA)의 유래, 자바...
자바(Java)의 특징, 자바(Java)의 기술, 자바(Java)의 장점, 자바스크립트(JavaScript)의 정...
소개글