0

op1 = c1 - '0'; op2 = c2 - '0';

where c1 and c2 is a charcter. let these two variables 1 , 2 in character. then, we c1 has value of 49, c2 value of 50. from ascii code, by this code, we have value op1 = 1; and op2 = 2; but in my case i have -47 and - 48. what is happen? edit // i'm sorry for verbose code. i thought , for problem solving all code needed. if i insert a expression 1+2+3 then it will convert to postfix. like (1 2 + 3 + ) and i'm trying to evaluate this postfix expr. by postfix_evaluation(). after this code i always get a -42 and testcode, at 1 tells me 2 , 1 and 3, -45

    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>
    #define MAX 50

    typedef struct stack
    {
        int data[MAX];
        int top;
    }stack;

    int precedence(char);
    void init(stack *);
    int empty(stack *);
    int full(stack *);
    int pop(stack *);
    void push(stack *, int);
    int top(stack *);
    void infix_to_postfix(char infix[], char postfix[]);
    int postfix_evaluation(char postfix[]);

    void main()
    {
        char infix[30], postfix[30];
        int x = 0;
        printf("expr?");
        gets(infix);
        infix_to_postfix(infix,postfix);
        x = postfix_evaluation(postfix);

        printf("%d is a value ", x);
        printf("postfix expr : %s", postfix);
    }
    int postfix_evaluation(char postfix[])
    {
        int i = strlen(postfix);
        int op1,op2,k, temp;
        int j;
        temp = 0;
        stack s;
        init(&s);
        char opr;
        for(j = 0; j < i; j++)
        {
            if(isalnum((int)postfix[j]))
            {
                push(&s, postfix[j]);
                printf("%c\n", postfix[j]);
            }
            else
            {
                op1 = pop(&s) - '0';
                op2 = pop(&s) - '0';
                opr = postfix[j];

                switch(opr)
                {
                case '+':
                    printf("%d , %d \n", op1 ,op2); -- 1
                    k = op1 + op2;
                    push(&s,k);
                    break;
                case '-':
                    push(&s,op2-op1);
                    break;
                case '*':
                    push(&s, op1*op2);
                    break;
                case '/':
                    push(&s, op2/op1);
                    break;
                }
            }
        }
        while(!empty(&s))
        {
            temp = temp + pop(&s);
        }
        return temp;

    }
garam
  • 1
  • 1

2 Answers2

2

Sometimes you push a character, but sometimes you push k. When you later pop the value from k you should not convert that again.

You should probably do push(&s, postfix[j] - '0'); and not convert when popping.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • I'm appreciate for your help. have a good day. and, could i ask about reason for not convert when popping? – garam Apr 25 '17 at 09:46
  • If you push both converted and unconverted values, you don't know which to convert when popping. That's why you end up with `-47` after converting twice. – Bo Persson Apr 25 '17 at 09:58
1

Use atoi to convert char to int.

int atoi(const char *str)

Example

int value = atoi("90");
printf("Value is %d\n",value);//Value is 90