Created
July 5, 2009 01:27
-
-
Save anonymous/140790 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #define MAX 50 | |
| typedef struct stack{ | |
| int top; | |
| char items[MAX]; | |
| }OPER; | |
| char pop(OPER* stk); | |
| char looktop(OPER* stk); | |
| void push(OPER* stk, const char* opnd); | |
| int isoperator(const char* symb); | |
| int cmp(const char* now, const char* top); | |
| int postfix(char a[]); | |
| int main(void){ | |
| int i, j; | |
| int count, N; | |
| char buf[5]; | |
| char post[MAX]; | |
| fgets(buf, 5, stdin); | |
| sscanf(buf, "%d", &N); | |
| fgets(buf, 5,stdin); | |
| for(i=1; i<=N; i++){ | |
| if(i != 1) | |
| printf("\n"); | |
| count = postfix(post); | |
| for(j=0; j<count; j++){ | |
| printf("%c", post[j]); | |
| } | |
| printf("\n"); | |
| } | |
| return 0; | |
| } | |
| int postfix(char a[]){ | |
| char buf[5]; | |
| char ch,top; | |
| int count; /* for a[] */ | |
| OPER stk; | |
| stk.top=-1; | |
| count = 0; | |
| while(fgets(buf, 5, stdin) != NULL){ | |
| if( buf[0] == '\n') | |
| break; | |
| sscanf(buf,"%c", &ch); | |
| if( isoperator(&ch) ){ | |
| if( ch == '(' ){ | |
| push(&stk, &ch); | |
| } | |
| else if( ch == ')' ){ | |
| top = pop(&stk); | |
| while( top != '(' ){ | |
| a[count++] = top; | |
| top = pop(&stk); | |
| } | |
| } | |
| else{ | |
| top = looktop(&stk); | |
| while( cmp(&ch, &top) <= 0 ){ | |
| a[count++] = pop(&stk); | |
| top = looktop(&stk); | |
| } | |
| push(&stk, &ch); | |
| } | |
| } | |
| else{ | |
| a[count++] = ch; | |
| } | |
| } | |
| top = pop(&stk); | |
| while( top != 'z' ){ | |
| a[count++] = top; | |
| top = pop(&stk); | |
| } | |
| return count; | |
| } | |
| char pop(OPER* stk){ | |
| /* if empty return 'z' */ | |
| if( stk->top == -1 ){ | |
| return 'z'; | |
| } | |
| else{ | |
| return stk->items[(stk->top)--]; | |
| } | |
| } | |
| char looktop(OPER* stk){ | |
| /* if empty return 'z' */ | |
| if( stk->top == -1 ){ | |
| return 'z'; | |
| } | |
| else{ | |
| return stk->items[stk->top]; | |
| } | |
| } | |
| void push(OPER* stk, const char* opnd){ | |
| stk->items[++(stk->top)] = *opnd; | |
| } | |
| int isoperator(const char* symb){ | |
| return (*symb < '0' || *symb > '9'); | |
| } | |
| /* 1: now>top, 0: now=top -1: now<top */ | |
| int cmp(const char* now, const char* top){ | |
| int vnow; /* value of now */ | |
| int vtop; | |
| switch(*now){ | |
| case '*': vnow=1; break; | |
| case '/': vnow=1; break; | |
| case '+': vnow=0; break; | |
| case '-': vnow=0; break; | |
| } | |
| switch(*top){ | |
| case '(': return 1; | |
| case '*': vtop=1; break; | |
| case '/': vtop=1; break; | |
| case '+': vtop=0; break; | |
| case '-': vtop=0; break; | |
| case 'z': return 1; | |
| } | |
| return (vnow - vtop); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment