hello im pretty sure im on the right track but i get errors in my code that cannot convert strings op1 and op2 and such can anyone help?
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 calc(int, int, int) ; int main() { FILE* infile ; int a = 0 ; int num = 0 ; char input_line[31] ; struct nstack even_Stack ; struct info_node* pnew_node = NULL ; struct info_node* ppop_node = NULL ; create_stack(&even_Stack) ; infile = fopen("express.txt", "r") ; while (!feof(infile) ) { fgets(input_line, 31, infile) ; // printf("%s \n", input_line) ; a = 0 ; while(input_line[a] != '\0') { if( isspace(input_line[a])) printf(" SPACE ") ; else if( isdigit(input_line[a] )) { num = atoi(&input_line[a]) ; //converts from character to integer push_stack(&even_Stack, num) ; //push to stack printf("%d ", num ) ; } else int result = 0 ; //must be operater pop twice and push to stack printf("%c", input_line[a]) ; num = input_line[a] ; int op1; int op2; op1 = pop_stack(&even_Stack) ; op2 = pop_stack(&even_Stack) ; calc(&op1, &num, &op2) ; push_stack(&even_Stack, result) ; result = pop_stack(&even_Stack); printf("%d ", result) ; a++; } printf("\n") ; } fclose(infile) ; return (0) ; } //end of function main int calc(int op1, int oper, int op2) { int result = 0 ; switch(oper) { case '+' : result = op1 + op2 ; break; case '-' : result = op1 - op2 ; break; case '*' : result = op1 * op2 ; break; case '/' : result = op1 / op2 ; break; } return(result) ; } //end of func calc() 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
also here is the numbers from the infile:
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


