示例#1
0
文件: Calc.cs 项目: vesai/Calc
 /// <summary> Создать объект для вычисления выражения из предопределенных функций, записанных строками </summary>
 public Calc()
 {
     calc = new Calc<double>(
         new UnaryOperation<double> [] {
             Calc<double>.CreateUnaryOperation("+", a => a),
             Calc<double>.CreateUnaryOperation("-", a => -a),
             Calc<double>.CreateUnaryOperation("sqrt", a => Math.Sqrt(a)),
             Calc<double>.CreateUnaryOperation("!", a => {
                 int res = 1;
                 for (int i = 2; i <= a; i++) {
                     res *= i;
                 }
                 return res;
             }, true),
         },
         new BinaryOperation<double> [][] {
             new BinaryOperation<double> [] {
                 Calc<double>.CreateBinaryOperation("^", (a,b) => Math.Pow(a,b), true),
             },
             new BinaryOperation<double> [] {
                 Calc<double>.CreateBinaryOperation("*", (a,b) => a*b),
                 Calc<double>.CreateBinaryOperation("/", (a,b) => a/b),
             },
             new BinaryOperation<double> [] {
                 Calc<double>.CreateBinaryOperation("+", (a,b) => a+b),
                 Calc<double>.CreateBinaryOperation("-", (a,b) => a-b),
             }
         }, new Func<CharEnum, Tuple<double>>[] { DoubleParser.Parse });
 }
示例#2
0
文件: Program.cs 项目: vesai/Calc
 static void Main(string[] args)
 {
     DoubleCalc.Calc calc = new DoubleCalc.Calc();
     Console.WriteLine("Введите выражение:");
     Console.Write("  ");
     string str = Console.ReadLine();
     try {
         double res = calc.Process(str);
         Console.WriteLine("Результат:");
         Console.Write("  " + res.ToString(CultureInfo.InvariantCulture));
     } catch (CantCalcException e) {
         if (e.ErrorPosition != null) {
             Console.WriteLine(str);
             for (int i = 0; i < e.ErrorPosition.Value; i++) {
                 Console.Write(" ");
             }
             Console.WriteLine("^");
             Console.WriteLine("Символ: " + (e.ErrorPosition.Value+1));
         }
         Console.WriteLine("Ошибка: " + e.Message);
     }
     Console.ReadLine();
 }