vb求素数程序_第1页
vb求素数程序_第2页
vb求素数程序_第3页
vb求素数程序_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、需要在窗体放置4个控件,不用设置控件任何属性:Command1,Command2,List1,Label1Dim ctExit As Boolean, ctStop As BooleanDim ctCiFind As Long, ctCiAdd As LongPrivate Sub Form_Load() Me.Caption = 查找 N 以内的所有素数 Label1.Caption = Me.Caption: Label1.AutoSize = True Command1.Caption = 查找: Command2.Caption = 取消End SubPrivate Sub Form_

2、Resize() Dim S As Long On Error Resume Next S = Me.TextWidth(A) Command1.Move S, S, S * 8, S * 3 Command2.Move S * 10, S, S * 8, S * 3 Label1.Move S, Me.ScaleHeight - S * 4, Label1.Width, S * 4 List1.Move 0, S * 5, Me.ScaleWidth, Label1.Top - S * 5End SubPrivate Sub Form_Unload(Cancel As Integer) ct

3、Exit = True: ctStop = True 保证在查找未结束时能顺利结束程序End SubPrivate Sub Command2_Click() ctStop = True 取消查找End SubPrivate Sub Command1_Click() Static N As Long Dim Su() As Long, S As Long, Gen As Long, I As Long, J As Long Dim nStr As String, T As Single, Ci As Long 查找 N 以内的所有素数,存入数组 Su(),素数的总个数为 S If N 1 the

4、n S = 1: Su(1)=2 根据冰麟轻武的建议,添加了本行 ctStop = False: Command1.Enabled = False List1.Clear: Label1.Caption = 正在查找 & N & 以内的素数 . DoEvents T = Timer If ctCiFind ctCiFind Then Ci = 0: DoEvents If ctStop Then GoTo Show1 Label1.Caption = N & 以内的素数: & S & 个, & Format(I / N * 100, 0.0) & % End If 用 I 除以已经找到的素数交

5、换下面两行代码,似乎能减少一次 Mod 运算 If I Mod Su(J) = 0 Then GoTo NextI 能整除,不是素数,检查下一个 If Su(J) Gen Then Exit For 检测到大于 I 的平方根就不用查了。删除此语句,结果一样,但速度慢得多 Next S = S + 1: ReDim Preserve Su(S): Su(S) = INextI: Next I = N 将找到的素数显示到列表框中Show1: If ctExit Then Exit Sub T = Timer - T Ci = I / T * 0.3 调整为查找过程中每 0.3 秒刷新一次进度 If

6、 ctCiFind 65535 Then I = 65534 Else I = S List1.Visible = False If ctCiAdd ctCiAdd Then Ci = 0: DoEvents If ctStop Then Exit For Label1.Caption = 添加到到列表框 & Format(J / S * 100, 0.0) & % End If Next T = Timer - T Ci = J / T * 0.3 调整为添加过程中每 0.3 秒刷新一次进度 If ctCiAdd Ci Then ctCiAdd = Ci If List1.ListCount

7、 0 Then Load Label1(I): Label1(I).Visible = True Label1(I).Move Me.ScaleWidth * Rnd, Me.ScaleHeight * Rnd Label1(I).Tag = Rnd * 7 - 3 & | & Rnd * 5 + 1 Label1(I).Font.Size = 5 + Rnd * 9 Next Label1(0).Font.Size = 9 Timer1.Enabled = True: Timer1.Interval = 50End SubPrivate Sub Timer1_Timer() Dim I As

8、 Long, S As Long, T As Single, L As Single Dim nTag As String, X As Single, y As Single, W As Single Randomize W = Label1(0).Height * 0.05 For I = 0 To Label1.Count - 1 nTag = Label1(I).Tag S = InStr(nTag, |) X = Left(nTag, S - 1): y = Mid(nTag, S + 1) S = Int(Rnd * 10) If S = 0 Then X = Rnd * 11 - 5: Label1(I).Tag = X & | & y L = Label1(I).Left + X * W: T = Label1(I).Top + y * W If L -Label1(I).Width Then L = Me.ScaleWidth If T Me.ScaleWidth Then L = -Label1(I).Wid

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论