Thread: problem of reading Postfix Evaluation

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    problem of reading Postfix Evaluation

    For example, 1 2 3 * + 4 -. A space between two data(operand or operator).
    How to read it step by step and save as a proper parameter? for example, I declare two variable `int operand; char operator`. If it is scanned a data(operand or operator), how to save this data to a proper variable(operand or operator variable)? Example below,
    Code:
    1 2 3 * + 4 -
    (Now It is read "1", save as operand)
    ....
    (Now It is read "*", save as operator)
    By this method, I can pop or push the data properly. How to implement it?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read everything as a string
    - if it looks like an int, push it onto the stack
    - it it looks like an operator, pop the required number of operands and push the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    can I use strtok to read it easily? or other method?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use fgets() to read the line.

    Use strtok() / sscanf() / strchr() / strspn() or any other method you like to break the string into meaningful tokens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    thank Salem. I have other problem about passing by value of variable "operator" at function "void operation(char operator)". The output is very strange. It cannot display "+", "-" or "*" properly. What's wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define DELIM " "
    #define MAXWORD 100
    #define MAXLEN 10
    #define MAX_PS 1000
    
    int postStack[MAX_PS];
    int top;
    
    void push(int x)
    {
    	top++;
    	postStack[top] = x;
    }
    
    int pop()
    {
    	if (top >= 0) {
    		top--;
    		return postStack[top+1];
    	}
    }
    
    void initStack()
    { top = -1; }
    
    void operation(char operator)
    {
    	int a, b, result;
    	a = pop();
    	b = pop();
    	if (operator == '+')
    		result = a+b;
    	else if (operator == '-')
    		result = a-b;
    	else if (operator == '*')
    		result = a*b;
    	push(result);
    	printf("%d %c %d = %d\n", a, b, operator, result);
    }
    
    int main()
    {
    	char words[MAXWORD][MAXLEN];
    	char buff[BUFSIZ];
    	int n, i;
    	initStack();
    	n = 0;
    	if (fgets(buff, sizeof buff, stdin) != NULL) {
    		char *sep = strtok(buff, DELIM);
    		while (sep != NULL) {
    			strcpy(words[n++], sep);
    			sep = strtok(NULL, DELIM);
    		}
    	}
    
    	for (i = 0; i < n; i++) {
    		if ((words[i][0] != '+') && (words[i][0] != '-') && (words[i][0] != '*'))
    			push(atoi(words[i]));
    		else operation(words[i][0]);
    	}
    
    	return 0;
    }
    data input/output
    Code:
    input:
    6 4 * 2 +
    output:
    4  42 = 0
    2 ▒ 43 = 0
    Last edited by Mathsniper; 12-15-2006 at 07:57 AM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    printf("%d %c %d = %d\n", a, b, operator, result);
    should be

    printf("%d %c %d = %d\n", a, operator, b , result);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    change your operator variable name to something else something like v_operator. operator is key word in c++. Even through u wont get any error message when it is saved as .c ext. Would,'t be recommended variable name

    ssharish2005

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    thanks ssharish2005, I haven't throught that it will be crashed with C++ variaible name of operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  2. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 09:55 AM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. file reading problem
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2005, 08:58 AM