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
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
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.
albert albertPosted Oct 19, 2011, 7:35 AM
But it is has to be a console app.
I write the hole algorithm:
a) put a left parenthesis '(' on the stack
b) Append a right parenthesis ')' on the end of infix
c) while the stack is not empty , read infix from left to right and do the following:
if the current character in infix is a digit , append it to postfix.
if the current character in infix is a left parenthesis , push it onto the stack
if the current character in infix is an operator:
Pop operators(if there are any) at the top of the stack while they have equal or higher precedence then the current operator, and append the popped operators to postfix.
push the current character in infix onto the stack.
if the current characters in infx is a right parenthesis:
pop operators from the pop of the stack and append them to postfix until Pop(and discard) the left parenthesis from the stack.
And there are three methods:
COnvertTo.Postfix(){} - I have it partly
IsOperator(){} - I have it
Precedence(){} - I have it
I have attached the project
Hemant KumarPosted Oct 19, 2011, 7:11 AM
There are generalized parsers available (use Google), but if I were going to write one, I would not start with a character array, but parse "tokens" that would be math symbols, keywords, numbers, variable names, etc.