-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
148 lines (128 loc) · 5.15 KB
/
MainWindow.xaml.cs
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
using System;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private Function m_checkedFunction = null;
private FunctionsArray m_functionsArray;
private TrapMethod m_method;
public MainWindow()
{
InitializeComponent();
m_method = new TrapMethod();
m_functionsArray = new FunctionsArray();
// Добавление всех функций в интерфейс
for(int i = 0;i<m_functionsArray.size();i++)
addFunction(m_functionsArray.get(i));
}
private void addFunction(Function fun)
{
// Добавление функции в интерфейс
RadioButton rb = new RadioButton {IsChecked = false, GroupName = "functionsCheck", Content = fun.Name};
rb.Checked += functionChoose;
functionStack.Children.Add(rb);
}
private void inputCorrect(object sender, TextChangedEventArgs e)
{
string newText = ((TextBox) sender).Text;
//Удаление лишних символов
Regex rgx = new Regex("[^-0-9,.]");
newText = rgx.Replace(newText, "");
rgx = new Regex("[.]");
newText = rgx.Replace(newText, ",");
// Удаление лишних точек
while(true)
{
int ind1 = newText.IndexOf(',');
if(ind1>=0)
{
int ind2 = newText.IndexOf(',',ind1+1);
if (ind2 > 0 || ind1 == 0)
newText = newText.Remove(ind1, 1);
else
break;
}
else
break;
}
// Удаление лишних '-'
while(true)
{
int ind1 = newText.IndexOf('-');
if(ind1==0)
{
int ind2 = newText.IndexOf('-', ind1+1);
if(ind2>0)
newText = newText.Remove(ind2,1);
else
break;
}
else if(ind1>0)
newText = newText.Remove(ind1,1);
else
break;
}
//((TextBox) sender).
((TextBox) sender).Text = newText;
}
private void calculate(object sender, RoutedEventArgs routedEventArgs)
{
// Отказ от вычисления, если одно из полей не заполнено
if (tLowLimit.Text.Length == 0 || tUpLimit.Text.Length == 0 || tPrecision.Text.Length == 0)
{
MessageBox.Show("Зваолните все поля");
return;
}
double lowLimit = Double.Parse(tLowLimit.Text);
double upLimit = Double.Parse(tUpLimit.Text);
double precision = Double.Parse(tPrecision.Text);
if (precision <=0)
{
MessageBox.Show("Нулевая или отрицательная погрешность технически нереализуема, измените значение");
return;
}
// Отказ от вычисления при отстутствии выбранной функции
if (m_checkedFunction == null)
{
MessageBox.Show("Выберерите интегрируемую функцию");
return;
}
// Вычисление результата
double fault;
int split;
double result = m_method.calculate(x => m_checkedFunction.Func(x),
lowLimit, upLimit, precision, out fault, out split);
tResult.Text = result.ToString();
tFault.Text = fault.ToString();
tSplit.Text = split.ToString();
}
private void functionChoose(object sender, RoutedEventArgs routedEventArgs)
{
// Поиск функции по имени
string functionName = ((RadioButton) sender).Content.ToString();
for (int i = 0; i < m_functionsArray.size(); i++)
{
if (m_functionsArray.get(i).Name.Equals(functionName))
m_checkedFunction = m_functionsArray.get(i);
}
}
private void checkInput(object sender, RoutedEventArgs e)
{
string newText = ((TextBox) sender).Text;
// Удаление - или . в конце строки
if (newText.Length == 0)
return;
char lastSym = newText[newText.Length-1];
if (lastSym == '-' || lastSym == ',')
newText = newText.Remove(newText.Length - 1);
((TextBox) sender).Text = newText;
}
}
}