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