一個学生成績排名的實現程序,以及Custom定制類的用法示例
程序代码:
CLEAR ALL SET TALK OFF SET SAFETY OFF CLOSE DATABASES ALL CLEAR PUBLIC gaSubjects[12] gaSubjects[1] = CREATEOBJECT("myField", "化學成績", "S01", "N", 6, 2, .T., 0) gaSubjects[2] = CREATEOBJECT("myField", "數學成績", "S02", "N", 6, 2, .T., 0) gaSubjects[3] = CREATEOBJECT("myField", "物理成績", "S03", "N", 6, 2, .T., 0) gaSubjects[4] = CREATEOBJECT("myField", "語文成績", "S04", "N", 6, 2, .T., 0) gaSubjects[5] = CREATEOBJECT("myField", "英語成績", "S05", "N", 6, 2, .T., 0) gaSubjects[6] = CREATEOBJECT("myField", "政治成績", "S06", "N", 6, 2, .T., 0) gaSubjects[7] = CREATEOBJECT("myField", "化學名次", "R01", "I") gaSubjects[8] = CREATEOBJECT("myField", "數學名次", "R02", "I") gaSubjects[9] = CREATEOBJECT("myField", "物理名次", "R03", "I") gaSubjects[10] = CREATEOBJECT("myField", "語文名次", "R04", "I") gaSubjects[11] = CREATEOBJECT("myField", "英語名次", "R05", "I") gaSubjects[12] = CREATEOBJECT("myField", "政治名次", "R06", "I") Create_DataTable(@gaSubjects) Do_Sort(@gaSubjects) CLOSE DATABASES ALL CLEAR ALL RETURN *--------------------------- * 創建數據表及測試數據 *--------------------------- PROCEDURE Create_DataTable(taSubjects) LOCAL lcDataBase, lcTable LOCAL lnIndex, lnRecord, lcField lcDataBase = "Test" lcTable = "Scores" IF !FILE(lcDataBase + ".DBC") CREATE DATABASE (lcDataBase) ELSE OPEN DATABASE (lcDataBase) ENDIF SET DATABASE TO (lcDataBase) IF INDBC(lcTable, "TABLE") DROP TABLE (lcTable) ENDIF CREATE TABLE &lcTable (ID C(10) PRIMARY KEY) SELECT (lcTable) DBSETPROP(lcTable + ".ID", "FIELD", "Caption", "學號") FOR lnIndex = 1 TO ALEN(taSubjects, 1) taSubjects[lnIndex].Add_Column NEXT RAND(-1) FOR lnRecord = 1 TO 1000 INSERT INTO &lcTable (ID) VALUES (TRANSFORM(lnRecord, "@L" + REPLICATE("9", 10))) FOR lnIndex = 1 TO ALEN(taSubjects, 1) / 2 lcField = taSubjects[lnIndex].Title REPLACE &lcField WITH RAND() * 100 NEXT NEXT USE IN "Scores" SET DATABASE TO "Test" CLOSE DATABASES ENDPROC PROCEDURE Do_Sort(taSubjects) LOCAL lnIndex, lcScoreField, lcRankField, lnRank OPEN DATABASE "Test" USE "Scores" IN 0 SELECT "Scores" FOR lnIndex = 1 TO ALEN(taSubjects, 1) / 2 lcScoreField = taSubjects[lnIndex].Title lcRankField = taSubjects[lnIndex + ALEN(taSubjects, 1) / 2].Title SET ORDER TO TAG &lcScoreField GOTO TOP lnRank = 0 SCAN ALL lnRank = lnRank + 1 REPLACE &lcRankField WITH lnRank ENDSCAN NEXT SET ORDER TO TAG ID GOTO TOP BROWSE USE IN "Scores" SET DATABASE TO "Test" CLOSE DATABASES ENDPROC DEFINE CLASS myField AS Custom Caption = "" && 標題 Title = "" && 字段名 Type = "" && 字段類型 Width = 0 && 字段寬度 Precision = 0 && 字段小數點位數 IsIndex = .F. && 是否索引字段 Ascend = 1 && 排序方向(1:升序,0:降序) PROCEDURE Init(tcCaption, tcName, tcType, tnWidth, tnPrecision, tlIndex, tnAscend) WITH This .Caption = tcCaption .Title = tcName .Type = UPPER(tcType) .Width = IIF(!EMPTY(tnWidth), tnWidth, 0) .Precision = IIF(!EMPTY(tnPrecision), tnPrecision, 0) IF VARTYPE(tlIndex) == "L" .IsIndex = tlIndex ENDIF IF VARTYPE(tnAscend) == "N" .Ascend = IIF(tnAscend != 0, 1, 0) ENDIF ENDWITH ENDPROC *------------------------ * 將字段添加到指定別名的表結構中 *------------------------ PROCEDURE Add_Column() LOCAL lcDBF, lcString, lcTag lcDBF = JUSTSTEM(DBF()) WITH This IF TYPE(.Name) == "U" lcString = .Title + " " + .Type DO CASE CASE INLIST(UPPER(.Type), "C", "Q", "V") lcString = lcString + "(" + TRANSFORM(.Width) + ")" CASE INLIST(UPPER(.Type), "N", "F") lcString = lcString + "(" + TRANSFORM(.Width) + "," + TRANSFORM(.Precision) + ")" CASE UPPER(.Type) == "B" lcString = lcString + "(" + TRANSFORM(.Precision) + ")" ENDCASE ALTER TABLE &lcDBF ADD COLUMN &lcString DBSETPROP(lcDBF + "." + .Title, "FIELD", "Caption", .Caption) IF .IsIndex lcTag = .Title IF .Ascend == 1 INDEX ON &lcTag TAG &lcTag COLLATE "MACHINE" ASCENDING ELSE INDEX ON &lcTag TAG &lcTag COLLATE "MACHINE" DESCENDING ENDIF ENDIF ENDIF ENDWITH ENDPROC ENDDEFINE