Infix to Prefix And Postfix in C
By: Prachi in C Tutorials on 2009-03-21
This is a sample program that converts Infix to Prefix and Postfix in C.
#include <stdio.h> #include <conio.h> #include <string.h> #define MAX 15 #define true 1 #define false 0 /*Structure Decvlaration*/ typedef struct { char data[MAX]; char top; } STK; /*Function Declarations*/ void input(char str[]); void intopre(char str1[], char pre[]); void intopost(char str1[], char post[]); int isoperand(char sym); int prcd(char sym); void push(STK *s1, char elem); int pop(STK *s1); int empty(STK *s2); int full(STK *s2); void dis(char str[]); void main() { STK s; int cs, ans; char str[MAX], pre[MAX], post[MAX]; clrscr(); do /*Using Do-while Loop*/ { clrscr(); printf("-----Program for Expressions-----"); printf("Input The String:"); printf("MENU:"); printf("1.Infix to Prefix"); printf("2.Infix to Postfix"); printf("3.Exit"); cs = getche(); switch (cs) /*Using Switch Case*/ { case 1: intopre(str, pre); break; case 2: intopost(str, post); break; case 3: break; default: printf("Enter a Valid Choise!"); /*Default Case*/ break; } printf("Do you wish to Continue?(y/n)"); ans = getche(); } while (ans == 'y' || ans == 'Y'); /*Condition for Do-while loop*/ getch(); } /**************************************************/ /*To Input String*/ /**************************************************/ void input(char str) { printf("Enter the Infix String:"); scanf("%s", str); } /**************************************************/ /*To Covert Infix To Prefix*/ /**************************************************/ void intopre(STK s1, char str1[], char pre[]) { int len, flag; len = strlen(str1); int check = 0, cnt = len - 1, pos = 0; char elem; while (cnt >= 0) /*while condition*/ { flag = 0; if (isoperand(str1[cnt])) /*Checking for Operand*/ { printf("%c", str1[cnt]); cnt--; pos++; } else { check = prcd(str1[cnt]); while (check == false) { pre[pos] = str1[cnt]; flag = 1; pos++; cnt--; } if (flag == 0) { elem = pop(&s1); printf("%c", elem); } } } } /**************************************************/ /*To Convert Infix To Postfix*/ /**************************************************/ void intopost(STK s1, char str1[], char post[]) { int len; len = strlen(str1); int check = 0, cnt = len - 1, pos = 0; } /**************************************************/ /*To Check For Operand*/ /**************************************************/ int isoperand(char sym) { if ('A' < sym < 'Z' || 'a' < sym < 'z') return (true); return (false); } /**************************************************/ /*To Check The Precedence*/ /**************************************************/ int prcd(char sym) { } /**************************************************/ /*To Display String*/ /**************************************************/ void dis(char str[]) { } /******************************************/ /*Push Function Definition*/ /******************************************/ void push(STK *s1, char elem) { if (!full(s1)) { s1->top++; /*Incrementing top*/ s1->data[s1->top] = elem; /*Storing element*/ } else printf("Stack is Full!"); } /******************************************/ /*Full Function Definition*/ /******************************************/ int full(STK *s2) { if (s2->top == MAX) /*Condition for Full*/ return (true); return (false); } /******************************************/ /*Pop Function Definition*/ /******************************************/ int pop(STK *s1) { char elem; if (!empty(s1)) { elem = s1->data[s1->top]; /*Storing top stack element in elem*/ s1->top--; /*Decrementing top*/ return (elem); } return (false); } /******************************************/ /*Empty Function Definition*/ /******************************************/ int empty(STK *s2) { if (s2->top == -1) /*Condition For Empty*/ return (true); return (false); }
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Comments