Home › Forums › C Programming › function › Reply To: function
September 10, 2008 at 12:22 pm
#3440
Participant
Heres a version of the infix to postfix conversion I found on the web. The implementation is pretty typical ( but could be simpler ). Ask if you have any questions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | /*************************************************************/<br /> /*************************************************************/<br /> /* ASSUMING INFIX EXPRESSION */<br /> /* IS VALID */<br /> /* !!!!!!!! */<br /> /*************************************************************/<br /> /*************************************************************/<br /> /* include necessary preprocessor header files */<br /> #include <stdio.h><br /> #include <stdlib.h><br /> #include <string.h><br /> #include <ctype.h><br /> /* constants */<br /> #define TRUE 1<br /> #define FALSE 0<br /> /* structure for stack */<br /> typedef struct<br /> {<br /> char data[20]; /* array to hold stack contents */<br /> int tos; /* top of the stack pointer */<br /> } STACK;<br /> <br /> /* function prototypes */<br /> void initStack(STACK *stack);<br /> void get_infix(char infix[]);<br /> void convertToPostfix(char infix[], char postfix[]);<br /> int isOperator(char c);<br /> int precedence(char operator1, char operator2);<br /> int pred_level(char ch);<br /> void push(STACK *stack, char value);<br /> char pop(STACK *stack);<br /> char stackTop(STACK *stack);<br /> int isEmpty(STACK *stack);<br /> int isFull(STACK *stack);<br /> void printResult(char infix[], char postfix[]);<br /> void print_msg(void);<br /> /* program entry point */<br /> int main(void)<br /> {<br /> char infix[20], postfix[20]="";<br /> /* convert from infix to postfix main function */<br /> convertToPostfix(infix, postfix);<br /> /* display the postfix equivalent */<br /> infix[strlen(infix)-2] = ' ';<br /> printResult(infix, postfix);<br /> return EXIT_SUCCESS;<br /> }<br /> /* initalise the stack */<br /> void initStack(STACK *stack)<br /> {<br /> stack->tos = -1; /* stack is initially empty */<br /> }<br /> /* get infix expression from user */<br /> void get_infix(char infix[])<br /> {<br /> int i;<br /> printf("Enter infix expression below (max 18 characters excluding spaces) : n");<br /> fflush(stdin);<br /> /* to read in only 18 characters excluding spaces */<br /> for ( i=0; i<18; )<br /> {<br /> if ( (infix = getchar()) == 'n' )<br /> {<br /> i++;<br /> break;<br /> }<br /> else if ( !(isspace(infix)) )<br /> i++;<br /> }<br /> infix = ' ';<br /> }<br /> /* convert the infix expression to postfix notation */<br /> void convertToPostfix(char infix[], char postfix[])<br /> {<br /> int i, length;<br /> int j=0;<br /> char tos_ch;<br /> STACK stack;<br /> initStack(&stack); /* initialise stack */<br /> get_infix(infix); /* get infix expression from user */<br /> length = strlen(infix);<br /> /* if strlen if infix is more than zero */<br /> if ( length )<br /> { <br /> push(&stack, '(');<br /> strcat(infix, ")");<br /> length++;<br /> <br /> for ( i=0; i<length; i++ )<br /> {<br /> /* if current operator in infix is digit */<br /> if ( isdigit(infix) )<br /> {<br /> postfix[j++] = infix;<br /> }<br /> /* if current operator in infix is left parenthesis */<br /> else if ( infix == '(' )<br /> {<br /> push(&stack, '(');<br /> }<br /> /* if current operator in infix is operator */<br /> else if ( isOperator(infix) )<br /> {<br /> while ( TRUE )<br /> {<br /> /* get tos */<br /> tos_ch = stackTop(&stack);<br /> /* no stack left */<br /> if ( tos_ch == ' ' )<br /> {<br /> printf("nInvalid infix expressionn");<br /> print_msg();<br /> exit(1);<br /> }<br /> else<br /> {<br /> if ( isOperator(tos_ch) )<br /> {<br /> if ( pred_level(tos_ch) >= pred_level(infix) )<br /> postfix[j++] = pop(&stack);<br /> else<br /> break;<br /> }<br /> else<br /> break;<br /> }<br /> }<br /> push(&stack, infix);<br /> }<br /> /* if current operator in infix is right parenthesis */<br /> else if ( infix == ')' )<br /> {<br /> while ( TRUE )<br /> {<br /> /* get tos */<br /> tos_ch = stackTop(&stack);<br /> /* no stack left */<br /> if ( tos_ch == ' ' )<br /> {<br /> printf("nInvalid infix expressionn");<br /> print_msg();<br /> exit(1);<br /> }<br /> else<br /> {<br /> if ( tos_ch != '(' )<br /> {<br /> postfix[j++] = tos_ch;<br /> pop(&stack);<br /> }<br /> else<br /> {<br /> pop(&stack);<br /> break;<br /> }<br /> }<br /> }<br /> continue;<br /> }<br /> }<br /> }<br /> postfix[j] = ' ';<br /> }<br /> /* determine if c is an operator */<br /> int isOperator(char c)<br /> {<br /> if ( c == '+' || c == '-' || c == '*' ||<br /> c == '/' || c == '%' || c == '^' )<br /> {<br /> return TRUE;<br /> }<br /> else<br /> return FALSE;<br /> }<br /> /* determine precedence level */<br /> int pred_level(char ch)<br /> {<br /> if ( ch == '+' || ch == '-' )<br /> return 1;<br /> else if ( ch == '^' )<br /> return 3;<br /> else<br /> return 2;<br /> }<br /> /* determine if the precedence of operator1 is less than,<br /> equal to, greater than the precedence of operator2 */<br /> int precedence(char operator1, char operator2)<br /> {<br /> if ( pred_level(operator1) > pred_level(operator2) )<br /> return 1;<br /> else if ( pred_level(operator1) < pred_level(operator2) )<br /> return -1;<br /> else<br /> return 0;<br /> }<br /> /* push a value on the stack */<br /> void push(STACK *stack, char value)<br /> {<br /> if ( !(isFull(stack)) )<br /> {<br /> (stack->tos)++;<br /> stack->data[stack->tos] = value;<br /> }<br /> }<br /> /* pop a value off the stack */<br /> char pop(STACK *stack)<br /> {<br /> char ch;<br /> if ( !(isEmpty(stack)) )<br /> {<br /> ch = stack->data[stack->tos];<br /> (stack->tos)--;<br /> return ch;<br /> }<br /> else<br /> return ' ';<br /> }<br /> /* return the top value of the stack without popping the stack */<br /> char stackTop(STACK *stack)<br /> {<br /> if ( !(isEmpty(stack)) )<br /> return stack->data[stack->tos];<br /> else<br /> return ' ';<br /> }<br /> /* determine if stack is empty */<br /> int isEmpty(STACK *stack)<br /> {<br /> /* empty */<br /> if ( stack->tos == -1 )<br /> return TRUE;<br /> /* not empty */<br /> else<br /> return FALSE;<br /> }<br /> /* determine if stack is full */<br /> int isFull(STACK *stack)<br /> {<br /> /* full */<br /> if ( stack->tos == 19 )<br /> return TRUE;<br /> /* not full */<br /> else<br /> return FALSE;<br /> }<br /> /* display the result postfix expression */<br /> void printResult(char infix[], char postfix[])<br /> {<br /> /*system("cls");*/<br /> printf("nn");<br /> printf("Infix notation : %sn", infix);<br /> printf("Postfix notation: %snn", postfix);<br /> print_msg();<br /> }<br /> /* print exit message */<br /> void print_msg(void)<br /> {<br /> printf("Hit <return> to exit......");<br /> fflush(stdin);<br /> getchar();<br /> }</return> |