注册 登录
编程论坛 VFP论坛

如何随机抽取试题

王咸美 发布于 2024-05-23 16:06, 585 次点击
只有本站会员才能查看附件,请 登录
现有试题库 timu.dbf,如何随机抽取5题组成word试卷。下列代码不能随机抽取试题,望高手指教,万分感谢!!!
只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录


8 回复
#2
wengjl2024-05-23 16:12
我想,思路大概是这样:
在试题库表上加一个字段,写上随机数,按随机数排序,最前5或后5,的五个题,到试卷
#3
王咸美2024-05-23 16:15
具体代码如下:

CLOSE DATABASES
CLEAR ALL
cPath=ADDBS(JUSTPATH(SYS(16)))
SET DEFAULT TO (cPath)
 
* 随机生成5个题目到EXCEL

USE timu IN 0 ALIAS timu
SELECT timu
replace isc WITH '' all
replace sjs WITH RAND()*1000 all
xt= 5
filecnt=INT(RECCOUNT()/xt)
SET ORDER TO sjs
GO top
FOR i=1 to filecnt
replace isc WITH ALLTRIM(STR(i)) NEXT xt
SKIP
ENDFOR
replace isc WITH ALLTRIM(STR(i)) FOR EMPTY(isc)
filepath="E:\temp1\"
eole=CREATEOBJECT('Excel.Application')
*!* eloe.visible=.t.
*!* 生成 EXCEL
FOR j=1 TO i
 SELECT * FROM timu WHERE isc== ALLTRIM(STR(j)) INTO CURSOR temp
 IF _tally=0
  RETURN
 ENDIF
 eole.workbooks.Add
 eole.worksheets("Sheet1").Activate
 lanmu()
 excelpath=filepath+"随机选题第"+ALLTRIM(STR(j))+"套.xls"
 ? '正在生成:'+excelpath
 SELECT temp
 k=2
 SCAN
  eole.Cells(k,1)='单选题'
  eole.Cells(k,2)=ALLTRIM(tm)+ALLTRIM(tm2)
  eole.Cells(k,3)=ALLTRIM(xxa)
  eole.Cells(k,4)=ALLTRIM(xxb)
  eole.Cells(k,5)=ALLTRIM(xxc)
  eole.Cells(k,6)=ALLTRIM(xxd)
  eole.Cells(k,8)=ALLTRIM(anwser)
  eole.Cells(k,10)='5'
  k=k+1
 ENDSCAN
 eole.ActiveWorkbook.SaveAs(excelpath)
 eole.Workbooks.close
ENDFOR
eole.Quit
RELEASE eole
clear
 
*!* 生成WORD试卷  
eole=CREATEOBJECT('word.application')
*!* eole.visible=.t.
FOR j=1 TO i
 SELECT * FROM timu WHERE isc== ALLTRIM(STR(j)) INTO CURSOR temp
 IF _tally=0
  RETURN
 ENDIF
 eole.Documents.Add
 eole.ActiveDocument.PageSetup.TopMargin=1.27/0.035
 eole.ActiveDocument.PageSetup.BottomMargin=1.27/0.035
 eole.ActiveDocument.PageSetup.LeftMargin=1.27/0.035
 eole.ActiveDocument.PageSetup.RightMargin=1.27/0.035
 wordname="随机选题第"+ALLTRIM(STR(j))+"套"
 ? '正在生成:'+wordname
 eole.Selection.TypeText(wordname)
 eole.Selection.TypeParagraph
 SELECT temp
 k=1
 SCAN
   eole.Selection.TypeText(ALLTRIM(STR(k))+"."+ALLTRIM(tm))
   eole.Selection.TypeParagraph
   IF !EMPTY(tm2)
     eole.Selection.TypeText(ALLTRIM(tm2))
     eole.Selection.TypeParagraph
   ENDIF
   xa=ALLTRIM(xxa)
   xb=ALLTRIM(xxb)
   xc=ALLTRIM(xxc)
   xd=ALLTRIM(xxd)
   IF LEN(xa+xb+xc+xd)<80
    eole.ActiveDocument.Tables.Add(eole.Selection.Range,1,4,0,0)
    eole.Selection.Tables(1).Rows(1).Cells(1).Range.InsertAfter(xa)
    eole.Selection.Tables(1).Rows(1).Cells(2).Range.InsertAfter(xb)
    eole.Selection.Tables(1).Rows(1).Cells(3).Range.InsertAfter(xc)
    eole.Selection.Tables(1).Rows(1).Cells(4).Range.InsertAfter(xd)
   ELSE
    eole.ActiveDocument.Tables.Add(eole.Selection.Range,2,2,0,0)
    eole.Selection.Tables(1).Rows(1).Cells(1).Range.InsertAfter(xa)
    eole.Selection.Tables(1).Rows(1).Cells(2).Range.InsertAfter(xb)
    eole.Selection.Tables(1).Rows(2).Cells(1).Range.InsertAfter(xc)
    eole.Selection.Tables(1).Rows(2).Cells(2).Range.InsertAfter(xd)
    eole.Selection.MoveDown(5,1,.f.)
   ENDIF
   eole.Selection.MoveDown(5,1,.f.)
   k=k+1
   ENDSCAN
   eole.ActiveDocument.SaveAs(filepath+wordname+".doc")
   eole.ActiveDocument.close
  ENDFOR
  CLEAR
  *!* eole.visible=.t.
 
 FOR j=1 TO i
 SELECT * FROM timu WHERE isc== ALLTRIM(STR(j)) INTO CURSOR temp
 IF _tally=0
  RETURN
 ENDIF
 eole.Documents.Add
 eole.ActiveDocument.PageSetup.TopMargin=1.27/0.035
 eole.ActiveDocument.PageSetup.BottomMargin=1.27/0.035
 eole.ActiveDocument.PageSetup.LeftMargin=1.27/0.035
 eole.ActiveDocument.PageSetup.RightMargin=1.27/0.035
 wordname="随机选题第"+ALLTRIM(STR(j))+"套附答案"
 ? '正在生成:'+wordname
 eole.Selection.TypeText(wordname)
 eole.Selection.TypeParagraph
 SELECT temp
 k=1
 SCAN
   eole.Selection.TypeText(ALLTRIM(STR(k))+"."+ALLTRIM(tm))
   eole.Selection.TypeParagraph
   IF !EMPTY(tm2)
     eole.Selection.TypeText(ALLTRIM(tm2))
     eole.Selection.TypeParagraph
   ENDIF
   xa=ALLTRIM(xxa)
   xb=ALLTRIM(xxb)
   xc=ALLTRIM(xxc)
   xd=ALLTRIM(xxd)
   IF LEN(xa+xb+xc+xd)<80
    eole.ActiveDocument.Tables.Add(eole.Selection.Range,1,4,0,0)
    eole.Selection.Tables(1).Rows(1).Cells(1).Range.InsertAfter(xa)
    eole.Selection.Tables(1).Rows(1).Cells(2).Range.InsertAfter(xb)
    eole.Selection.Tables(1).Rows(1).Cells(3).Range.InsertAfter(xc)
    eole.Selection.Tables(1).Rows(1).Cells(4).Range.InsertAfter(xd)
   ELSE
    eole.ActiveDocument.Tables.Add(eole.Selection.Range,2,2,0,0)
    eole.Selection.Tables(1).Rows(1).Cells(1).Range.InsertAfter(xa)
    eole.Selection.Tables(1).Rows(1).Cells(2).Range.InsertAfter(xb)
    eole.Selection.Tables(1).Rows(2).Cells(1).Range.InsertAfter(xc)
    eole.Selection.Tables(1).Rows(2).Cells(2).Range.InsertAfter(xd)
    eole.Selection.MoveDown(5,1,.f.)
   ENDIF
   eole.Selection.MoveDown(5,1,.f.)
   eole.Selection.TypeText("答案:"+ALLTRIM(anwser))
   eole.Selection.TypeParagraph
   k=k+1
   ENDSCAN
   eole.ActiveDocument.SaveAs(filepath+wordname+".doc")
   eole.ActiveDocument.close
  ENDFOR
  eole.Quit
  RELEASE eole
  MESSAGEBOX("导出完毕")
 
  PROCEDURE lanmu
   eole.cells(1,1)='题型'
   eole.cells(1,2)='题目'
   eole.cells(1,3)='选项1'
   eole.cells(1,4)='选项2'
   eole.cells(1,5)='选项3'
   eole.cells(1,6)='选项4'
   eole.cells(1,7)='选项5'
   eole.cells(1,8)='答案'
   eole.cells(1,9)='解析'
   eole.cells(1,10)='分值'
   
     
 
   
 
 
 
 
#4
shizi02024-05-23 17:10
a=[,]
For i=1 to 5
    cNo=Transform(Int(Rand()*题库总数)+1)
    Do While cNo+[,]$a
        cNo=Transform(Int(Rand()*题库总数)+1)
    Enddo     
    ?[抽取第]+Transform(i)+[题:题库序号]+cNo
    a=a+cNo+[,]
Endfor     

[此贴子已经被作者于2024-5-23 17:16编辑过]

#5
yiyanxiyin2024-05-23 17:10
既然生成了一个随机数字段sjs, 又搞个isc是干啥的, 直接按sjs排序取前5条数据不就行了么
#6
王咸美2024-05-23 17:27
isc 表示第几套试卷
#7
王咸美2024-05-23 22:28
从timu.dbf中随机抽5题生成一套试卷,能生成几套就生成几套,运行上面的代码每次生成的试卷都一样,不知为何?请高手赐教,不胜感激!

[此贴子已经被作者于2024-5-23 22:34编辑过]

#8
吹水佬2024-05-24 06:48
回复 7楼 王咸美
调用rand()之前变动一下种子,如rand(-1)
#9
王咸美2024-05-24 09:46
谢谢!

1