-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
248 lines (199 loc) · 6.85 KB
/
Program.cs
File metadata and controls
248 lines (199 loc) · 6.85 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Interpolator
{
class Program
{
private int n;
private int pointCount;
private double[] areas;
private List<double[]> points;
private double[] values;
private char[] variables;
private double[] resultPoint;
private double denominator;
public Program()
{
Dimension();
Console.WriteLine();
this.pointCount = Convert.ToInt16(Math.Pow(2, n - 1));
this.areas = new double[pointCount];
this.variables = new char[n];
this.resultPoint = new double[n];
for (int i = 0; i < n; i++)
{
this.variables[i] = Convert.ToChar(65 + i);
}
this.values = new double[pointCount];
this.points = new List<double[]>();
for (int i = 0; i < n - 1; i++)
points.Add(new double[2]);
InitLines();
InitValues();
SearchResult();
}
private void Dimension()
{
int dimension;
try
{
Console.Write("Type in number of dimensions: ");
dimension = int.Parse(Console.ReadLine());
if (dimension > 1)
this.n = dimension;
else
{
Console.WriteLine("Dimension must be greater than 1!");
Dimension();
}
}
catch (FormatException e)
{
Console.WriteLine("Dimension must be an inteeger!");
Dimension();
}
}
private void Calculate()
{
denominator = 1;
for (int i = 0; i < n - 1; i++)
{
denominator *= points[i][1] - points[i][0];
}
int counter;
for (int i = 0; i < pointCount; i++)
{
areas[i] = 1;
counter = i;
for (int j = 0; j < n - 1; j++)
{
areas[i] *= points[j][counter % 2] - resultPoint[j];
counter /= 2;
}
areas[i] = areas[i] / denominator;
areas[i] = Math.Abs(areas[i]);
}
for (int i = 0; i < pointCount; i++)
{
resultPoint[n - 1] += values[i] * areas[pointCount - 1 - i];
}
}
public override string ToString()
{
return "Value of interpolation for chosen point is: " + Convert.ToString(Math.Round(resultPoint[n - 1], 2));
}
private void SearchResult()
{
double value;
Console.WriteLine("Type in searched point coordinates ");
for (int i = 0; i < n - 1; i++)
{
Console.Write("{0,-25} | {1,-5}", " (" + Convert.ToString(Math.Min(points[i][0], points[i][1])) + ";" + Convert.ToString(Math.Max(points[i][0], points[i][1])) +")", variables[i] + ": ");
try
{
value = Double.Parse(Console.ReadLine());
while (value <= Math.Min(points[i][0], points[i][1]) || value >= Math.Max(points[i][0], points[i][1]))
{
Console.WriteLine("Value should be included in range (" + Convert.ToString(Math.Min(points[i][0], points[i][1])) + ";" + Convert.ToString(Math.Max(points[i][0], points[i][1]) + ")"));
Console.Write(" " + variables[i] + ": ");
value = Double.Parse(Console.ReadLine());
}
resultPoint[i] = value;
}
catch (FormatException)
{
ExeptionBlock();
i--;
}
}
Console.WriteLine();
Calculate();
}
private void ExeptionBlock()
{
Console.WriteLine();
Console.WriteLine("You have to use right format of floating-point number.");
Console.WriteLine("E.g. xx,x (where x - digit). Please do use \",\" instead of \".\"");
Console.WriteLine();
}
private void InitValues()
{
Console.WriteLine("Type in values of defined points: ");
DisplayPoints();
Console.WriteLine();
}
private void DisplayPoints()
{
int counter;
string coordinates;
for (int i = 0; i < pointCount; i++)
{
coordinates = "";
counter = i;
for (int j = 0; j < n - 1; j++)
{
coordinates += Convert.ToString(points[j][counter % 2]);
if (j != n - 2)
coordinates += ";";
counter /= 2;
}
Console.Write("{0,-25} | {1,-5}", " (" + coordinates + ") ", variables.Last() + Convert.ToString(i) + ": ");
try
{
values[i] = Double.Parse(Console.ReadLine());
}
catch (FormatException)
{
ExeptionBlock();
i--;
}
}
}
private void InitLines()
{
int counter = 0;
int q = 0;
if (n > 2)
Console.WriteLine("Type in coordinates of lines: ");
else
Console.WriteLine("Type in coordinates of points: ");
for (int i = 0; i < 2 * (n - 1); i++)
{
if (i % 2 == 0)
q = 0;
else
q = 1;
Console.Write(" " + variables[counter] + Convert.ToString(q) + ": ");
try
{
points[counter][q] = Double.Parse(Console.ReadLine());
if (q == 1 && points[counter][0] == points[counter][1])
{
Console.WriteLine(+ variables[counter] + "1 must be different from " + variables[counter] + "0");
i--;
q = 0;
}
}
catch (FormatException)
{
ExeptionBlock();
i--;
if (q == 1)
counter--;
}
if (q == 1)
counter++;
}
Console.WriteLine();
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p);
Console.ReadKey();
}
}
}