infix to postfix [소스]
#include <iostream>
// 문자를 저장하는 스택과 함수 정의
#define char_stack_size 30
using namespace std;
char char_stack[char_stack_size];
int char_top = -1;
int char_Isfull()
{
if(char_top>=char_stack_size-1)return 1;
else return 0;
}
int char_Isempty()
{
if(char_top<=-1)return 1;
else return 0;
}
void char_stack_full()
{
cout << endl << " ** err : char_stack full **" << endl;
}
void char_stack_add(char item)
{
if(char_top>=char_stack_size-1){
char_stack_full();
return;
}
char_stack[++char_top] = item;
}
char char_stack_empty()
{
cout << endl << " ** err : char_stack empty **" << endl;
char_stack[0] = '0';
return char_stack[0];
}
char char_stack_delete()
{
char item;
if(char_top <= -1)
return char_stack_empty();
item = char_stack[char_top--];
return item;
}
// 문자를 저장하는 스택 정의 끝
// 함수 Prototype
int operand(char x);
int Isp(char x);
int Icp(char x);
void postfix(char *token, char *post);
void main()
{
char exp[50];
char post[50];
char c = 'y';
while(c=='y')
{
cout << endl << "* enter infix expression : ";
cin >> exp;
postfix(exp, post);
cout << " * postfix notation : " << post << endl;
cout << "\n Do you want to do another expression('y'or'n')? ";
cin >> c;
}
}
int operand(char x)
{
char op[8] = {'(',')','+','-','*','/','%','\0'};
for(int i=0; i<8; i++)if(op[i]==x)return 0;
return 1;
}
int Isp(char x)
{
char op[8] = {'(',')','+','-','*','/','%','\0'};
int sp[8] = {0,19,12,12,13,13,13,0};
int i;
for(i=0; i<8; i++)if(op[i]==x)return sp[i];
cout << "* Isp error *\n";
return 0;
}
int Icp(char x)
{
char op[8] = {'(',')','+','-','*','/','%','\0'};
int cp[8] = {20,19,12,12,13,13,13,0};
int i;
for(i=0; i<8; i++)if(op[i]==x)return cp[i];
cout << "* Icp error *\n";
return 0;
}
void postfix(char *token, char *post)
{
char x;
int ipost = 0;
char_top = -1;
char_stack_add('\0');
for(int i=0; token[i]!='\0'; i++)
{
x=token[i];
if(operand(x))post[ipost++] = x;
else if(x==')'){
while(char_stack[char_top]!='(')post[ipost++] = char_stack_delete();
char_stack_delete();
}
else{
while(Isp(char_stack[char_top])>=Icp(x))post[ipost++] = char_stack_delete();
char_stack_add(x);
}
}
while((x=char_stack_delete()) != '\0')
post[ipost++] = x;
post[ipost] = '\0';
return;
}
'테크노트 > 기타' 카테고리의 다른 글
Grub 삭제하는 방법 (0) | 2006.09.28 |
---|---|
MFC 계산기 예제 소스 (0) | 2006.09.23 |
Standard C++ Library : <ios> Members (0) | 2006.09.23 |
infix -> postfix (0) | 2006.09.15 |
산술식 (A*(B-C)/(D**2))-E의 수식을 후위표기법 과정을 이용하여 기술하라 (0) | 2006.09.12 |