0

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!

1 Answers1

1

When you do:

push(&ph, argv[1][i]);

You are pushing the ASCII value for a given digit and not its decoded numeric value [the latter is equivalent to what atoi would return, if it could operate on a single character].

This is probably not what you, given that later, you push a + b, which are numeric/binary values.

Although this works for a single digit only, the quick fix is:

push(&ph, argv[1][i] - '0');

Otherwise, in general, you'll need to assemble the entire digit string and decode it with (e.g.) atoi.

In that case, you'd need to handle some whitespace for something 12 23 +


Here is a cleaned up version that uses strtok and atoi to allow more general numbers. [please pardon the gratuitous style cleanup]:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int top;
    int items[100];
} stack;

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;
}

int
main(int argc, char **argv)
{
    stack ph;
    int i,
     a,
     b;
    int val = 0;
    char *buf;
    char *token;
    int chr;

    if (argc != 2) {
        printf("Usage: %s argument\n", argv[0]);
        exit(1);
    }

    buf = argv[1];

    initializedStack(&ph);

    while (1) {
        token = strtok(buf," ");
        if (token == NULL)
            break;
        buf = NULL;

        chr = token[0];

        if (strcmp(token,"+") == 0) {
            a = pop(&ph);
            printf("%d\n", a);

            b = pop(&ph);
            printf("%d\n", b);

            val = a + b;
            push(&ph, val);
            continue;
        }

        if ((chr >= '0') && (chr <= '9')) {
            val = atoi(token);
            push(&ph, val);
            continue;
        }
    }

    printf("%d\n", pop(&ph));

    return 0;
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48