
16.11.2011, 21:00
|
Модератор
|
|
Регистрация: 17.04.2008
Сообщения: 8,096
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
|
|
можно делать по простому, можно по нормальному. во втором варианте ini-файл не катит, хотя можно и в ini.
если говорить по простому, то тебе потребуются примерно такие структуры:
Код:
type
TQuestion = record
Text : String;
Answers : Array Of String;
CorrectAnswer : Integer;
end;
TTest = record
Name : String;
Author : String;
Questions : Array Of TQuestion;
end;
Для удобства я бы добавил в секцию вопроса так же кол-во ответов на вопрос (что бы не разбирать динамически). Таким образом, код чтения и сохранения будет примерно такой:
Код:
var
FTest : TTest;
procedure LoadTest(AFileName : String);
var
ini : TIniFile;
I, J : Integer;
cQ, cA : Integer;
qName : String;
begin
FTest.Name := '';
FTest.Author := '';
SetLength(FTest.Questions,0);
If Not FileExist(AFileName) Then Exit;
ini := TIniFile.Create(AFileName);
Try
FTest.Name := Ini.ReadString('Тест','Название теста','');
FTest.Author := Ini.ReadString('Тест','Составитель теста','');
cQ := ini.ReadInteger('Тест','Количество вопросов',0);
SetLength(FTest.Questions,cQ);
For I := 0 To cQ-1 Do
Begin
qName := 'Вопрос №1'+IntToStr(I);
FTest.Questions[i].Text := ini.ReadString(qName,'Текст вопроса','');
FTest.Questions[i].CorrectAnswer := ini.ReadInteger(qName,'Правильный ответ',-1);
cA := ini.ReadInteger('Тест','Количество ответов',0);
SetLength(FTest.Questions[i].Answers,cA);
For J := 0 To cA-1 Do
FTest.Questions[i].Answers[J] := ini.ReadString(qName,'Текст ответа'+IntToStr(J),'');
End;
Finally
ini.Free;
End;
end;
procedure SaveTest(AFileName : String);
var
ini : TIniFile;
I, J : Integer;
qName : String;
begin
ini := TIniFile.Create(AFileName);
Try
Ini.WriteString('Тест','Название теста',FTest.Name);
Ini.WriteString('Тест','Составитель теста',FTest.Author);
ini.WriteInteger('Тест','Количество вопросов',Length(FTest.Questions));
For I := Low(FTest.Questions) To High(FTest.Questions) Do
Begin
qName := 'Вопрос №1'+IntToStr(I);
ini.WriteString(qName,'Текст вопроса',FTest.Questions[i].Text);
ini.WriteInteger(qName,'Правильный ответ',FTest.Questions[i].CorrectAnswer);
ini.WriteInteger('Тест','Количество ответов',Length(FTest.Questions[i].Answers));
For J := Low(FTest.Questions[i].Answers) To High(FTest.Questions[i].Answers) Do
ini.WriteString(qName,'Текст ответа'+IntToStr(J),FTest.Questions[i].Answers[J]);
End;
Finally
ini.Free;
End;
end;
ну а уж визуальную работу с этой структурой ты сам сделаешь, там уже не сложно.
|