100% found this document useful (1 vote)
7K views8 pages

Cs301 Assignment Solution

This document contains code for a C program that converts infix expressions to postfix expressions. It defines a struct called infix to store the expression data. It contains functions to initialize the struct, set the expression, push/pop operators to/from a stack, convert the expression, and display the result. The priority function determines operator precedence. The main function gets an infix expression, converts it to postfix, and displays the result.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
7K views8 pages

Cs301 Assignment Solution

This document contains code for a C program that converts infix expressions to postfix expressions. It defines a struct called infix to store the expression data. It contains functions to initialize the struct, set the expression, push/pop operators to/from a stack, convert the expression, and display the result. The priority function determines operator precedence. The main function gets an infix expression, converts it to postfix, and displays the result.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

http://vujannat.ning.

com/

Best Website To Help VuStudents

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>

#define MAX 50

struct infix

char target[MAX] ;

char stack[MAX] ;

char *s, *t ;

int top ;

};

void initinfix ( struct infix * ) ;

void setexpr ( struct infix *, char * ) ;

void push ( struct infix *, char ) ;

char pop ( struct infix * ) ;

void convert ( struct infix * ) ;

int priority ( char ) ;


http://vujannat.ning.com/

Best Website To Help VuStudents


http://vujannat.ning.com/

Best Website To Help VuStudents

void show ( struct infix ) ;

void main( )

struct infix p ;

char expr[MAX] ;

initinfix ( &p ) ;

clrscr( ) ;

printf ( "\nEnter an expression in infix form: " ) ;

gets ( expr ) ;

setexpr ( &p, expr ) ;

convert ( &p ) ;

printf ( "\nThe postfix expression is: " ) ;

show ( p ) ;

getch( ) ;

http://vujannat.ning.com/

Best Website To Help VuStudents


http://vujannat.ning.com/

Best Website To Help VuStudents

/* initializes structure elements */

void initinfix ( struct infix *p )

p -> top = -1 ;

strcpy ( p -> target, "" ) ;

strcpy ( p -> stack, "" ) ;

p -> t = p -> target ;

p -> s = "" ;

/* sets s to point to given expr. */

void setexpr ( struct infix *p, char *str )

p -> s = str ;

/* adds an operator to the stack */

void push ( struct infix *p, char c )

if ( p -> top == MAX )

printf ( "\nStack is full.\n" ) ;

else

{
http://vujannat.ning.com/

Best Website To Help VuStudents


http://vujannat.ning.com/

Best Website To Help VuStudents

p -> top++ ;

p -> stack[p -> top] = c ;

/* pops an operator from the stack */

char pop ( struct infix *p )

if ( p -> top == -1 )

printf ( "\nStack is empty.\n" ) ;

return -1 ;

else

char item = p -> stack[p -> top] ;

p -> top-- ;

return item ;

/* converts the given expr. from infix to postfix form */

void convert ( struct infix *p )


http://vujannat.ning.com/

Best Website To Help VuStudents


http://vujannat.ning.com/

Best Website To Help VuStudents

char opr ;

while ( *( p -> s ) )

if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )

p -> s++ ;

continue ;

if ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )

while ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )

*( p -> t ) = *( p -> s ) ;

p -> s++ ;

p -> t++ ;

if ( *( p -> s ) == '(' )

push ( p, *( p -> s ) ) ;

p -> s++ ;
http://vujannat.ning.com/

Best Website To Help VuStudents


http://vujannat.ning.com/

Best Website To Help VuStudents

if ( *( p -> s ) == '*' || *( p -> s ) == '+' || *( p -> s ) == '/' || *( p -> s ) == '%' || *( p -> s ) == '-' || *( p
-> s ) == '$' )

if ( p -> top != -1 )

opr = pop ( p ) ;

while ( priority ( opr ) >= priority ( *( p -> s ) ) )

*( p -> t ) = opr ;

p -> t++ ;

opr = pop ( p ) ;

push ( p, opr ) ;

push ( p, *( p -> s ) ) ;

else

push ( p, *( p -> s ) ) ;

p -> s++ ;

if ( *( p -> s ) == ')' )

http://vujannat.ning.com/

Best Website To Help VuStudents


http://vujannat.ning.com/

Best Website To Help VuStudents

opr = pop ( p ) ;

while ( ( opr ) != '(' )

*( p -> t ) = opr ;

p -> t++ ;

opr = pop ( p ) ;

p -> s++ ;

while ( p -> top != -1 )

char opr = pop ( p ) ;

*( p -> t ) = opr ;

p -> t++ ;

*( p -> t ) = '\0' ;

/* returns the priority of an operator */


http://vujannat.ning.com/

Best Website To Help VuStudents


http://vujannat.ning.com/

Best Website To Help VuStudents

int priority ( char c )

if ( c == '$' )

return 3 ;

if ( c == '*' || c == '/' || c == '%' )

return 2 ;

else

if ( c == '+' || c == '-' )

return 1 ;

else

return 0 ;

/* displays the postfix form of given expr. */

void show ( struct infix p )

printf ( " %s", p.target ) ;

http://vujannat.ning.com/

Best Website To Help VuStudents

You might also like