0

my code is relatively simple in what it aims to do, it takes in command line arguments and places the stack accordingly.

command line argument: "2 2 +"

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

  typedef struct stack {

     int top;
     int items[100];

  } stack;

  void initializeStack(stack* p);
  void push(stack* p, int val);
  int pop(stack* p);

  int main(int argc, char** argv) {

      int i, a, b;
      int val = 0;
      stack ph;
      initializeStack(&ph);

      for(i=1; i<argc; i++) {
          if(strcmp(argv[i], "*") == 0) {
              a = pop(&ph);
              b = pop(&ph);
              val = a*b;
              push(&ph, val);
          }

          else if(strcmp(argv[i], "/") == 0) {
              a = pop(&ph);
              b = pop(&ph);
              val = b/a;
              push(&ph, val);
          }

          else if(strcmp(argv[i], "+") == 0) {
              a = pop(&ph);
              printf("%d\n", a);
              b = pop(&ph);
              printf("%d\n", b);
              val = a+b;
              push(&ph, val);
          }

          else if(strcmp(argv[i], "-") == 0) {
              a = pop(&ph);
              b = pop(&ph);
              val = b-a;
              push(&ph, val);
          }

          else if(strcmp(argv[i], "^") == 0) {
              a = pop(&ph);
              b = pop(&ph);
              val = pow(b,a);
              push(&ph, val);
          }

          else if(strcmp(argv[i], "%") == 0) {
              a = pop(&ph);
              b = pop(&ph);
              val = b%a;
              push(&ph, val);
          }

          else {
              push(&ph, argv[i]);
          }
      }

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

      return 0;
  }

  void initializeStack(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 have put in printf statements in the my else if statement for addition as well as the pop at the end but if I run I get this output:

6956340 6956337 13912677

which leads me to believe it's printing memory addresses instead of the actual values that are passed in.

what would be the correct way to write my code so that if "2 2 +" was my code argument, the answer would be 4, or my current printf statements would become "2 2 4"?

Deleted User
  • 2,551
  • 1
  • 11
  • 18
LatentDenis
  • 2,839
  • 12
  • 48
  • 99
  • First step: **Compile with warnings enabled.** This cannot be emphasized enough. The compiler will tell you that you're sticking `char*` into `int`. – Jim Balter Jul 01 '14 at 07:07

3 Answers3

1

argv[i] is a string (char*) not an int, but you pass it to push() as if it is an int.

You need to convert it to an int (perhaps via atoi()) before pushing it on to the stack. Or you could change push to take a string and convert it to an int in there.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • thanks!! that worked. but there's one test that I pass through which isn't making much sense to me. if I pass "2 4 ^ 2 * 5 % 2 -", it's supposed to produce 0, instead I get -2. any idea on why this is? – LatentDenis Jul 01 '14 at 07:22
1

argv[i] is const char *. You push(&ph, argv[i]); Use atoi to convert to int.

Ivan Ivanov
  • 2,076
  • 16
  • 33
  • thanks!! that worked. but there's one test that I pass through which isn't making much sense to me. if I pass "2 4 ^ 2 * 5 % 2 -", it's supposed to produce 0, instead I get -2. any idea on why this is? – LatentDenis Jul 01 '14 at 07:23
1

You are right. Pusing pushing adresses of the argv[] strings to the stack by push(&ph, argv[i]. You should push(&ph, atoi(argv[i]) instead.

Jakub
  • 583
  • 3
  • 8
  • thanks!! that worked. but there's one test that I pass through which isn't making much sense to me. if I pass "2 4 ^ 2 * 5 % 2 -", it's supposed to produce 0, instead I get -2. any idea on why this is? – LatentDenis Jul 01 '14 at 07:23
  • Do you really want to calculate a bitwise XOR of 2 and 4 in the first operation? – Jakub Jul 01 '14 at 07:43
  • I thought it was 4^2, =16, 16*2=32, 32%5=2, 2-2=0? – LatentDenis Jul 01 '14 at 07:56
  • Sorry, not able to pay full attention to this conversation. But this will help: http://stackoverflow.com/questions/4843304/why-is-my-power-operator-not-working – Jakub Jul 01 '14 at 08:05