秋VB上机解答1.docx
- 文档编号:5450519
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:14
- 大小:16.96KB
秋VB上机解答1.docx
《秋VB上机解答1.docx》由会员分享,可在线阅读,更多相关《秋VB上机解答1.docx(14页珍藏版)》请在冰豆网上搜索。
秋VB上机解答1
VB01
一、改错题
OptionExplicit
OptionBase1
PrivateSubCommand1_Click()
DimstAsString,c(3)AsString*8
DimkAsInteger,color(3)AsString
color
(1)="红色"
color
(2)="绿色"
color(3)="蓝色"
st=Text1
Do
k=k+1
c(k)=Left(st,8)
st=Right(st,Len(st)-8)
'LoopUntilk>3原句
LoopUntilk>=3
Fork=1To3
'Text2=Text2&color(k)&":
"&b2d(c)&vbCrLf原句
Text2=Text2&color(k)&":
"&b2d(c(k))&vbCrLf
Nextk
EndSub
PrivateFunctionb2d(sAsString)AsInteger
DimkAsInteger
Do
b2d=b2d+Val(Right(s,1))*2^k
s=Left(s,Len(s)-1)
k=k+1
'LoopUntilk=Len(s)原句
LoopUntilk>7
EndFunction
PrivateSubCommand2_Click()
Debug.Printb2d("1100")
EndSub
二、编程题
OptionBase1
PrivateSubCommand1_Click()
DimA()AsInteger,MAsInteger,NAsInteger
DimIAsInteger,JAsInteger
DimS1AsInteger,S2AsInteger
Randomize
M=InputBox("数组行数m","数组处理",4)
N=InputBox("数组列数n","数组处理",5)
ReDimA(M,N)
ForI=1ToM
ForJ=1ToN
A(I,J)=Int(90*Rnd)+10
Picture1.PrintA(I,J);
Next
Picture1.Print
Next
Sub1A,S1,S2
Text1=S1
Text2=S2
EndSub
PrivateSubCommand2_Click()
Picture1.Cls
Text1=""
Text2=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
PrivateSubSub1(A()AsInteger,S1AsInteger,S2AsInteger)
'本过程功能是求数组A中周边元素之和,存放于S1中,内部元素之和存放于S2中
DimIAsInteger,JAsInteger
ForI=1ToUBound(A,1)
ForJ=1ToUBound(A,2)
IfI=1OrI=UBound(A,1)OrJ=1OrJ=UBound(A,2)Then
S1=S1+A(I,J)
Else
S2=S2+A(I,J)
EndIf
NextJ
NextI
EndSub
VB02
一、改错
OptionExplicit
PrivateSubCommand1_Click()
DimnAsInteger,fnAsInteger,stAsString
Forn=11To99
'IfInStr(CStr(n),"0")<>0Then原句
IfInStr(CStr(n),"0")=0Then
'fn=fx(n)原句
fn=fx((n))
Ifvalidate(n,fn)Andn st=n&"^2="&n^2&","&fn&"^2="&fn^2 List1.AddItemst EndIf EndIf Nextn IfList1.ListCount=0ThenList1.AddItem"无镜平方数" EndSub PrivateFunctionfx(nAsInteger)AsInteger DimsAsString Do 's=s&Str(nMod10)原句 s=s&CStr(nMod10) n=n\10 LoopUntiln=0 fx=s EndFunction PrivateFunctionvalidate(pAsInteger,qAsInteger)AsBoolean Iffx(p^2)=q^2Then validate=True EndIf EndFunction 二、编程 PrivateSubCommand1_Click() DimSAsString,IAsInteger,ChAsString,NAsInteger S=Text1 SearchS,Ch,N S=Left(S,N-1)&Mid(S,N+1)&Ch Text2=S EndSub PrivateSubCommand2_Click() Text1="" Text2="" Text1.SetFocus EndSub PrivateSubCommand3_Click() End EndSub PrivateSubSearch(SAsString,ChAsString,NAsInteger) '在S中找出最大ASCII字符Ch,及其位置n DimIAsInteger N=1 Ch=Left(S,1) ForI=2ToLen(S) IfCh Ch=Mid(S,I,1) N=I EndIf Next EndSub VB03 一、改错 OptionExplicit PrivateSubCommand1_Click() DimnAsLong Forn=34567To40000 'Ifprime(n)Orvalidate(n)Then原句 Ifprime(n)Andvalidate(n)Then Text1=Text1&n&vbCrLf EndIf Nextn IfText1=""ThenText1="无符合要求的数" EndSub PrivateFunctionprime(nAsLong)AsBoolean DimiAsInteger Fori=2ToSqr(n) IfnModi=0ThenExitFunction Nexti prime=True EndFunction 'PrivateFunctionvalidate(nAsLong)AsBoolean原句 PrivateFunctionvalidate(ByValnAsLong)AsBoolean Dimnum(5)AsInteger,iAsInteger,kAsInteger Fori=1To5 num(i)=nMod10 'n=n/10原句 n=n\10 Nexti Fori=1To4 Fork=i+1To5 Ifnum(i)=num(k)ThenExitFunction Nextk Nexti validate=True EndFunction 二、编程 PrivateSubCommand1_Click() DimSAsString,IAsInteger DimRAsInteger,GAsInteger,BAsInteger S=Text1 R=h2d(Mid(S,1,2)) G=h2d(Mid(S,3,2)) B=h2d(Mid(S,5,2)) Text2="红色: "&R&vbCrLf Text2=Text2&"绿色: "&G&vbCrLf Text2=Text2&"蓝色: "&B EndSub PrivateSubCommand2_Click() Text1="" Text2="" Text1.SetFocus EndSub PrivateSubCommand3_Click() End EndSub PrivateFunctionh2d(SAsString)AsInteger DimIAsInteger,nAsInteger,chAsString DimkAsInteger S=UCase(S) ForI=Len(S)To1Step-1 ch=Mid(S,I,1) Ifch>"9"Then n=10+Asc(ch)-Asc("A") Else n=Val(ch) EndIf h2d=h2d+n*16^k k=k+1 Next EndFunction VB04 一、改错 OptionExplicit PrivateSubCommand1_Click() DimnAsInteger,sAsString,iAsInteger,kAsInteger Forn=1000To9999 's=Str(n)原句 s=CStr(n) Fori=1ToLen(s) IfNotprime(Left(s,i))Then ExitFor EndIf Nexti 'Ifi Ifi>Len(s)Then k=k+1 List1.AddItemn EndIf Nextn Text1=k EndSub PrivateFunctionprime(nAsInteger)AsBoolean DimiAsInteger Ifn<>1Then Fori=2ToSqr(n) IfnModi=0Then ExitFunction EndIf Nexti prime=True'新位置 EndIf 'prime=True原句移到结构内 EndFunction 二、编程 PrivateSubCommand1_Click() DimSAsString,chAsString,nAsInteger S=Text1 ch=Text2 n=InStr(S,ch) Ifn<>0Then S=Left(S,n-1)&Mid(S,n+1) Else insertS,ch EndIf Text3=S EndSub PrivateSubCommand2_Click() Text2="" Text3="" Text2.SetFocus EndSub PrivateSubCommand3_Click() End EndSub PrivateSubinsert(SAsString,chAsString) DimiAsInteger Fori=1ToLen(S) Ifch Next S=Left(S,i-1)&ch&Mid(S,i) EndSub VB05 一、改错 OptionExplicit PrivateSubCommand1_Click() DimnAsInteger,p()AsInteger,sumAsLong DimiAsInteger,qAsString 'sum=0'原句位置 Forn=345To789 sum=0'新位置 Calldv(n,p) Fori=1ToUBound(p) sum=sum+p(i)^2 Nexti q="" Fori=UBound(p)To1Step-1 q=q&p(i)&"^2+" Nexti List1.AddItemn&": "&Left(q,Len(q)-1)&"="&sum Nextn EndSub 'PrivateSubdv(nAsInteger,p()AsInteger)原句 PrivateSubdv(ByValnAsInteger,p()AsInteger) DimkAsInteger Do k=k+1 ReDimPreservep(k) p(k)=nMod10 'n=n/10原句 n=n\10 LoopUntiln=0 EndSub 二、编程 OptionBase1 PrivateSubCommand1_Click() DimA()AsInteger,IAsInteger,nAsInteger,maxAsInteger,posAsInteger ForI=2To100 Ifprime(I)Then n=n+1 ReDimPreserveA(n) A(n)=I Picture1.PrintI; IfnMod5=0ThenPicture1.Print EndIf Next max=A (2)-A (1) pos=I ForI=2Ton-1 IfA(I+1)-A(I)>maxThen max=A(I+1)-A(I) pos=I EndIf Next Text1=A(pos)&"--"&A(pos+1) EndSub PrivateSubCommand2_Click() Picture1.Cls Text1="" EndSub PrivateSubCommand3_Click() End EndSub PrivateFunctionprime(nAsInteger)AsBoolean DimIAsInteger ForI=2ToSqr(n) IfnModI=0ThenExitFunction Next prime=True EndFunction VB06 一、改错 OptionExplicit PrivateSubCommand1_Click() Dima()AsInteger,nAsInteger DimiAsInteger,jAsInteger,stAsString n=InputBox("元素个数: ","数组处理",10) Randomize ReDima(n) Fori=1Ton a(i)=Int(Rnd*90)+10 Text1=Text1&Str(a(i)) Nexti 'st=""原句 Fori=nTo2Step-1 st=""'新位置 a(i-1)=a(i-1)/gcd(a(i),a(i-1)) 'ReDima(i-1)原句 ReDimPreservea(i-1) Forj=1ToUBound(a) st=st&Str(a(j)) Nextj List1.AddItemst Nexti EndSub PrivateFunctiongcd(ByValaAsInteger,ByValbAsInteger)AsInteger DimcAsInteger Do c=aModb a=b b=c LoopUntilc=0 'gcd=b原句 gcd=a EndFunction 二、编程 PrivateSubCommand1_Click() DimNAsLong N=Text1 DoUntilhws(N+fx(N)) List1.AddItemN&"+"&fx(N)&"="&N+fx(N) N=N+fx(N) Loop List1.AddItemN&"+"&fx(N)&"="&N+fx(N) List1.AddItem"验证结束" EndSub PrivateSubCommand2_Click() Text1="" List1.Clear Text1.SetFocus EndSub PrivateSubCommand3_Click() End EndSub PrivateFunctionfx(NAsLong)AsLong '本程序求N的反序数 DimsAsString,iAsInteger,s1AsString s=CStr(N) Fori=Len(s)To1Step-1 s1=s1&Mid(s,i,1) Next fx=Val(s1) EndFunction PrivateFunctionhws(NAsLong)AsBoolean '本程序为验证N是否为回文数,若是则返回TRUE DimsAsString,iAsInteger s=CStr(N) Fori=1ToLen(s)/2 IfMid(s,i,1)<>Mid(s,Len(s)-i+1,1)ThenExitFunction Next hws=True EndFunction
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB 上机 解答
![提示](https://static.bdocx.com/images/bang_tan.gif)