Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [Delphi] Программа для тестирования


Автор: ckazu 5.4.2007, 14:57
Привет всем.
Передо мной поставили задачу написать тест состоящий из 100 вопросов, программа должна читать вопросы из текстового файла (1.txt) 
Код

1. Сколько вам лет?
2. Как вас зовут?
3. Откуда родом?
........................
100. Какая сейчас погода?
 
Затем сопоставлять их с другим файлом (2.txt) это ответы
Код

1. 1
2. 2
3. 3
..................
4. 1

Тестируемому должно выпадать на угад 30 вопросов из 100, как это реальзовать?

Автор: comtat 6.4.2007, 23:18
Цитата(ckazu @  5.4.2007,  14:57 Найти цитируемый пост)
Тестируемому должно выпадать на угад 30 вопросов из 100, как это реальзовать? 

В твоем вопросе и кроется ответ.. а именно юзай random
чтобы один и тот же вопрос не повторялся помечай уже заданные 
ну вот в принципе и все 

Автор: ckazu 7.4.2007, 12:24
С этим я разобрался, появилась новая проблемка, заключается в том что если я выбираю кол-во вопросов больше 10 то при ответе на вопросы программа стопорится, если вопросов 9 то нормально работает, вот код:
Код

unit Unit6;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons, Grids, DBGrids, XPMan;

type
  TForm6 = class(TForm)
    Memo1: TMemo;
    SpeedButton1: TSpeedButton;
    Memo2: TMemo;
    Memo3: TMemo;
    Memo4: TMemo;
    Memo5: TMemo;
    Memo6: TMemo;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Shape1: TShape;
    Label3: TLabel;
    Timer2: TTimer;
    Timer3: TTimer;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    XPManifest1: TXPManifest;
    procedure FormShow(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Timer3Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form6: TForm6;
  q: array[0..100] of String;
  a: array[0..100] of String;
  answer: string;
  cnt, cnt1, ball, tmr, caseof: integer;

implementation

uses Unit1, Unit7, Unit8;

{$R *.dfm}

function CheckAnswer(): boolean;
begin
if Form1.Table1.Locate('Question', a[cnt1], []) then
  begin
    if answer=VarToStr(Form1.Table1.FieldByName('r_answer').Value) then Result:=true
    else Result:=false;
  end;
end;

procedure NextQuestion(cnt1: integer);
begin
if (cnt1)=StrToInt(Form7.ComboBox2.Text) then
      begin
        Form6.SpeedButton1.Enabled:=false;
        Form6.SpeedButton2.Enabled:=false;
        Form6.SpeedButton3.Enabled:=false;
        Form6.SpeedButton4.Enabled:=false;
        Form6.SpeedButton5.Enabled:=false;
        Form6.Timer1.Enabled:=false;
        Form6.Timer3.Enabled:=true;
      end
else
begin
Form6.Timer1.Enabled:=false;
Form1.Table1.Open;
if IntToStr(cnt1+1)<=Form7.ComboBox2.Text then
begin
if Form1.Table1.Locate('Question', a[cnt1], []) then
begin
if VarToStr(Form1.Table1.FieldByName('N').Value)='3' then
  begin
    if Form6.Timer1.Tag=0 then tmr:=tmr+StrToInt(VarToStr(Form1.Table1.FieldByName('Time').Value))
    else tmr:=tmr+(StrToInt(VarToStr(Form1.Table1.FieldByName('Time').Value))-Form6.Timer1.Tag);
    Form6.Timer1.Tag:=StrToInt(VarToStr(Form1.Table1.FieldByName('Time').Value));
    Form6.Memo1.Text:=VarToStr(Form1.Table1.FieldByName('Question').Value);
    Form6.Memo2.Text:=VarToStr(Form1.Table1.FieldByName('1_answer').Value);
    Form6.Memo3.Text:=VarToStr(Form1.Table1.FieldByName('2_answer').Value);
    Form6.Memo4.Text:=VarToStr(Form1.Table1.FieldByName('3_answer').Value);
    Form6.Memo5.Enabled:=false;
    Form6.Memo6.Enabled:=false;
    Form6.SpeedButton1.Enabled:=true;
    Form6.SpeedButton2.Enabled:=true;
    Form6.SpeedButton3.Enabled:=true;
    Form6.SpeedButton4.Enabled:=false;
    Form6.SpeedButton5.Enabled:=false;
    Form6.Timer1.Enabled:=true;
    end
else
  begin
    Form6.Memo5.Enabled:=true;
    Form6.Memo6.Enabled:=true;
    Form6.SpeedButton4.Enabled:=true;
    Form6.SpeedButton5.Enabled:=true;
    if Form6.Timer1.Tag=0 then tmr:=tmr+StrToInt(VarToStr(Form1.Table1.FieldByName('Time').Value))
    else tmr:=tmr+(StrToInt(VarToStr(Form1.Table1.FieldByName('Time').Value))-Form6.Timer1.Tag);
    Form6.Timer1.Tag:=StrToInt(VarToStr(Form1.Table1.FieldByName('Time').Value));
    Form6.Memo1.Text:=VarToStr(Form1.Table1.FieldByName('Question').Value);
    Form6.Memo2.Text:=VarToStr(Form1.Table1.FieldByName('1_answer').Value);
    Form6.Memo3.Text:=VarToStr(Form1.Table1.FieldByName('2_answer').Value);
    Form6.Memo4.Text:=VarToStr(Form1.Table1.FieldByName('3_answer').Value);
    Form6.Memo5.Text:=VarToStr(Form1.Table1.FieldByName('4_answer').Value);
    Form6.Memo6.Text:=VarToStr(Form1.Table1.FieldByName('5_answer').Value);
    Form6.SpeedButton1.Enabled:=true;
    Form6.SpeedButton2.Enabled:=true;
    Form6.SpeedButton3.Enabled:=true;
    Form6.SpeedButton4.Enabled:=true;
    Form6.SpeedButton5.Enabled:=true;
    Form6.Timer1.Enabled:=true;
  end;
end
else ShowMessage('Error');
end
else Form6.Timer3.Enabled:=true;
end;
end;

procedure TForm6.FormShow(Sender: TObject);
var
  i, j, b, c, k, n: integer;
  ok: boolean;
begin
Form1.Table1.Open;
for i:=0 to Form1.Table1.RecordCount do
  begin
    Form1.Table1.Open;
    Form1.Table1.RecNo:=i;
    if VarToStr(Form1.Table1.FieldByName('Theme').Value)=Form7.ComboBox1.Text then
      begin
        q[cnt]:=VarToStr(Form1.Table1.FieldByName('Question').Value);
        cnt:=cnt+1;
      end;
  end;
k:=0;
n:=StrToInt(Form7.ComboBox2.Text);
while k<n do
  begin
    Randomize;
    c:=random(cnt);
    ok:=false;
    for b:=0 to n do
      begin
        if a[b]=q[c] then ok:=true;
      end;
    if not ok then
      begin
        a[k]:=q[c];
        k:=k+1;
      end;
  end;
NextQuestion(cnt1);
end;

procedure TForm6.Timer1Timer(Sender: TObject);
var
  i: integer;
begin
Timer1.Tag:=Timer1.Tag-1;
if Timer1.Tag=0 then
  begin
    cnt1:=cnt1+1;
    NextQuestion(cnt1);
    Refresh;
  end;
Label2.Caption:=IntToStr(Timer1.Tag);
Form6.Refresh;
end;

procedure TForm6.SpeedButton1Click(Sender: TObject);
begin
answer:=Memo2.Text;
if CheckAnswer then
  begin
    ball:=ball+1;
    Shape1.Brush.Color:=clLime;
    Label3.Caption:='Ïðàâèëüíî';
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end
else
  begin
    Form6.Shape1.Brush.Color:=clRed;
    Form6.Label3.Caption:='Íåïðàâèëüíî';
    Beep;
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end;
end;

procedure TForm6.SpeedButton2Click(Sender: TObject);
begin
answer:=Memo3.Text;
if CheckAnswer then
  begin
    ball:=ball+1;
    Shape1.Brush.Color:=clLime;
    Label3.Caption:='Ïðàâèëüíî';
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end
else
  begin
    Form6.Shape1.Brush.Color:=clRed;
    Form6.Label3.Caption:='Íåïðàâèëüíî';
    Beep;
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end;
end;

procedure TForm6.SpeedButton3Click(Sender: TObject);
begin
answer:=Memo4.Text;
if CheckAnswer then
  begin
    ball:=ball+1;
    Shape1.Brush.Color:=clLime;
    Label3.Caption:='Ïðàâèëüíî';
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end
else
  begin
    Form6.Shape1.Brush.Color:=clRed;
    Form6.Label3.Caption:='Íåïðàâèëüíî';
    Beep;
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end;
end;

procedure TForm6.SpeedButton4Click(Sender: TObject);
begin
answer:=Memo5.Text;
if CheckAnswer then
  begin
    ball:=ball+1;
    Shape1.Brush.Color:=clLime;
    Label3.Caption:='Ïðàâèëüíî';
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end
else
  begin
    Form6.Shape1.Brush.Color:=clRed;
    Form6.Label3.Caption:='Íåïðàâèëüíî';
    Beep;
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end;
end;

procedure TForm6.SpeedButton5Click(Sender: TObject);
begin
answer:=Memo6.Text;
if CheckAnswer then
  begin
    ball:=ball+1;
    Shape1.Brush.Color:=clLime;
    Label3.Caption:='Ïðàâèëüíî';
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end
else
  begin
    Form6.Shape1.Brush.Color:=clRed;
    Form6.Label3.Caption:='Íåïðàâèëüíî';
    Beep;
    cnt1:=cnt1+1;
    Timer2.Enabled:=true;
    NextQuestion(cnt1);
  end;
end;

procedure TForm6.Timer2Timer(Sender: TObject);
begin
Form6.Label3.Caption:='îæèäàíèå:';
Form6.Shape1.Brush.Color:=clSkyBlue;
Timer2.Enabled:=false;
end;

procedure TForm6.FormHide(Sender: TObject);
begin
Memo1.Clear;
Memo2.Clear;
Memo3.Clear;
Memo4.Clear;
Memo5.Clear;
Memo6.Clear;
end;

procedure TForm6.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FormHide(Sender);
end;

procedure TForm6.Timer3Timer(Sender: TObject);
begin
if (cnt1)=StrToInt(Form7.ComboBox2.Text) then
  begin
    Form6.Hide;
    Form8.Show;
    Form6.Timer3.Enabled:=false;
  end;
end;

end.

Автор: comtat 8.4.2007, 11:36
ckazu, выложи в виде исходников, так проще будет..

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)