static void Main(string[] args) { string[] expressions = new string[] { "1+2", "3+4*5+6", "(1+2)*(3+4)", "(1-2/3)*(4/5-6)" }; InfixToPostfix infixToPostfix = new InfixToPostfix(); InfixToPrefix infixToPrefix = new InfixToPrefix(); PrefixToPostfix prefixToPostfix = new PrefixToPostfix(); PostfixToPrefix postfixToPrefix = new PostfixToPrefix(); PrefixToInfix prefixToInfix = new PrefixToInfix(); PostfixToInfix postfixToInfix = new PostfixToInfix(); foreach (var expression in expressions) { Console.WriteLine(expression); var prefix = infixToPrefix.Convert(expression); var postfix = infixToPostfix.Convert(expression); Console.WriteLine(prefixToInfix.Convert(prefix)); Console.WriteLine(postfixToInfix.Convert(postfix)); Console.WriteLine(prefix); Console.WriteLine(postfixToPrefix.Convert(postfix)); Console.WriteLine(postfix); Console.WriteLine(prefixToPostfix.Convert(prefix)); Console.WriteLine(); } Console.ReadKey(); }
/// <summary> /// 后序表达式求值 /// </summary> /// <param name="infix"></param> private static void EvaluatePostfix(string infix) { var postfix = InfixToPostfix.Convert(infix); Console.WriteLine($" Infix: {infix}"); Console.WriteLine($"Postfix: {postfix}"); var postfixQueue = InfixToPostfix.ConvertToQueue(infix); var result = InfixToPostfix.Evaluate(postfixQueue); Console.WriteLine($" Result: {result.ToString()}"); }
//------------------------------------------------------------------------------------------------------------ private void compileExpression(string[] tokens, int lineNumber, int storeLoc) { string infix = ""; // build infix expression for (int i = 4; i < tokens.Length; ++i) { infix += tokens[i]; } string postfix = InfixToPostfix.Convert(infix); Console.WriteLine($"Infix: {infix}, Postfix: {postfix}"); int temp = evaluatePostfix(postfix, lineNumber); // load temp _compiledCode.Add(Word.Build((int)EPC.Code.LOAD, temp), _workingLineNumber++); if (_symbolTable.HasLocation(storeLoc) && _symbolTable.AtLocation(storeLoc).IsConst()) { throw new SystemException($"Cannot override constant value at memory[{storeLoc}] -on code line {lineNumber}"); } _compiledCode.Add(Word.Build((int)EPC.Code.STORE, storeLoc), _workingLineNumber++); }
public void RegexConverterTests(string re, string pos) { Assert.That(InfixToPostfix.Convert(re), Is.EqualTo(pos)); }