InfixToPostfix.cpp #include<stdio.h> #include<conio.h> int getprec(char); int check(char *); void main() { char a[100],stack[100]; int top=-1,i,j; clrscr(); printf("\nEnter the infix expression:"); gets(a); printf("\nPostfix notation is:"); if(check(a)) { for(i=0;a[i]!='\0';i++) { if((a[i]>=97&&a[i]<=122)||(a[i]>=65&&a[i]<=90)||(a[i]>=48&&a[i]<=57)) printf("%c",a[i]); else if(a[i]==')') { while(top>=0&&stack[top]!='(') { printf("%c",stack[top]); top--; } if(stack[top]=='(') top--; } else { if((stack[top-1]=='(')&&(a[i]!='(')&&top>0) { j=getprec(a[i]); while(j<=getprec(stack[top])&&top>=0&&stack[top]!='(') { printf("%c",stack[top]); top--; } } stack[++top]=a[i]; } } while(top>=0) { printf("%c",stack[top]); top--; } } else printf("\nBrases arenot completely closed"); getch(); } int check(char *a) { int i,l=0,r=0; for(i=0;a[i]!='\0';i++) if(a[i]=='(') l++; else if(a[i]==')') r++; if(l==r) return 1; return 0; } int getprec(char c) { switch(c) { case '+':return 1; case '-':return 1; case '*': case '/': case '%':return 2; default:return 999; } }Have fun.....
Related Posts
Postfix Expression Evaluation
Hey friends check this out: Post.cpp: #include<iostream.h> #include<conio.h> #include&...Read more
C/C++ Interview Questions : Advance
1. When can you tell that a memory leak will occur? Memory leaks generally occur when a program l...Read more
C/C++ Interview Questions : Headway
1. Name some pure object oriented languages. There are several object oriented programming langua...Read more
C/C++ Interview Questions : Base Cleaning
1. What is an incomplete type? Incomplete types refers to pointers in which there is non availabi...Read more
C/C++ Interview Questions : Basics
1. What is a modifier? A modifier, also called a modifying function is a member function that ch...Read more
Binary Tree Deletion
Hey Friends here is a simple C++ code for deleting a node from a binary tree. Deletion.cpp: #inclu...Read more
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.
Click to see the code!
To insert emoticon you must added at least one space before the code.