Repository navigation
Expand file tree
/
Copy pathInput.cs
More file actions
303 lines (302 loc) · 12.9 KB
/
Copy pathInput.cs
File metadata and controls
303 lines (302 loc) · 12.9 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
namespace software_application_24point
{
class Input : INotifyPropertyChanged//for Controls to implement binding
{
public event PropertyChangedEventHandler PropertyChanged;
/*事实上,未来修正中此处接口可以取消。在最终程序中不需绑定。但由于调试过程所需因此遗留。*/
private int[] array;//array storage the ASCII code of user input
public int[] GetandSetArray
{
get { return array; }
set { array = value; }
}
private double caculationresult;//this member represents the caculation result of user input
public double CaculationResult
{
get { return caculationresult; }
set { caculationresult = value; }
}
private int[] rpn;//rpn storages the reversed polish notation of user input,operations are saved in form of ASCII
public int[] Rpn
{
get { return rpn; }
set { rpn = value; }
}
private string expression;//expression binding with the text property of a textcontrol,it represents the string input
public string Expression
{
get { return expression; }
set
{
expression = value;
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Expression"));
}
}
}
private string reverse_polish_notation;//storage the reversed Polish notation in string form
public string ReversePolishNotation
{
get { return reverse_polish_notation; }
set
{
reverse_polish_notation = value;
if (this.PropertyChanged != null)//由于不再绑定,这里实现的接口同样没有意义了,仅用于软件测试
{
PropertyChanged(this, new PropertyChangedEventArgs("ReversePolishNotation"));
}
}
}
public async Task StringDealAsync()//including ContentDialog, which needs async symbol
{
Expression.Trim();//remove blank
int length = Expression.Length;//we need to traverse the entire string and judge every character.
if (length > 0)//else the input is null
{
Stack<int> stack = new Stack<int>();//this stack is used to convert rpn to nifix expression, and the inversion.
ReversePolishNotation = null;//represents the string form of rpn
int correctinput = 1;//1 means input is legal.0 means the input is illegal.
int flag = 0;//flag represent how many number elements are before this element
int rkuo = 0, lkuo = 0;//rkuo means the number of right bracket; lkuo means the number of left bracket.
int number = 0;//number record the number of numbers in input.
if(expression[0] != '+' && expression[0] != '(' && ((expression[0] < '0' || expression[0] > '9')))//judge the first character
{
correctinput = 0;
}
if(expression[0] == '+' || expression[0] == '(')//if the first character is not a number,
{
flag = 1;//we assume there is a hidding number before.
}
for (int i = 0; i < length; i++)//flag represent how many number elements are before this element
{
/*we only admit the characters below:10 kinds of numbers and five kinds of operations*/
if ((expression[i] >= '0' && expression[i] <= '9') || expression[i] == '+' || expression[i] == '-' || expression[i] == '/' || expression[i] == '*' || expression[i] == '^' || expression[i] == ' ' || expression[i] == '(' || expression[i] == ')')
{
if(expression[i] >= '0' && expression[i] <= '9')
{
number++;
flag++;//flag can help us define the digit of input number
if(flag > 2)//flag > 2, means there is a 3-digit-number
{
correctinput = 0;
break;
}
}
else
{
if(flag == 0)//means there is an opreartion before
{
if (expression[i - 1] == ')')
{
if(expression[i] == '(')//右左括号不能相接,其他情况下若前一位是)那么本位可以是任何符号
{
correctinput = 0;
break;
}
}
else if(expression[i] == '(')//如果本位是(,那么前一位可以是除了)外任何符号
{
lkuo++;
}
else//如果本位不是(而且前一位也不是),则两个符号相连必定出错
{
correctinput = 0;
break;
}
}
else if(expression[i] == ')') { rkuo++; }//whatever the character before is,rkuo or lkuo should be record correctly
else if(expression[i] == '(') { lkuo++; }
flag = 0;
}
}
else
{
correctinput = 0;
}
}
if(lkuo != rkuo)//if'('is not the same number as ')',the input must wrong
{
correctinput = 0;
}
if (number < 4)//if the numbers in the input is less than 4,input must be wrong
{
correctinput = 0;
}
if (correctinput == 0)//when the input is incorrect, pop up a dialog to inform the user
{
ContentDialog noWifiDialog = new ContentDialog
{
Title = "WRONG INPUT",
Content = "Check your expression.",
CloseButtonText = "Ok"
};
ContentDialogResult result = await noWifiDialog.ShowAsync();//to make the main thread synchronize with the dialog
return;
}
else
{
if (expression[0] == '+')//after judge the input, copy the string into expression
{
for (int i = 1; i < length; i++)
{
if(expression[i] != ' ')
{
ReversePolishNotation += Expression[i];
}
}
}
else
{
for (int i = 0; i < length; i++)
{
if (expression[i] != ' ')
{
ReversePolishNotation += Expression[i];
}
}
}
}
length = ReversePolishNotation.Length;
array = new int[length];//array will storage the ASCII of input
flag = 0;
int j = 0;//j uses as the index of array
for (int i = 0; i < length; i++,j++)//flag represent how many number elements are before this element
{
if(reverse_polish_notation[i] > '9' || reverse_polish_notation[i] < '0')//collect operation
{
array[j] = (int)ReversePolishNotation[i];
flag = 0;
}
else
{
j -= flag;//flag !=0 means there is number before,then the continual numbers in reverse_polish_notation is one number
array[j] = (reverse_polish_notation[i] - 48)+array[j]*10;//the early number has bigger digit
flag = 1;
}
}
//获取后缀表达式(SACII版本)
rpn = new int[array.Length];
int temp;
j = 0;
for (int i =0; i < array.Length && array[i] != 0; i++)
{
if(array[i]>0 && array[i] < 14)// numbers directly add in the array
{
rpn[j++] = array[i];
}
else if (array[i] == '+' || array[i] == '-' || array[i] == '*' || array[i] == '/' || array[i] == '^')
{
if(stack.Count == 0)
{
stack.Push(array[i]);
}
else
{
while(stack.Count != 0)
{
temp = stack.Peek();
if(GetPriority(temp) >= GetPriority(array[i]))
{
rpn[j++] =temp;
stack.Pop();
}
else {break; }
}
stack.Push(array[i]);
}
}
else
{
if (array[i] == '(')
{
stack.Push(array[i]);
}
else
{
while (stack.Count != 0 && stack.Peek() != '(')
{
temp = stack.Pop();
rpn[j++] = temp;
}
if (stack.Count != 0)
{
stack.Pop();
}
}
}
}
while (stack.Count != 0)
{
temp = stack.Pop();
rpn[j++] = temp;
}
//计算后缀表达式的值
Stack<double> dstack = new Stack<double>();
length = rpn.Length;
double num1, num2, num3 = -1;
for (int i = 0; i < length; i++)
{
if(rpn[i] == 0)
{
break;
}
if (rpn[i] > 0 && rpn[i] < 14)
{
dstack.Push(rpn[i]);
}
else
{
num2 = dstack.Pop();
num1 = dstack.Pop();
if (rpn[i] == '+')
{
num3 = num1 + num2;
}
else if (rpn[i] == '-')
{
num3 = num1 - num2;
}
else if (rpn[i] == '*')
{
num3 = num1 * num2;
}
else if (rpn[i] == '/')
{
num3 = num1 / num2;
}
else if (rpn[i] == '^')
{
num3 = Math.Pow(num1,num2);
}
dstack.Push(num3);
}
}
CaculationResult = dstack.Pop();
}
}
public int GetPriority(int n)
{
int p= -1;
if(n == '(') { p = 0; }
if(n == '+' || n == '-') { p = 1; }
if(n == '*' || n == '/') { p = 2; }
if(n == '^') { p = 3; }
if(n == ')') { p = 4; }
return p;
}
public Input(string expression)
{
this.expression = expression;
//this.reverse_polish_notation = "he\nllo.";
}
}
}