Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Input:
- Enter the infix expression
- K+L-M*N+(O^P)*W/U/V*T+Q
- Output:
- The postfix expression is : KL+MN*-OP^W*U/V/T*+Q+
- */
- #include<stdio.h>
- #include<conio.h>
- #define Stack_Size 50
- char stack[Stack_Size];
- int top=-1;
- int Is_empty(){
- if(top==-1)return 1;
- else return 0;
- }
- void Push(int element){
- stack[++top]=element;
- }
- void Pop(){
- stack[top--]=0;
- }
- int Is_operand(char c){
- return ((c>='a' && c<='z') || (c>='A' && c<='Z'));
- }
- int prec(char c){
- if(c=='+' || c== '-') return 1;
- else if(c=='*' || c== '/') return 2;
- else if (c == '^') return 3;
- else return -1;
- }
- void Infix_to_Postfix(char inf[]){
- for(int i=0 ; inf[i] != '\0' ; i++){
- if(inf[i] == '(')Push(inf[i]);
- else if(Is_operand(inf[i]))
- printf("%c",inf[i]);
- else if (inf[i]==')'){
- while(stack[top]!='(' || top == -1){
- printf("%c",stack[top]);
- Pop();
- }
- Pop();
- }
- else{ // + , - , * , / , ^
- while( prec(stack[top])>=prec(inf[i]) ){
- printf("%c",stack[top]);
- Pop();
- }
- Push(inf[i]);
- }
- }
- while(!Is_empty()){
- printf("%c",stack[top]);
- Pop();
- }
- }
- int main()
- {
- char infix[50];
- printf("Enter the infix expression\n");
- scanf("%s",infix);
- printf("The postfix expression is : ");
- Infix_to_Postfix(infix);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment