lab02

  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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab02
{
class Program
{
const double accuracy = 1.0E-15;
static void Main(string[] args)
{
foreach (string arg in args)
if (arg == "-a")
{
Console.WriteLine("Лабораторная работа №2\n"+
"Напишите функцию, вычисляющую числа 𝑒,𝜋 и ln2\n" +
"Группа: 5-ПМИ-095\nСделал: Дубровский Николай\n" +
"Нажмите что-нибудь, чтобы продолжить работу");
Console.ReadLine();
Console.Clear();
}
SolvePi();
SolveEps();
SolveLn2();
}
static void SolvePi()
{
double pi = 0;
for (int i = 0; Math.Abs(Math.PI - 2 * Math.Sqrt(3) * pi) > accuracy; i++)
pi += Math.Pow(-1, i) / (Math.Pow(3, i) * (2 * i + 1));
Console.WriteLine("Pi = {0}", 2 * Math.Sqrt(3) * pi);
}
static void SolveLn2()
{
double ln2 = 0;
for (int i = 0; Math.Abs(Math.Log(2) - 2 * ln2) > accuracy; i++)
ln2 += 1 / ((2 * i + 1) * Math.Pow(3, 2 * i + 1));
Console.WriteLine("Ln(2) = {0}", 2 * ln2);
}
static void SolveEps()
{
int NumberOfDigits = 18, size = 16;
long[] eps = new long[size];
long[] variable = new long[size];
eps[0] = 1;
variable[0] = 1;
for (int i = 1; i != NumberOfDigits; ++i)
{
DivE(size, variable, i);
AddE(size, eps, variable);
}
Print(size, eps);
}
static void DivE(int n, long[] x, int del)
{
long RealPart = 1000; //целая часть
long var, tempXi, c = 0;
for (int i = 0; i != n; ++i)
{
tempXi = x[i] + RealPart * c;
var = tempXi / del;
c = tempXi - var * del;
x[i] = var;
}
}
static void AddE(int n, long[]x, long[] y)
{
long RealPart = 1000, c = 0;
for (int i = n - 1; i >= 0; i--)
{
x[i] += y[i] + c;
if (x[i] < RealPart) c = 0;
else
{
c = 1;
x[i] -= RealPart;
}
}
}
static void Print(int n, long[] x)
{
string a;
a = x[0].ToString() + '.';
for (int i = 1; i != 6; ++i)
{
switch (x[i].ToString().Length)
{
case 1:
a += "00" + x[i].ToString();
break;
case 2:
a += '0' + x[i].ToString();
break;
default:
a += x[i].ToString();
break;
}
}
Console.WriteLine("e = {0}", a);
}
}
}