본문내용
ur) == L) {
while(priority(peer().what,cur) == L)
post[p_posi++] = pop();
if(priority(peer().what,cur) == E)
post[p_posi++] = pop();
added(in[i_posi]);
}
else return -1;
}
post[p_posi].what = END;
post[p_posi].value= 0;
return 0;
}
//priority
int priority(int left,int right)
{
int table[8][8] = {
{N,R,R,R,R,R,R,N},
{L,N,L,L,L,L,N,L},
{L,R,R,L,L,L,R,L},
{L,R,R,L,R,L,R,L},
{L,R,R,L,R,L,R,L},
{L,R,R,R,R,L,R,L},
{N,R,R,R,R,R,R,E},
{L,N,N,L,L,L,N,L},
};
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
if(left == i && right == j)
return(table[i][j]);
return 0;
}
//calculate postfix..
int calcul_postfix(TOKEN *post)
{
int a,b;
TOKEN temp;
initial_stack();
for(int i=0; post[i].what != END; i++)
{
switch(post[i].what) {
case NUMBER :
added(post[i]);
break;
case SIGN :
a = pop().value;
temp.what = NUMBER;
temp.value = (post[i].value == '-') ? -a : a;
added(temp);
break;
default :
switch(post[i].value) {
case '*' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = a * b;
added(temp);
break;
case '/' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = a / b;
added(temp);
break;
case '^' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = power(a,b);
added(temp);
break;
case '+' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = a + b;
added(temp);
break;
case '-' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = a - b;
added(temp);
break;
case '(' :
case ')' :
break;
}
}
}
return pop().value;
}
//operator ^
int power(int a, int b)
{
int value = 1;
if(b==0) return value;
for(int i=0 ;i < b; i++)
value *= a;
return value;
}
//postfix printing..
void print_postfix(TOKEN* post)
{
for (int i=0 ; post[i].what != END;i++)
switch(post[i].what){
case R_PAR :
case L_PAR :
break;
case NUMBER :
cout<
break;
case SIGN :
cout<<'('<<(char)post[i].value<<')'<<" ";
break;
default :
cout<<(char)post[i].value<<" ";
break;
}
cout<
}
//sub-routin
inline int addsub(int x)
{
return((x == '+' || x == '-') ? 1 : 0);
}
inline int muldiv(int x)
{
return((x == '*' || x == '/') ? 1 : 0);
}
inline int power(int x)
{
return((x == '^') ? 1 : 0);
}
Output
Input the infix [(q)uit] : 1*2+3
Postfix expression : 1 2 * 3 +
Result : 5
Input the infix [(q)uit] : 1+2*3
Postfix expression : 1 2 3 * +
Result : 7
Input the infix [(q)uit] : 1*(2+3)
Postfix expression : 1 2 3 + *
Result : 5
Input the infix [(q)uit] : 1+2*(3+4)+1
Postfix expression : 1 2 3 4 + * + 1 +
Result : 16
Input the infix [(q)uit] : -1+(-3)*4+6
Postfix expression : 1 (-) 3 (-) 4 * + 6 +
Result : -7
Input the infix [(q)uit] : 5+3*2+4^2
Postfix expression : 5 3 2 * + 4 2 ^ +
Result : 27
Input the infix [(q)uit] : +--+- 2 +- + - -+ 3
Postfix expression : 2 (-) (+) (-) (-) (+) 3 (+) (-) (-) (+) (-) +
Result : -5
Input the infix [(q)uit] : (5*4)/(2*2)
Postfix expression : 5 4 * 2 2 * /
Result : 5
Input the infix [(q)uit] : 1s2+3^2
Postfix expression : Error
Input the infix [(q)uit] : 1#2+3
Postfix expression : Error
while(priority(peer().what,cur) == L)
post[p_posi++] = pop();
if(priority(peer().what,cur) == E)
post[p_posi++] = pop();
added(in[i_posi]);
}
else return -1;
}
post[p_posi].what = END;
post[p_posi].value= 0;
return 0;
}
//priority
int priority(int left,int right)
{
int table[8][8] = {
{N,R,R,R,R,R,R,N},
{L,N,L,L,L,L,N,L},
{L,R,R,L,L,L,R,L},
{L,R,R,L,R,L,R,L},
{L,R,R,L,R,L,R,L},
{L,R,R,R,R,L,R,L},
{N,R,R,R,R,R,R,E},
{L,N,N,L,L,L,N,L},
};
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
if(left == i && right == j)
return(table[i][j]);
return 0;
}
//calculate postfix..
int calcul_postfix(TOKEN *post)
{
int a,b;
TOKEN temp;
initial_stack();
for(int i=0; post[i].what != END; i++)
{
switch(post[i].what) {
case NUMBER :
added(post[i]);
break;
case SIGN :
a = pop().value;
temp.what = NUMBER;
temp.value = (post[i].value == '-') ? -a : a;
added(temp);
break;
default :
switch(post[i].value) {
case '*' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = a * b;
added(temp);
break;
case '/' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = a / b;
added(temp);
break;
case '^' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = power(a,b);
added(temp);
break;
case '+' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = a + b;
added(temp);
break;
case '-' :
b = pop().value;
a = pop().value;
temp.what = NUMBER;
temp.value = a - b;
added(temp);
break;
case '(' :
case ')' :
break;
}
}
}
return pop().value;
}
//operator ^
int power(int a, int b)
{
int value = 1;
if(b==0) return value;
for(int i=0 ;i < b; i++)
value *= a;
return value;
}
//postfix printing..
void print_postfix(TOKEN* post)
{
for (int i=0 ; post[i].what != END;i++)
switch(post[i].what){
case R_PAR :
case L_PAR :
break;
case NUMBER :
cout<
case SIGN :
cout<<'('<<(char)post[i].value<<')'<<" ";
break;
default :
cout<<(char)post[i].value<<" ";
break;
}
cout<
//sub-routin
inline int addsub(int x)
{
return((x == '+' || x == '-') ? 1 : 0);
}
inline int muldiv(int x)
{
return((x == '*' || x == '/') ? 1 : 0);
}
inline int power(int x)
{
return((x == '^') ? 1 : 0);
}
Output
Input the infix [(q)uit] : 1*2+3
Postfix expression : 1 2 * 3 +
Result : 5
Input the infix [(q)uit] : 1+2*3
Postfix expression : 1 2 3 * +
Result : 7
Input the infix [(q)uit] : 1*(2+3)
Postfix expression : 1 2 3 + *
Result : 5
Input the infix [(q)uit] : 1+2*(3+4)+1
Postfix expression : 1 2 3 4 + * + 1 +
Result : 16
Input the infix [(q)uit] : -1+(-3)*4+6
Postfix expression : 1 (-) 3 (-) 4 * + 6 +
Result : -7
Input the infix [(q)uit] : 5+3*2+4^2
Postfix expression : 5 3 2 * + 4 2 ^ +
Result : 27
Input the infix [(q)uit] : +--+- 2 +- + - -+ 3
Postfix expression : 2 (-) (+) (-) (-) (+) 3 (+) (-) (-) (+) (-) +
Result : -5
Input the infix [(q)uit] : (5*4)/(2*2)
Postfix expression : 5 4 * 2 2 * /
Result : 5
Input the infix [(q)uit] : 1s2+3^2
Postfix expression : Error
Input the infix [(q)uit] : 1#2+3
Postfix expression : Error
추천자료
디지털도서관과 도서관자동화 사례
[영상처리실무] 텀 프로젝트 Term Project (image2004edu)
오픈마켓 전략수립(A+레포트), 외부 환경 분석, 내부 환경 분석, 핵심 성공 요인, 기능 전략,...
디지털컨텐츠 사업에 대한 이해
미래기업의 조건 1.변화의 신호-요약본
DFT, FFT, Band Pass Filter 설계 및 신호 처리
디지털방송 전환에 관한 연구 A+
[신호및시스템설계] 여러 시정수에 해당하는 샘플링으로 아날로그와 디지털 출력을 비교
[저작권][지식재산권][지적재산권][창작물]저작권의 개념, 저작권의 디지털환경, 저작권의 남...
디지털 지식정보시대의 바람직한 리더십
B2B 전자상거래(기업간 전자상거래), B2C 전자상거래(기업과 소비자간 전자상거래), B2G 전자...
CALS(광속거래) 전자상거래(EC), B2B 전자상거래(기업간 전자상거래), B2C 전자상거래(기업과...
MATLAB 실습과 함께 배우는 아날로그 및 디지털 통신이론 (생능/김명진) MATLAB 실습 프로그램