public void Submit(string[] elementMath) { InfixToPostfix ITP = new InfixToPostfix(); if (textMath.Length > 0) { try { if (!ITP.check_error) { elementMath = ITP.ProcessString(textMath.ToString()); } if (!ITP.check_error) { elementMath = ITP.PostFix(elementMath); } if (!ITP.check_error) { textAns = new StringBuilder(ITP.ValueMath(elementMath)); } screenAns.Text = textAns.ToString(); screenTextMath = new StringBuilder(); textMath = new StringBuilder(); checkSubmit = 1; } catch (Exception ex) { Error(); } if (ITP.check_error) { Error(); } } }
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(); }
public void TestInfixToPostFix() { string infix = "((A+B)-C*(D/E))+F"; string postFix = InfixToPostfix.GetPostfix(infix); Assert.IsTrue(postFix == "AB+CDE/*-F+"); infix = "a+b*(c^d-e)^(f+g*h)-i"; postFix = InfixToPostfix.GetPostfix(infix); Assert.IsTrue(postFix == "abcd^e-fgh*+^*+i-"); }
/// <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()}"); }
/// <summary> /// 将infix转换为postfix后进行计算 /// </summary> public static void infixAndPostfixTest() { char[] infixArray = "4+4/2+(4*5-2^(2^2))*1".ToCharArray(); InfixToPostfix infix = new InfixToPostfix(); Postfix postfix = new Postfix(); string[] postfixArray = infix.Translate(infixArray); string result = postfix.GetResult(postfixArray); System.Console.WriteLine(result); }
public void ConverterTestOperatorWithParenthesis() { string expression = "10*(2+3+4)"; Token[] infix = Tokenizer.Tokenize(expression); Token[] postfix = InfixToPostfix.ToPostfix(infix); string[] expected = { "10", "2", "3", "+", "4", "+", "*" }; Assert.AreEqual(expected.Length, postfix.Length); for (int i = 0; i < postfix.Length; ++i) { Assert.AreEqual(expected[i], postfix[i].Value); } }
public void ConverterTestFunctionAndOperatorMix() { string expression = $"{LConstants.ABS_F}({LConstants.MAX_F}(STR,INT)+{LConstants.MIN_F}(10,-20))"; Token[] infix = Tokenizer.Tokenize(expression); Token[] postfix = InfixToPostfix.ToPostfix(infix); string[] expected = { "(", "(", "STR", "INT", LConstants.MAX_F, "(", "10", "-20", LConstants.MIN_F, "+", LConstants.ABS_F }; Assert.AreEqual(expected.Length, postfix.Length); for (int i = 0; i < postfix.Length; ++i) { Assert.AreEqual(expected[i], postfix[i].Value); } }
public void ConverterTestMultipleParams() { string expression = $"{LConstants.MAX_F}(STR,INT)"; Token[] infix = Tokenizer.Tokenize(expression); Token[] postfix = InfixToPostfix.ToPostfix(infix); string[] expected = { "(", "STR", "INT", LConstants.MAX_F }; Assert.AreEqual(expected.Length, postfix.Length); for (int i = 0; i < postfix.Length; ++i) { Assert.AreEqual(expected[i], postfix[i].Value); } }
public void ConverterTestFunctionAndOperator() { string expression = $"{LConstants.ABS_F}(10-100)"; Token[] infix = Tokenizer.Tokenize(expression); Token[] postfix = InfixToPostfix.ToPostfix(infix); string[] expected = { "(", "10", "100", "-", LConstants.ABS_F }; Assert.AreEqual(expected.Length, postfix.Length); for (int i = 0; i < postfix.Length; ++i) { Assert.AreEqual(expected[i], postfix[i].Value); } }
public void ConverterTestUnaryOperator() { string expression = "!(X+Y)"; Token[] infix = Tokenizer.Tokenize(expression); Token[] postfix = InfixToPostfix.ToPostfix(infix); string[] expected = { "X", "Y", "+", "!" }; Assert.AreEqual(expected.Length, postfix.Length); for (int i = 0; i < postfix.Length; ++i) { Assert.AreEqual(expected[i], postfix[i].Value); } }
public double CALCULATE(string[] formula, int ID_NhanVien, DateTime date) { Parse p = new Parse(formula, ID_NhanVien, date); List <Element> e = p.PARSE; InfixToPostfix i = new InfixToPostfix(); e = i.ConvertFromInfixToPostFix(e); PostFixEvaluator pfe = new PostFixEvaluator(); if (e.Count == 2 && (e[1].ToString() == "+" || e[1].ToString() == "-")) { this.Value = (double)Double.Parse(formula[0]); } else { this.Value = pfe.Evaluate(e); } return(Value); }
static void Main(string[] args) { //compute.stringValue = ("34+93-*"); Console.WriteLine("Enter an Infix expression: "); string input = Console.ReadLine(); char[] tokens = { ' ' }; //string[] inputList = input.Split(tokens, StringSplitOptions.RemoveEmptyEntries); string [] inputList = Regex.Split(input, @"(?=[+\-*\/)(])|(?<=[+\-*\/()])"); InfixToPostfix postfixConverter = new InfixToPostfix(); IList <string> postfix = postfixConverter.infixToPostfix(inputList); //Console.Write("Enter a postfix expression: "); //string input = Console.ReadLine(); //strinping[] inputList = input.Sp lit(' '); Evaluator evaluate = new Evaluator(); float floatAnswer = evaluate.postFixEvaluation(postfix); Console.WriteLine($"Postfix evaluation of {string.Join(" ", postfix)} is {floatAnswer}"); //Console.WriteLine("postfix evaluation of " + compute.stringValue + " is " + compute.expression()); }
//------------------------------------------------------------------------------------------------------------ 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)); }
public static void InfixToPostfixTest() { InfixToPostfix infix = new InfixToPostfix(); infix.Translate(); }