關於成績名次排序的撰寫 |
尚未結案
|
gigitahoo
一般會員 ![]() ![]() 發表:8 回覆:2 積分:2 註冊:2004-09-16 發送簡訊給我 |
各位前輩:
關於成績名次排序的撰寫..不知該判斷要如何撰寫。。
小女子一直RUN不出來..麻請各位前輩幫忙解答!TKS~~
程式碼如下: unit hw; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1;
a:array of integer;
stdnum:integer;
implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject);
var
i,j,x,y,z:integer;
begin
button1.Caption:='開始計算';
button2.Caption:='End(&X)';
stdnum:=Strtoint(inputbox('輸入成績人數','輸入人數','5'));
with stringgrid1 do
begin
colcount:=8;
rowcount:=stdnum+1;
Cells[0,0]:='座號';
Cells[1,0]:='國文';
Cells[2,0]:='英文';
Cells[3,0]:='數學';
Cells[4,0]:='總分';
Cells[5,0]:='平均';
Cells[6,0]:='不及格科數';
Cells[7,0]:='排名'; for i:=1 to stdnum do
begin
stringgrid1.Cells[0,i]:=inttostr(i);
x:=Strtoint(inputbox('請輸入第'+inttostr(i)+'位的國文成績','請輸入','60'));
stringgrid1.Cells[1,i]:=inttostr(x);
y:=Strtoint(inputbox('請輸入第'+inttostr(i)+'位的英文成績','請輸入','60'));
stringgrid1.Cells[2,i]:=inttostr(y);
z:=Strtoint(inputbox('請輸入第'+inttostr(i)+'位的數學成績','請輸入','60'));
stringgrid1.Cells[3,i]:=inttostr(z);
stringgrid1.Cells[4,i]:=inttostr(0);
stringgrid1.Cells[5,i]:=inttostr(0);
stringgrid1.Cells[6,i]:=inttostr(0);
stringgrid1.Cells[7,i]:=inttostr(0);
end;
end;
end; procedure TForm1.Button1Click(Sender: TObject);
var
j,c,k,l,w:integer;
begin
//計算每位總成績
w:=0;
for j:=1 to stdnum do
begin
for k:=1 to 3 do
begin
//strtoint(stringgrid1.Cells[4,j]):=strtoint(stringgrid1.Cells[4,j])strtoint(stringgrid1.Cells[k,j]);
//stringgrid1.Cells[4,j]:=inttostr(stringgrid1.Cells[4,j]);
w:=w+strtoint(stringgrid1.Cells[k,j]);
stringgrid1.Cells[4,j]:=inttostr(w);
end;
w:=0;
end;
//計算每位平均
for l:=1 to stdnum do
begin
w:=strtoint(stringgrid1.Cells[4,l]) div 3;
stringgrid1.Cells[5,l]:=inttostr(w);
end; //統計不及格科數
//for j:=1 to stdnum do
//begin
//for l:=1 to 3 do
//begin
//if strtoint(stringgrid1.Cells[j,l])<60 then
// begin
//w:=strtoint(stringgrid1.Cells[6,j]) 1;
//stringgrid1.cells[6,j]:=inttostr(w);
//end;
//end;
//end;
//for j:=1 to stdnum do
//begin
end; procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end; end.
|
likush
高階會員 ![]() ![]() ![]() ![]() 發表:5 回覆:235 積分:103 註冊:2002-10-08 發送簡訊給我 |
個人爛方法 供參考 有錯請指正
Procedure TForm1.FormCreate(Sender: TObject);
Var
vlist: TstringList;
i: integer;
Begin
button1.Caption := '開始計算';
button2.Caption := 'End(&X)';
stdnum := Strtoint(inputbox('輸入成績人數', '輸入人數', '5'));
With stringgrid1 Do
Begin
colcount := 8;
rowcount := stdnum 1;
setlength(vA, stdnum 1);
vA[0].v0 := '座號';
vA[0].v1 := '國文';
vA[0].v2 := '英文';
vA[0].v3 := '數學';
vA[0].v4 := '總分';
vA[0].v5 := '平均';
vA[0].v6 := '不及格科數';
vA[0].v7 := '排名';
For i := 1 To stdnum Do
Begin
vA[I].v0 := inttostr(I);
vA[I].v1 := inttostr(40 I);
vA[I].v2 := inttostr(50 I);
vA[I].v3 := inttostr(50 I);
vA[I].v4 := inttostr(strtoint(vA[I].v1) strtoint(vA[I].v2) strtoint(vA[I].v3));
vA[I].v5 := inttostr(strtoint(vA[I].v4) Div 3);
vA[I].v6 := inttostr(0);
vA[I].v7 := inttostr(0);
End;
vlist := TstringList.Create;
Try
For i := low(vA) 1 To high(vA) Do
vList.Add(vA[I].v5);
vlist.Sort;
For I := low(vA) To High(vA) Do
Begin
Cells[0, i] := vA[I].v0;
Cells[1, i] := vA[I].v1;
If I <> 0 Then
Begin
If (strtoint(vA[I].v1) < 60) Then vA[I].v6 := IntToStr(StrToInt(vA[I].v6) 1);
If (strtoint(vA[I].v2) < 60) Then vA[I].v6 := IntToStr(StrToInt(vA[I].v6) 1);
If (strtoint(vA[I].v3) < 60) Then vA[I].v6 := IntToStr(StrToInt(vA[I].v6) 1);
End;
Cells[2, i] := vA[I].v2;
Cells[3, i] := vA[I].v3;
Cells[4, i] := vA[I].v4;
Cells[5, i] := vA[I].v5;
Cells[6, i] := vA[I].v6;
If I = 0 Then
Cells[7, i] := vA[I].v7
Else //排序部份則由小至大 若相反則自行修改
Cells[7, i] := IntToStr(vlist.IndexOf(vA[I].v5) 1);
End;
Finally
vlist.Free;
End;
End;
End; =========================
讀萬卷書~不如來K.TOP走一遭
=========================
|
00156
高階會員 ![]() ![]() ![]() ![]() 發表:45 回覆:195 積分:112 註冊:2002-06-01 發送簡訊給我 |
試試下面的函數吧...
先加入宣告: private procedure SortByCol(SG:TStringGrid; index:integer); 實作裡加入: procedure TForm1.SortByCol(SG:TStringGrid; index:integer); var i,j:integer; procedure swap(a,b:integer); var temp:string; begin temp:=SG.Rows[a].CommaText; SG.Rows[a].CommaText:=SG.Rows[b].CommaText; SG.Rows[b].CommaText:=temp; end; begin for i:=0 to SG.RowCount-2 do for j:= i 1 to SG.RowCount-1 do if StrToIntDef(SG.Cells[index,i],0)如果有標題列,將紅色的部份改為1(第0列不排序);如果要多欄比較,再修改藍色的部份的條件。妳參考看看吧! |
Fishman
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi gigitahoo, 懶方法,用 ClientDateSet 搭配 DBGrid,排序交給 ClientDataSet 負責(設定 IndexDefs 及 IndexName),範例如下:
http://delphi.ktop.com.tw/loadfile.php?TOPICID=17999078&CC=402542
------
Fishman |
gigitahoo
一般會員 ![]() ![]() 發表:8 回覆:2 積分:2 註冊:2004-09-16 發送簡訊給我 |
Fishman 您好: 謝謝您的資料..可是小女子才是初學者.你的語法太專業了.有的我都看不懂耶..
所以我的語法是老師上課教的....
老師主要要求我們一執行後必須是show出如最下方的那個方框...
會讓你輸入5個人的成績...輸入完後按計算button會出現名次的排序..
在麻煩知道怎麼寫排序的前輩幫小女子解答!感激不盡.... 程式碼如下: unit hw; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1;
a:array of integer;
stdnum:integer;
implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject);
var
i,j,x,y,z:integer;
begin
button1.Caption:='開始計算';
button2.Caption:='End(&X)';
stdnum:=Strtoint(inputbox('輸入成績人數','輸入人數','5'));
with stringgrid1 do
begin
colcount:=8;
rowcount:=stdnum+1;
Cells[0,0]:='座號';
Cells[1,0]:='國文';
Cells[2,0]:='英文';
Cells[3,0]:='數學';
Cells[4,0]:='總分';
Cells[5,0]:='平均';
Cells[6,0]:='不及格科數';
Cells[7,0]:='排名'; for i:=1 to stdnum do
begin
stringgrid1.Cells[0,i]:=inttostr(i);
x:=Strtoint(inputbox('請輸入第'+inttostr(i)+'位的國文成績','請輸入','60'));
stringgrid1.Cells[1,i]:=inttostr(x);
y:=Strtoint(inputbox('請輸入第'+inttostr(i)+'位的英文成績','請輸入','60'));
stringgrid1.Cells[2,i]:=inttostr(y);
z:=Strtoint(inputbox('請輸入第'+inttostr(i)+'位的數學成績','請輸入','60'));
stringgrid1.Cells[3,i]:=inttostr(z);
stringgrid1.Cells[4,i]:=inttostr(0);
stringgrid1.Cells[5,i]:=inttostr(0);
stringgrid1.Cells[6,i]:=inttostr(0);
stringgrid1.Cells[7,i]:=inttostr(0);
end;
end;
end; procedure TForm1.Button1Click(Sender: TObject);
var
j,c,k,l,w:integer;
begin
//計算每位總成績
w:=0;
for j:=1 to stdnum do
begin
for k:=1 to 3 do
begin
//strtoint(stringgrid1.Cells[4,j]):=strtoint(stringgrid1.Cells[4,j])strtoint(stringgrid1.Cells[k,j]);
//stringgrid1.Cells[4,j]:=inttostr(stringgrid1.Cells[4,j]);
w:=w+strtoint(stringgrid1.Cells[k,j]);
stringgrid1.Cells[4,j]:=inttostr(w);
end;
w:=0;
end;
//計算每位平均
for l:=1 to stdnum do
begin
w:=strtoint(stringgrid1.Cells[4,l]) div 3;
stringgrid1.Cells[5,l]:=inttostr(w);
end; //統計不及格科數
//for j:=1 to stdnum do
//begin
//for l:=1 to 3 do
//begin
//if strtoint(stringgrid1.Cells[j,l])<60 then
// begin
//w:=strtoint(stringgrid1.Cells[6,j])+1;
//stringgrid1.cells[6,j]:=inttostr(w);
//end;
//end;
//end;
//for j:=1 to stdnum do
//begin
end; procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end; end.
|
Fishman
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi gigitahoo, 改一下就行啦!
http://delphi.ktop.com.tw/loadfile.php?TOPICID=18002521&CC=402619
------
Fishman |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |