-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecondWindow.xaml.cs
229 lines (206 loc) · 7.14 KB
/
SecondWindow.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Calculator
{
/// <summary>
/// Логика взаимодействия для SecondWindow.xaml
/// </summary>
public partial class SecondWindow : Window
{
public SecondWindow()
{
InitializeComponent();
}
Parser p = new Parser();
private void result()
{
double intOne;
double intTwo;
double denominatorOne;
double denominatorTwo;
double numeratorOne;
double numeratorTwo;
if (intNumberOne.Text != "")
intOne = getResultOfExpression(intNumberOne.Text);
else
intOne = 0;
if (intNumberTwo.Text != "")
intTwo = getResultOfExpression(intNumberTwo.Text);
else
intTwo = 0;
if (denominatorNumberOne.Text != "")
denominatorOne = getResultOfExpression(denominatorNumberOne.Text);
else
denominatorOne = 1;
if (denominatorNumberTwo.Text != "")
denominatorTwo = getResultOfExpression(denominatorNumberTwo.Text);
else
denominatorTwo = 1;
if (numeratorNumberOne.Text != "")
{
numeratorOne = getResultOfExpression(numeratorNumberOne.Text);
if (numeratorOne == 0)
{
textBox.AppendText("Error: Знаменатель 1 не может равняться 0");
}
}
else
{
numeratorOne = 1;
}
if (numeratorNumberTwo.Text != "")
{
numeratorTwo = getResultOfExpression(numeratorNumberTwo.Text);
if (numeratorTwo == 0)
{
textBox.AppendText("Error: Знаменатель 2 не может равняться 0");
}
}
else
{
numeratorTwo = 1;
}
double[] dataArray = getDenominator(numeratorOne, numeratorTwo);
denominatorOne = (numeratorOne * intOne + denominatorOne) * dataArray[1];
denominatorTwo = (numeratorTwo * intTwo + denominatorTwo) * dataArray[2];
double[] resNumber = getResult(denominatorOne, numeratorOne, denominatorTwo, numeratorTwo, dataArray[0]);
if (resNumber[0] < resNumber[1])
{
denominatorResult.AppendText(resNumber[0].ToString());
numeratorResult.AppendText(resNumber[1].ToString());
}
else
{
intResult.AppendText(((int)(resNumber[0] / resNumber[1])).ToString());
denominatorResult.AppendText((resNumber[0] % resNumber[1]).ToString());
numeratorResult.AppendText(resNumber[1].ToString());
}
}
private double[] getFactor(double first, double second, double max)
{
double[] array = new double[2];
if (max == first)
{
array[1] = first / second;
array[0] = 1;
}
else if (max == second)
{
array[0] = second / first;
array[1] = 1;
}
return array;
}
private double[] getDenominator(double firstNumber, double secondNumber)
{
double[] result = new double[3];
double maxNumber = System.Math.Max(firstNumber, secondNumber);
double minNumber = System.Math.Min(firstNumber, secondNumber);
if(maxNumber%minNumber == 0)
{
result[0] = maxNumber;
if (maxNumber == firstNumber)
{
result[2] = firstNumber / secondNumber;
result[1] = 1;
}
else if (maxNumber == secondNumber)
{
result[1] = secondNumber / firstNumber;
result[2] = 1;
}
else
{
result[1] = 1;
result[2] = 1;
}
}
else
{
for(int i = 2; i < maxNumber; i++)
{
double newMaxNumber = maxNumber;
newMaxNumber = maxNumber * i;
if (newMaxNumber % minNumber == 0)
{
result[0] = newMaxNumber;
if (maxNumber == firstNumber)
{
result[2] = newMaxNumber / minNumber;
result[1] = i;
}
else if (maxNumber == secondNumber)
{
result[1] = newMaxNumber / minNumber;
result[2] = i;
}
break;
}
}
}
return result;
}
private double getResultOfExpression(String str)
{
return Convert.ToDouble(p.Evaluate(str));
}
private double[] getResult(double denFirst, double numFirst, double denSecond, double numSecond, double common)
{
double[] result = new double[2];
string oper = operand.Text;
switch (oper)
{
case "+":
result[0] = denFirst + denSecond;
result[1] = common;
break;
case "-":
result[0] = denFirst - denSecond;
result[1] = common;
break;
case "*":
result[0] = denFirst * denSecond;
result[1] = numFirst * numSecond;
break;
case "/":
result[0] = denFirst * numSecond;
result[1] = numFirst * denSecond;
break;
}
return result;
}
private void cleanButton_Click(object sender, RoutedEventArgs e)
{
denominatorResult.Clear();
numeratorResult.Clear();
intResult.Clear();
denominatorNumberOne.Clear();
denominatorNumberTwo.Clear();
numeratorNumberOne.Clear();
numeratorNumberTwo.Clear();
intNumberOne.Clear();
intNumberTwo.Clear();
textBox.Clear();
}
private void equalButton_Click(object sender, RoutedEventArgs e)
{
textBox.Clear();
result();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}