Thread: evaluate postfix expressions read from infile

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    43

    evaluate postfix expressions read from infile

    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("&#37;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 - /

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What does "cannot convert strings op1 and op2" mean? You don't seem to have strings op1 and op2. Is this a compile-time error (and if so, what is it, actually), or a run-time error?

    Your indentation does not match your program logic (or at least, it better not) and you seem to have lost some braces somewhere, so that's something to consider. For one thing, you need to make sure in your isdigit case that you advance a far enough to get all the way past the number you read in.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    Exclamation

    num = atoi(&input_line[a]) I am not sure that you necessarily want to do it like that... though I dunno... Technically, its not going to cause you too much grief..

    You can always tell a new programmer by how they parse a string. If it takes them like 50 lines to separate a line filled only with numbers and math operators, instead of like 5 lines, they are a rookie...

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    well specifically the errors are:
    Code:
    Warning	1	warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.	c:\documents and settings\kmq\my documents\visual studio 2005\projects\stacklab\stacklab\stacklabcode.cpp	41
    Error	2	error C2664: 'push_stack' : cannot convert parameter 2 from 'int' to 'info_node *'	c:\documents and settings\kmq\my documents\visual studio 2005\projects\stacklab\stacklab\stacklabcode.cpp	59
    Error	5	error C2664: 'calc' : cannot convert parameter 1 from 'int *__w64 ' to 'int'	c:\documents and settings\kmq\my documents\visual studio 2005\projects\stacklab\stacklab\stacklabcode.cpp	75
    Error	3	error C2440: '=' : cannot convert from 'info_node *' to 'int'	c:\documents and settings\kmq\my documents\visual studio 2005\projects\stacklab\stacklab\stacklabcode.cpp	72
    Error	4	error C2440: '=' : cannot convert from 'info_node *' to 'int'	c:\documents and settings\kmq\my documents\visual studio 2005\projects\stacklab\stacklab\stacklabcode.cpp	73
    Error	6	error C2065: 'result' : undeclared identifier	c:\documents and settings\kmq\my documents\visual studio 2005\projects\stacklab\stacklab\stacklabcode.cpp	77

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't push a "just plain int" -- you have to push a node! So when you read a number, make a node, stuff the int into it, and push that. Same deal for popping.

    Although, to be honest, if 'twere me, I would change my push_stack and pop_stack to take/return ints and construct/deconstruct the nodes in the functions themselves.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As would I. And I didn't really look too much at your code, so as for the compiler errors.

    After your includes add this line:
    Code:
    #pragma warning(disable : 4996)
    Please only do that when me, tabstop, mats, Elysia, laserlight or Salem say its ok. Otherwise you are just going to ignore your way out of all kinds of stuff. In this case, you are simply telling Microsoft to shove it with their unportable functions.

    Do I really need to baby step you through the others? The errors say exactly what is wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  3. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 07:02 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM