-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
463 lines (437 loc) · 18.8 KB
/
Form1.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using System.Collections;
namespace Four_operation_generators
{
public partial class Form1 : Form
{
private int IExercisesNum = 100; //100道题也不少了,照顾点孩子吧!!!
private const int MaxNumOfOperands = 4; // 最大操作数
private bool IsContainDecimal = true; // 是否包含小数
string strQuestionPath = @"C:\";
string strAnswerPath;
private bool IsRight; //类型转换结果标志量(用于string转int、double)
private bool[] bFourOperator = { true, true, true, true }; //四个操作符选择
private int ISelection = 0; //难度选择
private double DMin = -100; //操作数最小值
private double DMax = 100; //操作数最大值
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
IsRight = Int32.TryParse(textBox1.Text, out IExercisesNum);
if (IsRight || String.IsNullOrEmpty(textBox1.Text))
{
;
}
else
{
MessageBox.Show("能不能整点阳间能看懂的数啊!秋梨膏!","错误提示");
}
}
private void Form1_Load(object sender, EventArgs e) //窗体加载执行初始参数设置
{
textBox1.Text = IExercisesNum.ToString();
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
comboBox1.SelectedIndex = 0;
minText.Text = DMin.ToString();
maxText.Text = DMax.ToString();
}
private void txt_GotFocus(object sender, EventArgs e) //一旦题目量输入口得到焦点,清除显示值
{
textBox1.Text = "";
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
bFourOperator[i] = checkedListBox1.GetItemChecked(i);
}
if(!bFourOperator[0] && !bFourOperator[1] && !bFourOperator[2] && !bFourOperator[3])
{
MessageBox.Show("至少要选择一个算符吧???", "错误提示");;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ISelection = comboBox1.Items.IndexOf(comboBox1.Text);
}
private void MIN_MAX_LeftFocus(object sender, EventArgs e)
{
IsRight = Double.TryParse(minText.Text, out DMin);
if (IsRight || String.IsNullOrEmpty(minText.Text))
{
;
}
else
{
MessageBox.Show("能不能整点阳间能看懂的数啊!秋梨膏!", "错误提示");
}
IsRight = Double.TryParse(maxText.Text, out DMax);
if (IsRight || String.IsNullOrEmpty(maxText.Text))
{
;
}
else
{
MessageBox.Show("能不能整点阳间能看懂的数啊!秋梨膏!", "错误提示");
}
if(DMin>=DMax)
{
MessageBox.Show("喂喂喂!最大值和最小值填反了吧,搞事情?", "错误提示");
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
IsContainDecimal = true;
}
else
{
IsContainDecimal = false;
}
}
private void produce_Click(object sender, EventArgs e)
{
if(!CheckProjectList())
{
;
}
else
{
SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog();
dialog.Filter = "文件(*.txt)|*.txt"; //设置对话框保存的文件类型
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//将ok返回默认用户公共对话框
{
strQuestionPath = dialog.FileName; //获取文件路径和文件名
strAnswerPath = strQuestionPath.Substring(0, strQuestionPath.LastIndexOf(".")) + "_Answer.txt";
CreateTxt();
textBox2.Text = strQuestionPath;
textBox3.Text = strAnswerPath;
MessageBox.Show("创建成功!");
}
}
}
private bool CheckProjectList() //检查项目设置,没有错误生成项目清单,有错返回false
{
if (String.IsNullOrEmpty(textBox1.Text)|| !Int32.TryParse(textBox1.Text, out IExercisesNum)
|| IExercisesNum<=0)
{
MessageBox.Show("请输入合法题目数量值,生成失败!", "错误提示");
return false;
}
else if(!bFourOperator[0] && !bFourOperator[1] && !bFourOperator[2] && !bFourOperator[3])
{
MessageBox.Show("请至少选择一个算符,生成失败!", "错误提示");
return false;
}
else if((!Double.TryParse(maxText.Text, out DMax) || String.IsNullOrEmpty(minText.Text))
|| !Double.TryParse(maxText.Text, out DMax) || String.IsNullOrEmpty(maxText.Text)
|| DMin>=DMax)
{
MessageBox.Show("请输入合法的操作数范围区间,生成失败!", "错误提示");
return false;
}
else if(!IsContainDecimal&&(DMax - DMin) <= 1.0)
{
MessageBox.Show("此区间无整数,生成失败!", "错误提示");
return false;
}
else
{
return true;
}
}
private void CreateTxt() //生成题目和答案
{
FileStream fsQuestion = File.Create(strQuestionPath);
FileStream fsAnswer = File.Create(strAnswerPath);
fsQuestion.Close();
fsAnswer.Close();
bool brackets = false; //括号开关
if(!IsContainDecimal)
{
NotContainDecimal();
}
else
{
ContainDecimal();
}
}
private void ContainDecimal()
{
StreamWriter swQuestion = new StreamWriter(strQuestionPath);
StreamWriter swAnswer = new StreamWriter(strAnswerPath);
for (int x = 0; x < IExercisesNum; x++)
{
ArrayList list = new ArrayList();
bool Istongji;
string opt = "";
string tmp = "";
//Random rd = new Random(); //创立随机数种子
int GetRandomSeed() //提高随机数不重复概率的种子生成方法
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
Random rd = new Random(GetRandomSeed());
int left = rd.Next() % (MaxNumOfOperands - 1); // 括号左位置
int right = rd.Next() % (MaxNumOfOperands - left - 1) + left + 1; //括号右位置
while (left == 0 && right == MaxNumOfOperands - 1)
{
left = rd.Next() % (MaxNumOfOperands - 1); // 括号左位置
right = rd.Next() % (MaxNumOfOperands - left - 1) + left + 1; //括号右位置
}
if ((bFourOperator[0] && bFourOperator[1] && !bFourOperator[2] && !bFourOperator[3]) || (!bFourOperator[0] && !bFourOperator[1] && bFourOperator[2] && bFourOperator[3]) || (bFourOperator[0] && !bFourOperator[1] && !bFourOperator[2] && !bFourOperator[3]) || (!bFourOperator[0] && bFourOperator[1] && !bFourOperator[2] && !bFourOperator[3]) || (!bFourOperator[0] && !bFourOperator[1] && bFourOperator[2] && !bFourOperator[3]) || (!bFourOperator[0] && !bFourOperator[1] && !bFourOperator[2] && bFourOperator[3]))
{
Istongji = true;
}
else
{
Istongji = false;
}
while (left == 0 && Istongji)
{
left = rd.Next() % (MaxNumOfOperands - 1); // 括号左位置
right = rd.Next() % (MaxNumOfOperands - left - 1) + left + 1; //括号右位置
}
if (bFourOperator[0])
{
opt = opt.Insert(opt.Length, "+");
}
if(bFourOperator[1])
{
opt = opt.Insert(opt.Length, "-");
}
if(bFourOperator[2])
{
opt = opt.Insert(opt.Length, "*");
}
if(bFourOperator[3])
{
opt = opt.Insert(opt.Length, "÷");
}
for (int i = 0; i < MaxNumOfOperands; i++)
{
string temp;
double a = rd.NextDouble() * (DMax - DMin) + DMin;
double b = Math.Round(a, 2);
bool Isnegative = false;
if ( b < 0 )
{
Isnegative = true;
}
temp = Convert.ToString(b);
if(Isnegative)
{
temp = temp.Insert(0, "(");
temp = temp.Insert(temp.Length, ")");
}
if(ISelection == 1)
{
if (i == left)
{
temp = temp.Insert(0, "(");
temp = temp.Insert(temp.Length, " ");
}
else if (i == right)
{
temp = temp.Insert(temp.Length, ")");
}
else
{
temp = temp.Insert(temp.Length, " ");
}
}
else
{
temp = temp.Insert(temp.Length, " ");
}
if(temp.Length == 2)
{
temp = temp.Insert(0, " ");
}
else if(temp.Length == 3)
{
temp = temp.Insert(0, " ");
}
tmp = tmp.Insert(tmp.Length, temp);
if (i < MaxNumOfOperands - 1)
{
string option = opt.Substring(rd.Next() % opt.Length, 1);
tmp = tmp.Insert(tmp.Length, option);
tmp = tmp.Insert(tmp.Length, " ");
}
else
tmp = tmp.Insert(tmp.Length, " = ");
}
list.Add(tmp);
for (int i = 0; i < list.Count; i++)
{
swQuestion.WriteLine(list[i].ToString() + " ");
}
}
swQuestion.Close();
swAnswer.Close();
}
private void NotContainDecimal()
{
StreamWriter swQuestion = new StreamWriter(strQuestionPath);
StreamWriter swAnswer = new StreamWriter(strAnswerPath);
for (int x = 0; x < IExercisesNum; x++)
{
ArrayList list = new ArrayList();
bool Istongji;
string opt = "";
string tmp = "";
//Random rd = new Random(); //创立随机数种子
int GetRandomSeed() //提高随机数不重复概率的种子生成方法
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
Random rd = new Random(GetRandomSeed());
int left = rd.Next() % (MaxNumOfOperands - 1); // 括号左位置
int right = rd.Next() % (MaxNumOfOperands - left - 1) + left + 1; //括号右位置
while (left == 0 && right == MaxNumOfOperands - 1)
{
left = rd.Next() % (MaxNumOfOperands - 1); // 括号左位置
right = rd.Next() % (MaxNumOfOperands - left - 1) + left + 1; //括号右位置
}
if ((bFourOperator[0] && bFourOperator[1] && !bFourOperator[2] && !bFourOperator[3]) || (!bFourOperator[0] && !bFourOperator[1] && bFourOperator[2] && bFourOperator[3]) || (bFourOperator[0] && !bFourOperator[1] && !bFourOperator[2] && !bFourOperator[3]) || (!bFourOperator[0] && bFourOperator[1] && !bFourOperator[2] && !bFourOperator[3]) || (!bFourOperator[0] && !bFourOperator[1] && bFourOperator[2] && !bFourOperator[3]) || (!bFourOperator[0] && !bFourOperator[1] && !bFourOperator[2] && bFourOperator[3]))
{
Istongji = true;
}
else
{
Istongji = false;
}
while (left == 0 && Istongji)
{
left = rd.Next() % (MaxNumOfOperands - 1); // 括号左位置
right = rd.Next() % (MaxNumOfOperands - left - 1) + left + 1; //括号右位置
}
if (bFourOperator[0])
{
opt = opt.Insert(opt.Length, "+");
}
if(bFourOperator[1])
{
opt = opt.Insert(opt.Length, "-");
}
if(bFourOperator[2])
{
opt = opt.Insert(opt.Length, "*");
}
if(bFourOperator[3])
{
opt = opt.Insert(opt.Length, "÷");
}
for (int i = 0; i < MaxNumOfOperands; i++)
{
string temp;
bool Isnegative = false;
int max = Convert.ToInt32(DMax);
int min = Convert.ToInt32(DMin);
int a = rd.Next(0, 2);
int b;
if(min < 0 && max < 0)
{
b = rd.Next(-max, -min + 1) * (-1);
}
else if(min >= 0 && max >0)
{
b = rd.Next(min, max + 1);
}
else
{
if(a == 0)
{
b = rd.Next(0, max + 1);
}
else
{
b = rd.Next(0 , -min + 1) * (-1);
}
}
if ( b < 0 )
{
Isnegative = true;
}
temp = Convert.ToString(b);
if(Isnegative)
{
temp = temp.Insert(0, "(");
temp = temp.Insert(temp.Length, ")");
}
if(ISelection == 1)
{
if (i == left)
{
temp = temp.Insert(0, "(");
temp = temp.Insert(temp.Length, " ");
}
else if (i == right)
{
temp = temp.Insert(temp.Length, ")");
}
else
{
temp = temp.Insert(temp.Length, " ");
}
}
else
{
temp = temp.Insert(temp.Length, " ");
}
if(temp.Length == 2)
{
temp = temp.Insert(0, " ");
}
else if(temp.Length == 3)
{
temp = temp.Insert(0, " ");
}
tmp = tmp.Insert(tmp.Length, temp);
if (i < MaxNumOfOperands - 1)
{
string option = opt.Substring(rd.Next() % opt.Length, 1);
tmp = tmp.Insert(tmp.Length, option);
tmp = tmp.Insert(tmp.Length, " ");
}
else
tmp = tmp.Insert(tmp.Length, " = ");
}
list.Add(tmp);
for (int i = 0; i < list.Count; i++)
{
swQuestion.WriteLine(list[i].ToString() + " ");
}
}
swQuestion.Close();
swAnswer.Close();
}
private void 版本ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("四则运算生成器1.1.1.2020042517_beta\ndesigned by ZhangHao and Yangyingying", "版本");
}
}
}