Postfix Notation Calculator

Nov 13, 2011 at 11:40pm
I was doing an exercise in the C Programming Language (exercise 5-10), where I have to create a reverse polish notation calculator. Everything is working except for multiplication, and I can't figure out why! Addition, subtraction and division work out, but the case for multiplication never actually gets executed for some reason, it jumps straight to the default. When I started printing out the value of c, it printed out a c as the last argument instead of a '*'. I'm assuming this c was from the first letter of the program name calc, so it's going to the first argument instead of the last or something weird. Here's my code:
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
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

void push(int n);
int  pop();

int main(int argc, char *argv[]) {
    if(argc == 1) {		/* if no arguments */
	printf("usage: expr numbers operators");
	return 0;
    }
    while(*++argv != NULL) {
	char c;
	int next = 0;
	while((c = *(*argv)++) != '\0') {
	    int temp;
	    switch(c) {
	    case '0':		/* if it was a number */
	    case '1':
	    case '2':
	    case '3':
	    case '4':
	    case '5':
	    case '6':
	    case '7':
	    case '8':
	    case '9':
		next *= 10;
		next += c - '0';
		break;
	    case '+':
		next = pop() + pop();
		break;
	    case '*':
		next = pop() * pop();
		break;
	    case '-':		
		temp = pop();	/* make sure its a-b not b-a */
		next = pop() - temp;
		break;
	    case '/':
		temp = pop();
		next = pop() / temp;
		break;
	    default:		/* otherwise an invalid character */
		printf("error: invalid character\n");
		return -1;
	    }
	}
	push(next);
    }
    printf("%d\n", pop());
    return 0;
} 

#define MAXLENGTH 100
static int stack[MAXLENGTH];
static int index;

void push(int n) {
    if(index < MAXLENGTH) {	/* if there was enough space */
	stack[index++] = n;
	return;
    }
    printf("error: stack full\n");
    return;
}

int pop() {
    if(index == 0) {		/* if no characters */
	printf("error, invalid syntax\n");
	exit(1);
    }
    return stack[--index];	/* delete and return the last element */
}


Thanks!
Last edited on Nov 14, 2011 at 3:33am
Nov 14, 2011 at 12:10am
I can't say. I just tested with the arguments "2 3 *" and it outputted 6, as it should. Is there an exact example you have that isn't working?
Nov 14, 2011 at 12:14am
Umm... thats weird. Whenever I run it it gives me an error. I tried running "2 3 *" and it said "error: invalid character." I changed the multiplication symbol to 'x', ran "2 3 x", and it worked perfectly! Strange stuff... might have something do with the fact I'm running linux.

http://imageshack.us/photo/my-images/51/failpe.png/
Last edited on Nov 14, 2011 at 12:23am
Nov 14, 2011 at 11:11pm
Honestly can anyone explain why this is happening? I'm really confused as to why changing the character used would fix the problem.
Nov 15, 2011 at 12:17am
The problem isn't with your code, it's because you're passing command-line arguments from the linux shell.

When you run the code with exe 2 3 *, your linux shell expands the '*' to be all the files in the current directory. So your code is probably seeing "2 3 file1 file2 ... filen".

What you could do is quote the *, so enter exe 2 3 "*", or similar. Then the actual '*' character will be passed in.
Nov 15, 2011 at 12:36am
Haha, didn't see that coming. So it's interpreting it as the operator that can fill in any character(s) in a file/directory name, that's kind of awesome and annoying at the same time. Good catch!
Topic archived. No new replies allowed.