Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char ope[5]= {'+','-','/','^'};
- char f(char a)
- {
- if(a==ope[0] || a==ope[1]) return 1;
- if(a==ope[2] || a==ope[3]) return 2;
- else return 3;
- }
- int main()
- {
- char input[500]= {'\0'},output[500]= {'\0'},stack[500]= {'\0'};
- int i,n=0,s1,s2,top1,top2; // s คือ คะแนน ope top1 ใช้กับ stack
- //printf("%s",ope);
- scanf("%s",input);
- for(i=0; i<strlen(input); i++)
- {
- top1=strlen(stack);
- top2=strlen(output);
- if(input[i]>='A' && input[i]<='Z')
- {
- output[n]=input[i];
- n++;
- }
- else if(input[i]!='(' && input[i]!=')')
- {
- if(top1==0) stack[0]=input[i];
- else
- {
- while(1)
- {
- s1=f(input[i]);
- s2=f(stack[top1-1]);
- if(s1>s2)
- {
- stack[top1]=input[i];
- break;
- }
- else
- {
- output[n]=stack[top1-1];
- n++;
- stack[top1-1]='\0';
- }
- }
- }
- }
- else if(input[i]=='(')
- {
- stack[top1]=input[i];
- }
- else if(input[i]==')')
- {
- input[i]=='\0';
- s1=i-1;
- while(1)
- {
- if(input[s1]=='(')
- {
- input[s1]=='\0';
- break;
- }
- output[n]=input[s1];
- s1--;
- }
- }
- }
- if(strlen(stack)!=0)
- {
- strrev(stack);
- }
- printf("\n\nOutput : %s",output);
- for(i=0;i<strlen(stack);i++)
- {
- if(stack[i]!='(' && stack[i]!=')') printf("%c",stack[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement