using System using System Collections Generic using System Linq using

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace огыеагт
{
class Program
{
static void Main1(string[] args)
{
ParameterExpression X = Expression.Parameter(typeof(double), "X");
ParameterExpression upperBound = Expression.Parameter(typeof(double), "upperBound");
ParameterExpression numerator = Expression.Variable(typeof(double), "numerator");
ParameterExpression denominator = Expression.Variable(typeof(double), "denominator");
ParameterExpression factorial = Expression.Variable(typeof(double), "factorial");
ParameterExpression I = Expression.Variable(typeof(double), "I");
ParameterExpression result = Expression.Variable(typeof(double), "result");
ParameterExpression mainResult = Expression.Variable(typeof(double), "mainResult");
LabelTarget innerLoopLabel = Expression.Label(typeof(double));
LabelTarget outerLoopLabel = Expression.Label(typeof(double));
BlockExpression block = Expression.Block(
new[] { mainResult, result, I },
Expression.Assign(mainResult, Expression.Constant(0.0)),
Expression.Assign(result, Expression.Constant(1.0)),
Expression.Assign(I, Expression.Constant(0.0)),
Expression.Loop(
Expression.IfThenElse(
Expression.And(
Expression.GreaterThan(result, Expression.Constant(0.0001)),
Expression.LessThan(I, upperBound)
),
Expression.Block(
new[] { numerator, denominator, factorial },
Expression.Assign(numerator,
Expression.Power(X,
Expression.Add(
Expression.Multiply(Expression.Constant(2.0),I),
Expression.Constant(1.0)
)
)
),
Expression.Assign(denominator, Expression.Constant(1.0)),
Expression.Assign(factorial, Expression.Add(
Expression.Multiply(Expression.Constant(2.0), I),
Expression.Constant(1.0)
)),
Expression.Loop(
Expression.IfThenElse(
Expression.GreaterThan(factorial, Expression.Constant(0.0)),
Expression.Block(
Expression.MultiplyAssign(denominator, Expression.PostDecrementAssign(factorial))
),
Expression.Break(innerLoopLabel, denominator)
),
innerLoopLabel
),
Expression.Assign(result,Expression.Divide(numerator, denominator)),
Expression.AddAssign(mainResult, result),
Expression.Assign(I,Expression.Increment(I))
),
Expression.Break(outerLoopLabel, mainResult)
),
outerLoopLabel
),
mainResult
);
Console.WriteLine(Expression.Lambda<Func<double, double, double>>(block, X, upperBound).Compile()(2, 100));
Console.ReadKey();
}
}
class NoLinq
{
static void Main(string[] args)
{
double upperBound = 100, X = 2;
double mainResult, result, I;
mainResult = 0;
result = 1;
I = 0;
for (; ; )
{
if (result > 0.0001 && I < upperBound)
{
double numerator, denomirator, factorial;
numerator = Math.Pow(X, 2 * I + 1);
denomirator = 1.0;
factorial = 2 * I + 1;
for (; ; )
{
if (factorial > 0)
{
denomirator *= factorial--;
}
else
{
break;
}
}
result = numerator / denomirator;
Console.WriteLine("I = " + I + "; result = " + result + "; numerator = " + numerator + "; denominator = " + denomirator);
mainResult += result;
I++;
}
else
{
break;
}
}
Console.WriteLine(mainResult);
Console.ReadKey();
}
}
}