Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Linq.Expressions;
- namespace ConsoleApplication12
- {
- class Program
- {
- static void Main(string[] args)
- {
- ParameterExpression value = Expression.Parameter(typeof(double), "value");
- ParameterExpression value2 = Expression.Parameter(typeof(double), "value2");
- // Creating an expression to hold a local variable.
- ParameterExpression result = Expression.Parameter(typeof(double), "result");
- // Creating a label to jump to from a loop.
- LabelTarget label = Expression.Label(typeof(double));
- // Creating a method body.
- BlockExpression block = Expression.Block(
- // Adding a local variable.
- new[] { result },
- // Assigning a constant to a local variable: result = 1
- Expression.Assign(result, Expression.Constant(1.0)),
- // Adding a loop.
- Expression.Loop(
- // Adding a conditional block into the loop.
- Expression.IfThenElse(
- // Condition: value > 1
- Expression.GreaterThan(value2, Expression.Constant(0.0)),
- // If true: result *= value --
- Expression.MultiplyAssign(result,
- Expression.Divide(value,Expression.PostDecrementAssign(value2))),
- // If false, exit the loop and go to the label.
- Expression.Break(label, result)
- ),
- // Label to jump to.
- label
- )
- );
- // Compile and execute an expression tree.
- for (double j = 0.1; j <= 1.01; j += 0.05)
- {
- int i = 0;
- double sum = 1, res = 0;
- while (sum > 0.0004)
- {
- sum = Expression.Lambda<Func<double, double, double>>(block, value, value2).Compile()(j, 2 * i + 1);
- i++;
- res += sum;
- }
- double y = 0;
- y = (Math.Pow(Math.E, j) - Math.Pow(Math.E, -j)) / 2;
- Console.WriteLine("{0:f2} {1:f4} {2:f4}", j, res, y);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement