albert albert

albert albert

  • NA
  • 503
  • 0

InfixToPostfix

Oct 19 2011 6:28 AM
Hi everyone,

I try to make an infix postfix converter with the following algorithm:

(6 + 2) * 5 -8 / 4 and then as postfix notation: 6 2 + 5 * 8 4 / -

The program reads expression into Stringbuilder infix then uses stack Inheritence to help the create the postfix expression in Stringbuilder postfix.

Untill now I have this:

[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InorderToPostfixApp
{
   public class StackInheritance:List
    {

       public StackInheritance():base("stack")
       {        
       }

       public void Push(object item)
       {
           insertAtFront(item);
       }

       public object Pop()
       {
           return RemoveFromFront();
       }
    }
}
[/code]

and the converter class:

[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.CodeDom.Compiler;




namespace InorderToPostfixApp
{
  public  class InfixToPostfixConverter
    {

      Stack stack = new Stack();
      object infixExpression;
      int num1;
      int num2;
      char operarand;
      StringBuilder infix = new StringBuilder();
      StringBuilder postfix = new StringBuilder();
      Stack<string>postfix_str = new Stack<string>();
      private bool isOperator;
      private const string OPERATOR = " +/*^% ";
      private int[] PRECEDENCE = { 1, 1, 2, 2 };
    
     
      char[] operand;

      public void ConvertToPostfix(string infix_str, char[]token)
      {
         // StringTokenizer tokes = new StringTokenizer();
          Stack<char> opstack = new Stack<char>();
          StringBuilder postfix = new StringBuilder();
          string[] tokens;
          tokens = infix_str.Split(token);

          stack.Push("(");
          Console.Write(stack);
           char first = token[0];

          for (int i = 0; i < infix.Length; i++)
          {
              infix[i].Append(")");
          }

          try
          {
              while (opstack.Count> 0)
              {
                

                  if (first == infix_str[0])
                      infix.Append(postfix);
              }//end while
              //return infix.ToString();
          }//end try
          catch(EmptyListException exception)
          {
              Console.Write(exception.Message);
           
          }
         
          if()
          {}


         // if(infix.cu == char)


     
     
      }//end method

      public bool IsOperator(char ch)
      {
          return OPERATOR.IndexOf(ch) >= 0;    
      }

      public int Precedence(char ch)
      {
          return (ch == null) ? 0 : PRECEDENCE[OPERATOR.IndexOf(ch)];
       
      }
    }
}
[/code]

and the StackEmpty class:

[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InorderToPostfixApp
{
  public  class StackEmptyException: ApplicationException
    {
      public StackEmptyException(string name)
          : base("the" + name + "is empty")
      { }
    }
}
[/code]

THX for helping.




Answers (2)