My code takes in 1 command line argument which reads the command line character by character and places the stack accordingly.
command line argument of: "12+" should equal to equation of "1+2"
int pop(stack *p);
int main(int argc, char **argv)
{
stack ph;
int i, a, b;
int val = 0;
if (argc!=2)
{
printf("Usage: %s argument\n", argv[0]);
exit(1);
}
else{
int i;
int length = strlen(argv[1]);
int count;
initializedStack(&ph);
for(i=0;i<length;i++)
{
if (argv[1][i] == '+'){
a = pop(&ph);
printf("%d\n", a);
b = pop(&ph);
printf("%d\n", b);
val = a+b;
push(&ph,val);
}
else{
push(&ph, argv[1][i]);
}
}
printf("%d\n", pop(&ph));
}
return 0;
}
void initializedStack(stack *p){
p->top = 0;
}
void push(stack *p, int val){
p->top++;
p->items[p->top] = val;
}
int pop(stack *p){
int y;
y = p->items[p->top];
p->items[p->top] = 0;
(p->top)--;
return y;
}
I am currently in the testing stages of the program and it only includes the addition operation. To test this program out, I have print statements for the addition part of the if statements and pop at the end. Running this gives me the output of:
50
49
99
When the output should be:
1
2
3
It seems like the addition operation works, but I do not know where the 50 and 49 is coming from? What is correct way to write my code so that it provides the accurate output? Thanks!