Листинг 15.2. Программа тестирования, версия 2
001.
unit
test2_;
002.
interface
003.
uses
004.
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
005.
Forms, Dialogs, StdCtrls, ExtCtrls;
006.
type
007.
TForml =
class
(TFonn)
008.
LabelS: TLabel;
// поле вывода вопроса
009.
Image1: Tlmage;
// область вывода иллюстрации
010.
Panel1: TPanel;
011.
Buttonl: TButton; /'/ кнопка Ok, Дальше, Завершить
012.
procedure
FormActivate(Sender: TObject);
013.
procedure
FomCreate (Sender: TObject];
014.
procedure
ButtonlClicklSender: TObject];
015.
procedure
SelectorClick(Sender: TObject);
016.
private
017.
{ Private declarations }
018.
public
019.
{ Public declarations }
020.
end
;
021.
var
022.
023.
Forml: TForml;
// форма
024.
implementation
025.
const
026.
N__flNSWERS=
4
;
// четыре варианта ответов
027.
N_LEVEL=
4
;
// четыре уровня оценки
028.
var
029.
// динамически создаваемые компоненты
030.
answer:
array
[
1..
M_ANSWERS]
of
TLabel;
// альтернативные ответы
031.
selector:
array
[l
..
N_ANSWERS+
1
]
of
TRadioButton;
// кнопки выбора ответа
032.
f:TextFile;
033.
fn:
string
;
// имя файла вопросов
034.
level:
array
[l
..
N___LEVEL]
of
integer
;
// сумма, соответствующая уровню
035.
mes:
array
[l
..
N_LEVEL]
of
string
;
// сообщение, соответствующее уровню
036.
score:
array
[
1..
N_ANSWERS]
of
integer
;
// очки за выбор ответа
037.
summa:
integer
;
// набрано очков
038.
vopros:
integer
;
// номер текущего вопроса
039.
n_otv:
integer
;
// число вариантов ответа
040.
otv:
integer
;
// номер выбранного ответа
041.
// установка формы в исходное состояние
042.
Procedure ResetForm(frm:TForml);
043.
var
044.
i:
integer
;
045.
begin
046.
for
i:*l
to
N_ANSWERS
do
047.
begin
048.
answer.width:=frm
.
ClientWidth-answer.left-
5
;
049.
answer.Visible:-FAL5E;
050.
Selector .Visible:=FALSE,-
051.
end
;
052.
frm
.
LabelS- width:=frm
.
ClientWidth-frm
.
Labels
.
left-
5
;
053.
irm
.
Imagel
.
Visible:=False;
054.
end
;
055.
// определение достигнутого уровня
056.
procedure
Itog(suirana:
integer
;frm:TForml);
057.
var
058.
i:
integer
;
059.
buf:
string
;
060.
begin
061.
buf:=";
062.
str(surma:
5
, buf);
063.
buf:=
'Результаты тестирования'
+chr(
13
)+
'Всего баллов: '
-t-buf;
064.
i:=l;
065.
while
(sumna < level[ij)
and
(i<N_LEVEL|
do
066.
inc(i);
067.
buf:=buf+chr(
13
)+mes;
068.
frm
.
Labels
.
caption:=buf;
069.
end
;
070.
071.
procedure
TForml
.
FormCreate[Sender: TObj ect);
072.
var
073.
i:
integer
;
074.
begin
075.
// создадим пять меток для вывода вопроса и альтернативных ответов
076.
for
i:=l
to
M_ANSWERS
do
077.
begin
078.
answer:=TLabel
.
Create(self);
079.
answerfi].Parent:=Forml;
080.
answer.Left:=
36
;
081.
answer.Wordwrap:=True;
082.
end
;
083.
// создадим переключатели для выбора ответа
084.
for
i:=l
to
N__ANSWERS-H
do
085.
begin
086.
selector:^TRactioButton
.
Create(self);
087.
selector.Parent:-self;
088.
selector Ii].Caption: =
''
;
089.
selector-Width:=
17
;
090.
091.
selector.Left:=
16
;
092.
selector.Visible:=False;
093.
selector!!].Enabled:=True;
094.
selector,onclick:=SelectorClick;
095.
end
;
096.
ResetForm(Forml);
097.
end
;
098.
// вывол начальной информации о гесте
099.
procedure
info(
var
f:TextFile;l:TLabel);
100.
var
101.
s,buf:
string
;
102.
begin
103.
buf: = ";
104.
repeat
105.
readln(f, s);
106.
if
s[l) <>
'- '
107.
then
buf:=buf+s+
' '
;
108.
until
s[l]:=
'.'
;
109.
Forml
.
Labels
.
caption:^buf;
110.
end
;
111.
// прочитать информацию об оценках за тест
112.
Procedure GetLevellvar f:TextFile>;
113.
var
114.
i:
integer
;
115.
buf :
string
;
116.
begin
// заполняем значения глобальных массивов
117.
i:=l;
118.
repeat
119.
readlri (f ,buf ) ;
120.
if
buf [
1
] <>
'.'
then
begin
121.
mes :=buf ;
122.
readln(f,level);
123.
inc(i);
124.
125.
end
;
126.
until
buf[l]:=
'.'
;
127.
128.
end
,-
129.
// масштабирование иллюстрации
130.
Procedure SealePicture;
131.
var
132.
w,h:
integer
;
// максимально допустимые размеры картинки
133.
scaleXrreal;
// коэф. масштабирования по X
134.
scaleY:
real
;
// коэф. масштабирования no Y
135.
scale:
real
;
// общий коэф. масштабирования
136.
i:
integer
;
137.
begin
138.
// вычислить максимально допустимые размеры картинки
139.
w:-Forml
.
ClientWidth-Forml
.
LabelS
.
Left;
140.
h:=Forml
.
ClientHeight
141.
Forml
.
Panell
.
Height -
5
142.
Forml
.
Labels
.
Top
143.
Forml
.
Labels
.
Height —
5
;
144.
for
i:=l
to
N_ANSWERS
do
145.
if
answer.Caption <> •'
146.
then
hi=h-answer.Height-
5
;
147.
// здесь определена максимально допустимая величина иллюстрации
148.
// определить масштаб
149.
if
w>Forml. Imagel
.
Picture
.
Width
150.
then
scaleX:=l
151.
else
scaleX:=w/Forml
.
Imagel
.
Picture
.
Width;
152.
if
h>Forml
.
Imagel
.
Picture
.
Height
153.
then
scaleY:=l
154.
else
scaleY:=h/Forml
.
Imagel
.
Picture
.
Height;
155.
if
ScaleY<ScaleX
156.
then
scale:=scaleY
157.
else
scale:-scaleX;
158.
// здесь масштаб определен
159.
Forml. Imagel
.
Top-.^Forml
.
Labels
.
Top+Forml
.
Labels
.
Height+
5
;
160.
Forml
.
Imagel
.
Left:=Forml
.
Label5
.
Left;
161.
Forml
.
Imagel
.
Width:=Round(Forml
.
Imagel
.
Picture
.
Width'scale);
162.
Forml
.
Imagel
.
Height:-Round(Forml
.
Imagel
.
Picture
.
Height"scale);
163.
Forml
.
Labels
.
Visible:-TRUE;
164.
165.
end
;
166.
// вывод вопроса на экран
167.
Procedure VoprosToScr (
var
f :TextFile; frm:TForml;
var
vopros:
integer
! ;
168.
var
169.
i:
integer
;
170.
code:
integer
;
171.
s,buf:
string
;
172.
ifn:3tring;
// файл иллюстрации
173.
begin
174.
vopros : =vopros+l ;
175.
str (vopro5:
3
,s] ;
176.
frm
.
captioiK^
'Bonpoc'
+ s;
177.
// выведем текст вопроса
178.
buf: = ",-
179.
repeat
180.
readln(f,s] ;
181.
if
|s[l] <>
'. '
]
and
( g [ l ]
0
' \ '
)
182.
then
buf :=buf+s-t-
' '
;
183.
until
[s[l] =
' - '
)
or
(s[l] -
'\'
l;
184.
f rm. Labels. capt ion r=buf;
185.
if
s[l] -
'\'
186.
then
// к вопросу есть иллюстрация
187.
beg-
in
188.
frm. Imagel. Tag:=l;
189.
ifn:=copy(s,
2
,length(s) ) ;
190.
try
191.
frm. Imagel . Picture . LoadFromFile (
if
n) ;
192.
except
193.
on
E:EFOpenError
do
194.
frm. tag : =
0
;
195.
end
// try
196.
end
197.
else
frm. Imagel. Tag:=
0
;
198.
// читаем варианты ответов
199.
200.
foe i:=l
to
N_AHSWERS
do
201.
begin
202.
answer.caption:^
''
;
203.
answer.Width:=frm
.
ClientWidth-Forml
.
Labels
.
Left-
5
;
204.
end
;
205.
i:=l;
206.
repeat
207.
buf: = ";
208.
repeat
// читаем текст варианта ответа
209.
readln(f,s);
210.
if
( s [ l ] < >
' . '
)
and
(s[l]
0
' , '
)
211.
than buf:=buf+s+
' '
;
212.
until
( s [ l ] =
' , '
) o r ( s [ l ] =
' . '
) ;
213.
// прочитан альтернативный ответ
214.
val
(s[
2
],score,code);
215.
answer.caption:=buf;
216.
ii-i-Hj
217.
until
s [ l ] =
' . '
;
218.
// здесь прочитана иллюстрация и альтернативные ответы
219.
if
Forml
.
Imagel
.
Tag =
1
// есть иллюстрация к вопросу?
220.
then
begin
221.
ScalePicture;
222.
Fonol
.
Imagel
.
Visible:=TRUE;
223.
end
;
224.
// вывод альтернативных ответов
225.
i:=ll
226.
while
[answerji].caption <> ")
and
(i <- M_flNSWERS)
do
227.
begin
228.
if
i -
1
229.
then
230.
if
frni
.
Imagel
.
Tag =
1
231.
then
answer[
1
].top:=frm. Imagel
.
Top+frm. Imagel
.
Height+
5
232.
else
answer.top:=frm
.
Label5
.
Top+frm
.
Labels
.
Height+
5
233.
else
234.
235.
answer.top:=answer[i-
1
].top+ answer[i-l].height+
5
;
236.
selector-top:=answer.top;
237.
selector!!].visible:=TROE;
238.
answerti],visible:=TRUE;
239.
i:=i+l;
240.
end
;
241.
end
;
242.
)
$R
*.DFM}
243.
procedure
TForml
.
FormActivate(Senderг TObject);
244.
begin
245.
ResetForm(Forml);
246.
if
ParamCount =
0
247.
then
248.
begin
249.
Label5
.
font
.
color:=clRed;
250.
Labels
.
caption:=
'He задан файл вопросов геста.'
;
251.
Buttonl
.
caption:=
'Ok'
;
252.
Buttonl
.
tag:=
2
;
253.
Buttonl- Enabled:=TRUE
254.
end
255.
else
begin
256.
fn:=FaramStr(
1
);
257.
assignfile(f
.
fn) ;
258.
{$!-}
259.
reset(fI;
260.
{I+i
261.
if
IOResult=
0
then
262.
begin
263.
Infо(f,LabelS);
264.
GetLevel(f);
265.
end
;
266.
summa:=
0
;
267.
end
;
268.
end
;
269.
procedure
TForml
.
ButtorilClick(Sender: TObject);
270.
begin
271.
272.
case
Button!.tag
of
273.
0
:
begin
274.
Buttonl
.
caption:=
'Дальше'
;
275.
Buttonl
.
tag:=l;
276.
Selector[N_ftNSWERS+l].Checked:=TRUE;
277.
// вывод первого вопроса
278.
Buttonl
.
Enabled:=False;
279.
ResetForm (Foml);
280.
VoprosToScr(f,Forml,vopros)
281.
end
;
282.
1
:
begin
// вывод остальных вопросов
283.
surama:=surnma+scoEe[otvJ ;
284.
Selector[N_ANSWERS+l].Checked:=TRUE;
285.
Buttonl
.
Enabled:-False;
286.
ResetFormlFoml] ;
287.
if
not
eof(f!
288.
then
VoprosToScr(f,Forml,vopros]
289.
else
290.
begin
291.
closefileff |;
292.
ButConl
.
caption:=
'Ok'
;
293.
Forml
.
caption:=
'Результат'
;
294.
Buttonl
.
tag:=
2
;
295.
Buttonl
.
Enabled:=TRUE;
296.
Itog(surama,Forml];
297.
end
;
298.
end
;
299.
2
:
begin
// завершение работы
300.
Forral
.
Close;
301.
end
;
302.
end
;
303.
end
;
304.
// щелчок на кнопке выбора ответа
305.
procedure
TFoml
.
SelectorClick(Sender: TObject) ;
306.
var
307.
i:
integer
;
308.
begin
309.
i:=
1
;
310.
while
selector .Checked = FALSE
do
311.
inc(i);
312.
otv:=
1
;
313.
314.
Buttonl . enabled : =*TRUE;
315.
end
;
316.
end
.
По сравнению с первым вариантом программа Тест, версия 2 обладает существенным преимуществом. Для ее модернизации, например для увеличения количества альтернативных ответов, достаточно изменить только описание именованной константы N ANSWERS.
������� ������ ��� dle ������� ��������� ������