hello..my goal here is to take the stack program that i already have and use it to evaluate postfix expressions from a file. I need help figuring out how to modify the stack to work with integers and evaluate the expressions. Any suggestions?
Code:#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define TRUE 1 #define FALSE 0 struct info_node { int weight ; struct info_node* next ; } ; struct nstack { struct info_node* top ; } ; void create_stack(struct nstack*) ; int empty_stack(struct nstack*) ; void push_stack(struct nstack* , struct info_node*) ; struct info_node* pop_stack( struct nstack* ) ; int main() { FILE* infile ; int a = 0 ; int num = 0 ; struct nstack even_Stack ; struct nstack odd_Stack ; struct info_node* pnew_node = NULL ; struct info_node* ppop_node = NULL ; create_stack(&even_Stack) ; create_stack(&odd_Stack) ; infile = fopen("express.txt", "r") ; while (!feof( infile ) ) { a++ ; fscanf(infile, "%d" , &num) ; pnew_node = (struct info_node* ) malloc(sizeof(info_node) ) ; pnew_node->weight = num ; pnew_node->next = NULL ; if( (num % 2) == 0 ) push_stack(&even_Stack, pnew_node) ; else push_stack(&odd_Stack, pnew_node) ; } while( !empty_stack(&even_Stack) ) { ppop_node = pop_stack(&even_Stack) ; num = ppop_node-> weight ; printf("%d ", num ) ; a++; if( (a % 20) == 0 ) printf("\n") ; } printf("\n\n") ; a = 0 ; while( !empty_stack(&odd_Stack) ) { ppop_node = pop_stack(&odd_Stack) ; num = ppop_node-> weight ; printf("%d ", num ) ; a++; if( (a % 20) == 0 ) printf("\n") ; } fclose(infile) ; return (0) ; } //end of func main void create_stack(struct nstack* pnstack) { pnstack->top = NULL ; return ; } //end of func create_stack int empty_stack(struct nstack* pnstack) { if (pnstack->top == NULL) return (TRUE) ; else return (FALSE) ; } //end of func empty_stack void push_stack(struct nstack* pnstack, struct info_node* pnnode) { if(empty_stack(pnstack)) { pnstack->top = pnnode ; pnnode->next = NULL ; } else { pnnode->next = pnstack->top ; pnstack->top = pnnode ; } return ; } //end of func push_stack struct info_node* pop_stack( struct nstack* pnstack ) { struct info_node* prnode = NULL ; if(empty_stack(pnstack) ) printf("ERROR: STACK IS EMPTY!!! \n") ; else { prnode = pnstack->top ; pnstack->top = pnstack->top->next ; prnode->next = NULL ; } return( prnode ) ; } // end of func pop_stack int calc(int operand1, int oper, int operand2) { //Local Declaration int result = 0 ; //Statements switch(oper) { case '+' : result = operand1 + operand2; break ; case '-' : result = operand1 - operand2; break ; case '*' : result = operand1 * operand2; break ; case '/' : result = operand1 / operand2; break; } //end of switch(oper) return (result) ; } //end of function calc()
Also these are the expressions i am supposed to read in from a file(express.txt):
1 2 3 * 4 / +
1 2 3 4 / * +
1 2 + 3 * 4 /
1 2 + 4 3 / *
1 2 3 * + 4 /
4 3 / 1 2 + *
6 7 8 / 9 * -
6 7 8 9 * / -
6 7 - 8 / 9 *
6 7 - 9 8 * /
6 7 8 / - 9 *
8 9 * 6 7 - /



LinkBack URL
About LinkBacks



