title: Evaluating a postfix expression using expression tree in C
body: <p>I can't seem to get this program to work, it is a doubly linked list representation of expression tree. After creating the tree I need to evaluate the result but I can't seem to figure out how.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;math.h&gt;
  typedef struct node
  {
      char item;
      struct node * left;
      struct node * right;
  }*Btree;
  
  Btree root;
  void operandFunc(char);
  void operatorFunc(char);
  void push(Btree);
  Btree pop();
  void infix(Btree);
  void postfix(Btree);
  void prefix(Btree);
  int solve(Btree);
  int calculate(char,int,int);
  int isOperand(char); 
  char expression[25];
  Btree stack[25];
  int stackPtr = -1;
  
  int main()
  {
      int count = 0;
      printf("Please enter a postfix expression\n");
      while((expression[count++]=getchar())!='\n');
      expression[--count] = '\0';
      //puts(expression);
  
      for(count = 0;expression[count]!='\0';count++)
      {
          switch(expression[count])
          {
              case '+':
              case '-':
              case '*':
              case '/':
              case '^':
              case '%':
              case '$':
                  operatorFunc(expression[count]);
                  break;
              default:
                  operandFunc(expression[count]);
          }
      }
      if(stackPtr != 0)
      {
          printf("Incomplete / Incorrect postfix expression given!\n");
      }
      else
      {
          printf("\n\nThe result = %d",solve(stack[stackPtr])+'0');
          printf("\n\n");
          return 0;
      }
  }
  
  void prefix(Btree root)
  {
      Btree temp = root;
      if(temp)
      {
          printf("%c ",temp-&gt;item);
          prefix(temp-&gt;left);
          prefix(temp-&gt;right);        
      }
  }
  
  void infix(Btree root)
  {
      Btree temp = root;
      if(temp != NULL)
      {
          infix(temp-&gt;left);
          printf("%c ",temp-&gt;item);       
          infix(temp-&gt;right);     
      }
  }
  
  void postfix(Btree root)
  {
      Btree temp = root;
      if(temp)
      {
          postfix(temp-&gt;left);
          postfix(temp-&gt;right);       
          printf("%c ",temp-&gt;item);       
      }
  }
  
  void push(Btree root)
  {
      stack[++stackPtr] = root;
  }
  
  Btree pop()
  {
      return (stack[stackPtr--]);
  }
  
  void operandFunc(char var)
  {
      Btree root = (Btree)malloc(sizeof(struct node));
      root-&gt;item= var;
      root-&gt;left= NULL;
      root-&gt;right= NULL;
      push(root);
  }
  
  void operatorFunc(char var)
  {
      Btree root = (Btree)malloc(sizeof(struct node));
      root-&gt;item = var;
      root-&gt;right = pop();
      root-&gt;left = pop();
      push(root);
  }
  
  int solve(Btree root)
  {
      Btree temp = root;
      char num1,num2;
      char operator;
      int result;
      if(temp)
      {
          Btree LEFTP = temp-&gt;left;
          Btree RIGHTP = temp-&gt;right; 
  
          if(LEFTP)
          {
              if(isOperand(LEFTP-&gt;item))
              {
                  num1 = LEFTP-&gt;item;
              }   
              else
              {
                  num1 = solve(LEFTP);
              }
          }
  
          if(RIGHTP)
          {
              if(isOperand(RIGHTP-&gt;item))
              {
                  num2 = RIGHTP-&gt;item;
              }   
              else
              {
                  num2 = solve(RIGHTP);
              }
          }
  
          operator = temp-&gt;item;
          printf("Test 1 = %c, num1 = %c, num2 = %c\n",operator,num1,num2);
          result = calculate(operator,num1-'0',num2-'0');
          printf("Test Result = %d\n",result);
          temp-&gt;item = (result+'0');
          printf("Root Item = %c and %d\n",temp-&gt;item,temp-&gt;item);
          return result;
      }
  
      return NULL;
  }
  
  int calculate(char operator,int op1,int op2)
  {
      printf("Operator = %c , num1 = %d, num2 = %d\n",operator,op1,op2);
      switch(operator)
      {
          case '+':   return(op1+op2);
                      break;
          case '-':   return(op1-op2);
                      break;
          case '*':   return(op1*op2);
                      break;
          case '/':   return(op1/op2);
                      break;
          case '%':   return(op1%op2);
                      break;
          case '$':   return pow(op1,op2);
                      break;
          default:    printf("\n illegal operation.");
                      exit;
      }
  }
  
  int isOperand(char var)
  {
      switch(var)
      {
          case '+': 
          case '-':
          case '*':
          case '/':
          case '$':
          case '%':
                      return 0;
          default:
                      return 1;
      }
  }
  </code></pre>
  
  <p>I'm having trouble converting and returning the characters as integers.</p>
  
  <p>UPDATE 1: I was able to make it solve single digit number inputs to recieve a multi digit result. I'm still working on a new data structure to compute multi digit numbers. Here's the updated code below.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;math.h&gt;
  typedef struct node
  {
      char item;
      struct node * left;
      struct node * right;
  }*Btree;
  
  Btree root;
  void operandFunc(char);
  void operatorFunc(char);
  void push(Btree);
  Btree pop();
  void infix(Btree);
  void postfix(Btree);
  void prefix(Btree);
  int solve(Btree);
  int calculate(char,int,int);
  int isOperand(char); 
  char expression[25];
  Btree stack[25];
  int stackPtr = -1;
  
  int main()
  {
      int count = 0;
      printf("Please enter a postfix expression\n");
      while((expression[count++]=getchar())!='\n');
      expression[--count] = '\0';
      //puts(expression);
  
      for(count = 0;expression[count]!='\0';count++)
      {
          switch(expression[count])
          {
              case '+':
              case '-':
              case '*':
              case '/':
              case '^':
              case '%':
              case '$':
                  operatorFunc(expression[count]);
                  break;
              default:
                  operandFunc(expression[count]);
          }
      }
      if(stackPtr != 0)
      {
          printf("Incomplete / Incorrect postfix expression given!\n");
      }
      else
      {
          printf("\n\nThe result = %d",solve(stack[stackPtr])-'0');
          printf("\n\n");
          return 0;
      }
  }
  
  void prefix(Btree root)
  {
      Btree temp = root;
      if(temp)
      {
          printf("%c ",temp-&gt;item);
          prefix(temp-&gt;left);
          prefix(temp-&gt;right);        
      }
  }
  
  void infix(Btree root)
  {
      Btree temp = root;
      if(temp != NULL)
      {
          infix(temp-&gt;left);
          printf("%c ",temp-&gt;item);       
          infix(temp-&gt;right);     
      }
  }
  
  void postfix(Btree root)
  {
      Btree temp = root;
      if(temp)
      {
          postfix(temp-&gt;left);
          postfix(temp-&gt;right);       
          printf("%c ",temp-&gt;item);       
      }
  }
  
  void push(Btree root)
  {
      stack[++stackPtr] = root;
  }
  
  Btree pop()
  {
      return (stack[stackPtr--]);
  }
  
  void operandFunc(char var)
  {
      Btree root = (Btree)malloc(sizeof(struct node));
      root-&gt;item= var;
      root-&gt;left= NULL;
      root-&gt;right= NULL;
      push(root);
  }
  
  void operatorFunc(char var)
  {
      Btree root = (Btree)malloc(sizeof(struct node));
      root-&gt;item = var;
      root-&gt;right = pop();
      root-&gt;left = pop();
      push(root);
  }
  
  int solve(Btree root)
  {
      Btree temp = root;
      char num1,num2;
      char operator;
      int result;
      if(temp)
      {
          Btree LEFTP = temp-&gt;left;
          Btree RIGHTP = temp-&gt;right; 
  
          if(LEFTP)
          {
              if(isOperand(LEFTP-&gt;item))
              {
                  num1 = LEFTP-&gt;item;
              }   
              else
              {
                  num1 = solve(LEFTP);
              }
          }
  
          if(RIGHTP)
          {
              if(isOperand(RIGHTP-&gt;item))
              {
                  num2 = RIGHTP-&gt;item;
              }   
              else
              {
                  num2 = solve(RIGHTP);
              }
          }
  
          operator = temp-&gt;item;
          printf("Test 1 = %c, num1 = %c, num2 = %c\n",operator,num1,num2);
          result = calculate(operator,num1-'0',num2-'0');
          printf("Test Result = %d\n",result);
          temp-&gt;item = (result+'0');
          printf("Root Item = %c and %d\n",temp-&gt;item,temp-&gt;item);
          return root-&gt;item;
      }
  
      return NULL;
  }
  
  int calculate(char operator,int op1,int op2)
  {
      printf("Operator = %c , num1 = %d, num2 = %d\n",operator,op1,op2);
      switch(operator)
      {
          case '+':   return(op1+op2);
                      break;
          case '-':   return(op1-op2);
                      break;
          case '*':   return(op1*op2);
                      break;
          case '/':   return(op1/op2);
                      break;
          case '%':   return(op1%op2);
                      break;
          case '$':   return pow(op1,op2);
                      break;
          default:    printf("\n illegal operation.");
                      exit;
      }
  }
  
  int isOperand(char var)
  {
      switch(var)
      {
          case '+': 
          case '-':
          case '*':
          case '/':
          case '$':
          case '%':
                      return 0;
          default:
                      return 1;
      }
  }
  </code></pre>