Search
 
SCRIPT & CODE EXAMPLE
 

CPP

infix to prefix using cpp linked list program

#include <stdio.h>#include <conio.h>#include <string.h>#include <ctype.h>#define MAX 50struct infix{	char target[MAX] ;	char stack[MAX] ;	char *s, *t ;	int top, l ;} ; 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 c ) ;void show ( struct infix ) ; void main( ){	struct infix q ;	char expr[MAX] ;	clrscr( ) ;	initinfix ( &q ) ;	printf ( "
Enter an expression in infix form: " ) ;	gets ( expr ) ;	setexpr ( &q, expr ) ;	convert ( &q ) ;	printf ( "The Prefix expression is: " ) ;	show ( q ) ;	getch( ) ;}/* initializes elements of structure variable */void initinfix ( struct infix *pq ){	pq -> top = -1 ;	strcpy ( pq -> target, "" ) ;	strcpy ( pq -> stack, "" ) ;	pq -> l = 0 ;}/* reverses the given expression */void setexpr ( struct infix *pq, char *str ){	pq -> s = str ;	strrev ( pq -> s ) ;	pq -> l = strlen ( pq -> s ) ;	*( pq -> target + pq -> l ) = '' ;	pq -> t = pq -> target + ( pq -> l - 1 ) ;}/* adds operator to the stack */void push ( struct infix *pq, char c ){	if ( pq -> top == MAX - 1 )		printf ( "
Stack is full.
" ) ;	else	{		pq -> top++ ;		pq -> stack[pq -> top] = c ;	}}/* pops an operator from the stack */char pop ( struct infix *pq ){	if ( pq -> top == -1 )	{		printf ( "Stack is empty
" ) ;		return -1 ;	}	else	{		char item = pq -> stack[pq -> top] ;		pq -> top-- ;		return item ;	}}/* converts the infix expr. to prefix form */void convert ( struct infix *pq ){	char opr ;	while ( *( pq -> s ) )	{		if ( *( pq -> s ) == ' ' || *( pq -> s ) == '	' )		{			pq -> s++ ;			continue ;		}		if ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )		{			while ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )			{				*( pq -> t ) = *( pq -> s ) ;				pq -> s++ ;				pq -> t-- ;			}		}		if ( *( pq -> s ) == ')' )		{			push ( pq, *( pq -> s ) ) ;			pq -> s++ ;		}		if ( *( pq -> s ) == '*' || *( pq -> s ) == '+' || *( pq -> s ) == '/' || *( pq -> s ) == '%' || *( pq -> s ) == '-' || *( pq -> s ) == '$' )		{			if ( pq -> top != -1 )			{				opr = pop ( pq ) ;				while ( priority ( opr ) > priority ( *( pq -> s ) ) )				{					*( pq -> t ) = opr ;					pq -> t-- ;					opr = pop ( pq ) ;				}				push ( pq, opr ) ;				push ( pq, *( pq -> s ) ) ;			}			else				push ( pq, *( pq -> s ) ) ;				pq -> s++ ;		}		if ( *( pq -> s ) == '(' )		{			opr = pop ( pq ) ;			while ( opr != ')' )			{				*( pq -> t ) = opr ;				pq -> t-- ;				opr = pop ( pq ) ;			}			pq -> s++ ;		}	}	while ( pq -> top != -1 )	{		opr = pop ( pq ) ;		*( pq -> t ) = opr ;		pq -> t-- ;	}	pq -> t++ ;}/* returns the priotity of the operator */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 prefix form of given expr. */void show ( struct infix pq ){	while ( *( pq.t ) )	{		printf ( " %c", *( pq.t ) ) ;		pq.t++ ;	}}
Comment

PREVIOUS NEXT
Code Example
Cpp :: vector.rbegin() 
Cpp :: namespace c++ 
Cpp :: convert c++ program to c online 
Cpp :: how to store array of string with spaces in c++ stl 
Cpp :: C++ Enumeration Type 
Cpp :: how to read qlistwidget in c++ 
Cpp :: total sales in array c++ two dimensional array 
Cpp :: how to make a goto area in c++ 
Cpp :: return value optimization example 
Cpp :: initializer before void c++ 
Cpp :: Integer Literrals in C++ Programming 
Cpp :: c++ suare 
Cpp :: how to declare a function in c++ header file 
Cpp :: c++ install 
Cpp :: c++ sudoku solver 
Cpp :: if statement in c++ 
Cpp :: convert from hex to decimal c++ 
Cpp :: c vs c++ vs c# 
Cpp :: how to define range of numbers inside a if condition in c++ 
Cpp :: how to find maximum value in c++ 
C :: powershell search files for string 
C :: manifest orientation portrait 
C :: div en langage c 
C :: tainted love 
C :: prime numbers c 
C :: multiplication of two matrix in c 
C :: c printf uint32_t 
C :: how to create calculator with switch in c 
C :: c read csv 
C :: PATH_MAX 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =