Пример #1
0
 static void Main(string[] args)
 {
     //StringLikeArray sla = new StringLikeArray();
     Console.WriteLine("Введите формулу:");
     //SpaceInserter si = new SpaceInserter(Console.ReadLine());
     //si.InsertSpaces();
     ShuntingYard sy = new ShuntingYard(Console.ReadLine());//si.GetOutputString());
     sy.StringAnalys();
     Console.WriteLine("Обратная польская запись:");
     Console.WriteLine(sy.ReturnReversePolish());
     Console.ReadLine();
     ReversePolish rp = new ReversePolish(sy.ReturnReversePolish());
     if (!rp.VariableCollect())
     {
         Console.WriteLine("Переменных не обнаружено");
     }
     else
     {
         Console.WriteLine("Введите значения переменных:");
     }
     for (int i = 0; i < rp.VariableSet.Count; i++)
     {
         Variable v = (Variable)(rp.VariableSet[i]);
         Console.Write(v.var + " = ");
         v.value = Convert.ToDouble(Console.ReadLine());
         rp.VariableSet[i] = v;
     }
     rp.Calculate();
     Console.WriteLine("Результат: " + rp.Result);
     Console.ReadLine();
 }
Пример #2
0
        static void Test(string input)
        {
            Console.WriteLine(input);
            Console.WriteLine(new String('-', input.Length));
            var lex     = CreateLex();
            var grammar = CreateGrammar();
            var sy      = new ShuntingYard();

            sy.Grammar.AddRange(grammar);

            try
            {
                var lexems = lex.Execute(input);
                foreach (var lexem in lexems)
                {
                    Console.WriteLine("   {0,-30} {1,-15} {2}", lexem.Text, lexem.Kind == Kinds.Value ? lexem.ValueType.Name : KindsBase.ToString(lexem.Kind), lexem.Value);
                }
                Console.WriteLine();


                var upn = new UpnConverter();
                sy.Execute(lexems, upn);
                Console.WriteLine("UPN");
                Console.WriteLine("   {0}", upn.GetSource());
                Console.WriteLine();
            }
            catch (LexException le)
            {
                Console.WriteLine(le.Message);
                Console.WriteLine(le.GetErrorHelper());
            }
        }
Пример #3
0
 public static double Calculate(string formula)
 {
     StringLikeArray sla = new StringLikeArray();
     SpaceInserter si = new SpaceInserter(formula.Replace(",","."));
     si.InsertSpaces();
     ShuntingYard sy = new ShuntingYard(si.GetOutputString());
     sy.StringAnalys();
     ReversePolish rp = new ReversePolish(sy.ReturnReversePolish());
     rp.Calculate();
     return rp.Result;
 }