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
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.IO;
using System.Xml;
namespace WpfApplication17_Курсач_1
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string fpath; // путь к файлу теста
string fname; // файл теста
System.Xml.XmlReader xmlReader; //чтение данных xml-файла
string qw; // вопрос
string[] answ = new string[4]; // варианты ответа
int right; // правильный ответ (номер)
int otv; // выбранный ответ (номер)
int n; // количество правильных ответов
int nv; // общее количество вопросов
int mode; // состояние программы (0 - начало работы; 1 - тестирование; 2 - завершение работы)
public class Questions
{
public int right { get; set; }
public string[] answ { get; set; }
public string que { get; set; }
public int num { get; set; }
public Questions(int n, int r, string[] a, string q)
{
num = n;
right = r;
answ = a;
que = q;
}
}
public LinkedList<Questions> questions = new LinkedList<Questions>();
int count = 0;
public MainWindow()
{
InitializeComponent();
radioButton1.Visibility = System.Windows.Visibility.Hidden;
radioButton2.Visibility = System.Windows.Visibility.Hidden;
radioButton3.Visibility = System.Windows.Visibility.Hidden;
radioButton4.Visibility = System.Windows.Visibility.Hidden;
fpath = System.Windows.Forms.Application.StartupPath;
fname = "\\document.Xml";
try
{
// получаем доступ к xml-документу
xmlReader = new System.Xml.XmlTextReader(fpath + fname);
xmlReader.Read();
mode = 0;
n = 0;
this.showHead(); // загружаем заголовок теста
this.showDescription(); // загружаем описание теста
this.getQw(); // загружаем все вопросы теста
}
catch(Exception)
{
tblock1.Text = "Ошибка доступа к файлу " + fname;
System.Windows.Forms.MessageBox.Show("Ошибка доступа к файлу.\n" + fname + "\n", "Электронное тестирование",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
mode = 2;
}
}
private void showHead()
{
// ищем узел <head>
do xmlReader.Read();
while (xmlReader.Name != "head");
xmlReader.Read(); // считываем заголовок
this.Title = xmlReader.Value; // выводим название теста в заголовок окна
xmlReader.Read(); // выходим из узла <head>
}
private void showDescription() // выводим описание теста
{
// ищем узел <description>
do
xmlReader.Read();
while (xmlReader.Name != "description");
xmlReader.Read(); // считываем описание теста
tblock1.Text = xmlReader.Value; // выводим описание теста
xmlReader.Read(); // выходим из узла <description>
// ищем узел вопросов <qw>
do
xmlReader.Read();
while (xmlReader.Name != "qw");
xmlReader.Read(); // входим внутрь узла
}
private void getQw() // читает вопрос из файла теста
{
bool ex = true;
while (ex)
{
// считываем тэг <q>
xmlReader.Read();
if (xmlReader.Name == "q")
{
qw = xmlReader.GetAttribute("text"); // извлекаем значение атрибута
xmlReader.Read(); // входим внутрь узла
int i = 0;
count++;
// считываем данные узла вопроса <q>
while (xmlReader.Name != "q")
{
xmlReader.Read();
// варианты ответа
if (xmlReader.Name == "a")
{
// запоминаем правильный ответ
if (xmlReader.GetAttribute("right") == "yes")
right = i;
// считываем вариант ответа
xmlReader.Read();
if (i < 4)
{
answ[i] = xmlReader.Value;
}
// выходим из узла <a>
xmlReader.Read();
i++;
}
}
Questions question = new Questions(count, right, new string[] { answ[0], answ[1], answ[2], answ[3] }, qw);
questions.AddLast(question);
// выходим из узла вопроса <q>
xmlReader.Read();
ex = true;
}
// если считанный тэг не является тэгом вопроса <q>
else
ex = false;
}
}
private void showQw() // выводим вопрос и варианты ответа
{
LinkedListNode<Questions> node;
for (node = questions.First; node != null; node = node.Next)
{
// выводим вопрос
tblock1.Text = node.Value.que;
// показать варианты ответа
radioButton1.Content = node.Value.answ[0];
radioButton2.Content = node.Value.answ[1];
radioButton3.Content = node.Value.answ[2];
radioButton4.Content = node.Value.answ[3];
//удаляем использованый вопрос
questions.Remove(node);
radioButton5.IsChecked = true;
button1.IsEnabled = false;
}
}
// щелчок на кнопке выбора ответа; функция обрабатывает событие Click
private void radioButton1_Click(object sender, RoutedEventArgs e)
{
if ((RadioButton)sender == radioButton1) otv = 0;
if ((RadioButton)sender == radioButton2) otv = 1;
if ((RadioButton)sender == radioButton3) otv = 2;
if ((RadioButton)sender == radioButton4) otv = 3;
button1.IsEnabled = true;
}
// щелчок на кнопке Ok
private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
switch (mode)
{
case 0: // начало работы программы
radioButton1.Visibility = System.Windows.Visibility.Visible;
radioButton2.Visibility = System.Windows.Visibility.Visible;
radioButton3.Visibility = System.Windows.Visibility.Visible;
radioButton4.Visibility = System.Windows.Visibility.Visible;
this.showQw();
mode = 1;
button1.Content = "Далее";
button1.IsEnabled = false;
radioButton5.IsChecked = true;
break;
case 1:
nv++;
radioButton1.IsChecked = false;
radioButton2.IsChecked = false;
radioButton3.IsChecked = false;
radioButton4.IsChecked = false;
// правильный ли ответ выбран
if (otv == right)
n++;
if (questions.Count > 0)
{
this.showQw();
}
else
{
// больше вопросов нет
radioButton1.Visibility = System.Windows.Visibility.Hidden;
radioButton2.Visibility = System.Windows.Visibility.Hidden;
radioButton3.Visibility = System.Windows.Visibility.Hidden;
radioButton4.Visibility = System.Windows.Visibility.Hidden;
// обработка и вывод результата
this.showLevel();
button1.Content = "Выход";
// следующий щелчок на кнопке Ok
// закроет окно программы
mode = 2;
}
break;
case 2: // завершение работы программы
this.Close(); // закрыть окно
break;
}
}
// выводит оценку
private void showLevel()
{
// ищем узел <levels>
do
xmlReader.Read();
while (xmlReader.Name != "levels");
// входим внутрь узла
xmlReader.Read();
// читаем данные узла
while (xmlReader.Name != "levels")
{
xmlReader.Read();
if (xmlReader.Name == "level")
// n - кол-во правильных ответов,
// проверяем, попадаем ли в категорию
if (n >= System.Convert.ToInt32(xmlReader.GetAttribute("score")))
break;
}
// выводим оценку
tblock1.Text = "Тестирование завершено.\n" + "Всего вопросов: " + nv.ToString() + ". " +
"Правильных ответов: " + n.ToString() + ".\n" + xmlReader.GetAttribute("text");
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (mode == 1)
{
if (System.Windows.Forms.MessageBox.Show("Вы действительно хотите выйти из программы теста?", "Подтверждение выхода",
System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
e.Cancel = true;
else
MessageBox.Show("Вы не сдали тест. Полученая оценка - НЕУДОВЛЕТВАРИТЕЛЬНО!");
}
}
}
} |