第七章答案.docx
《第七章答案.docx》由会员分享,可在线阅读,更多相关《第七章答案.docx(26页珍藏版)》请在冰豆网上搜索。
![第七章答案.docx](https://file1.bdocx.com/fileroot1/2023-2/6/ec00f738-591a-4001-a8a6-1c34cca62334/ec00f738-591a-4001-a8a6-1c34cca623341.gif)
第七章答案
第七章习题答案
1.在过程中声明的局部变量其生命期只到本次过程执行结束就终止了,但是在过程中声明的静态变量其生命期是只有当包含此过程的窗体被终止的时候才终止。
2.有三种形式,传值、传址和利用全局变量向过程传递数据。
传值:
给过程传递的是变量的值,不会有副作用。
传址:
给过程传递的是变量的地址,有副作用
利用全局变量:
有副作用,
3.
PrivateSubC1_Click()
DimiAsInteger,jAsInteger
i=8:
j=8
Callaa(i,j)
Printi
Printj
EndSub
PrivateSubaa(ByValmAsInteger,ByRefnAsInteger)
m=m+1
n=n+1
EndSub
4.
(1)BASIC
(2)第一行:
626;第二行:
262;第三行:
-298
(3)第一行:
23;第二行:
47
(4)第一行:
108;第二行:
12-10;第三行:
52;第四行:
12-10
5.
PrivateSubCommand1_Click()
DimssAsSingle,hsAsSingle
ss=Text1
Text2=tra(ss)
EndSub
PrivateSubCommand2_Click()
Text1.Text=""
Text2=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
PublicFunctiontra(ByValCAsSingle)AsSingle
DimFAsSingle
F=(C*9)/5+32
tra=F
EndFunction
6.
PrivateSubCommand1_Click()
Dimdata_oneAsInteger
data_one=InputBox("请输入要判断的数:
","素数判断")
IfPrime(data_one)Then
sushu=Str(data_one)+"是素数"
else
sushu=Str(data_one)+"不是素数"
EndIf
EndSub
PrivateSubCommand2_Click()
Text1=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
PublicFunctionPrime(ByValNAsInteger)AsBoolean
DimkAsInteger
Fork=2ToSqr(N)
IfNModk=0ThenExitFunction
Nextk
Prime=True
EndFunction
7.
PrivateSubCommand1_Click()
DimssAsString
DimiAsInteger
Fori=1To100
IfPrime(i)AndPrime(i+2)Then
ss=ss&Chr(13)&Chr(10)&i&Str(i+2)
EndIf
Nexti
Text1=ss
EndSub
PublicFunctionPrime(ByValNAsInteger)AsBoolean
DimkAsInteger
Fork=2ToSqr(N)
IfNModk=0ThenExitFunction
Nextk
Prime=True
EndFunction
8.
OptionExplicit
OptionBase1
PrivateSubForm_Click()
DimaAsInteger,nAsInteger
DimiAsInteger
Randomize
Dimaa(20)AsInteger
a=Int(100*Rnd()*1)
n=1
Do
Fori=1Ton-1
Ifa=aa(i)Then
ExitFor
EndIf
Nexti
Ifi>=nThen
aa(n)=a
n=n+1
EndIf
a=Int(100*Rnd()*1)
LoopWhilen<=20
Fori=1To20
Printaa(i);
Nexti
EndSub
9.
Dima(19)AsInteger
PrivateSubCommand1_Click()
a(0)=Int(100*Rnd)+1
Fori=1To19
a(i)=Int(100*Rnd)+1
Forj=0Toi-1
Ifa(i)=a(j)Then
i=i-1
ExitFor
EndIf
Nextj
Ifj=iThenPrinta(i);
Nexti
EndSub
PrivateSubCommand2_Click()
Max=a(0)+a
(1)+a
(2)+a(3)
maxi=0
Fori=0To19
s=a(i)+a((i+1)Mod20)+a((i+2)Mod20)+a((i+3)Mod20)
Ifs>MaxThen
Max=s
maxi=i
EndIf
Nexti
PrintStr(a(maxi))&"+"&Str(a(maxi+1))&"+"&Str(a(maxi+2))&"+"&Str(a(maxi+3))&"="&Str(Max)
EndSub
10.
PrivateSubCommand1_Click()
Dima(5,5)AsInteger
Randomize
Fori=1To5
Forj=1To5
a(i,j)=Int(100*Rnd)+1
Printa(i,j);
Nextj
Print
Nexti
Callfmax(a)
EndSub
PrivateSubfmax(a()AsInteger)
Max=a(1,1)
maxi=1
maxj=1
Fori=1To5
Forj=1To5
Ifa(i,j)>MaxThen
Max=a(i,j)
maxi=i
maxj=j
EndIf
Nextj
Nexti
Print"最大元素为:
a("+Str(maxi)+","+Str(maxj)+")="+CStr(Max)
EndSub
11.
PrivateSubCommand1_Click()
DimiAsInteger
Fori=1To6
Text1=Text1&Str(fab(i))
Nexti
EndSub
PrivateFunctionfab(nAsInteger)AsLong
Ifn=1Orn=2Then
fab=1
Else
fab=fab(n-1)+fab(n-2)
EndIf
EndFunction
12.
PrivateSubCommand1_Click()
DimiAsInteger
a=InputBox("请输入一个大于2的偶数:
")
n=Val(a)
Ifn=4Then
Print"4=2+2"
ExitSub
EndIf
Fori=3Ton-1Step2
Ifprime(i)Andprime(n-i)Then
PrintStr(n)+"="+Str(i)+"+"+Str(n-i)
ExitSub
EndIf
Nexti
Print"歌德巴赫猜想不对!
"
EndSub
PrivateFunctionprime(ByValnAsInteger)AsBoolean
Fori=2Ton-1
IfnModi=0Then
prime=False
ExitFunction
EndIf
Nexti
prime=True
EndFunction
13.
PrivateSubCommand1_Click()
DimiAsInteger
Fori=10To9999
Callarm(i)
Nexti
EndSub
PrivateSubarm(jAsInteger)
a=CStr(j)
n=Len(a)
Fork=1Ton
b=Mid(a,k,1)
s=s+Val(b)^n
Nextk
Ifs=jThen
Printj
EndIf
EndSub
14.
PrivateSubCommand1_Click()
DimnAsInteger,mAsString,bAsInteger
n=InputBox("请输入原始数的进制(2-16):
")
m=InputBox("请输入原始数:
")
Label1.Caption=Str(n)+"进制数"
Text1=m
b=trans(m,n)
Text2=Str(b)
EndSub
PrivateFunctiontrans(mAsString,nAsInteger)AsInteger
DimkAsInteger,tAsString,rAsInteger,sAsInteger
k=Len(m)
Fori=1Tok
t=UCase(Mid(m,i,1))
Ift>="A"Andt<="F"Then
r=Asc(t)-55
Else
r=Val(t)
EndIf
s=s+r*n^(k-i)
Nexti
trans=s
EndFunction
15.
Form1:
Dimb(5)AsSingle
PrivateSubCommand1_Click()
DimaAsSingle
Randomize
Fori=0To5
a=(Int(61*Rnd)+40)/10
b(i)=a
Text1(i)=Str(a)
Nexti
EndSub
PrivateSubCommand2_Click()
Min=b(0)
Max=b(0)
Sum=b(0)
Fori=1To5
Ifb(i)>MaxThenMax=b(i)
Ifb(i)Sum=Sum+b(i)
Nexti
Sum=Sum-Max-Min
Text2=Str(Sum/4)
EndSub
PrivateSubCommand3_Click()
Fori=0To5
Text1(i)=""
Nexti
Text2=""
EndSub
PrivateSubCommand4_Click()
End
EndSub
Form2:
Dimb(5)AsSingle
PrivateSubCommand1_Click()
DimaAsSingle
Randomize
Fori=0To5
a=(Int(61*Rnd)+40)/10
b(i)=a
List1.AddItemChr(65+i)+":
"+Str(a)
Nexti
EndSub
PrivateSubCommand3_Click()
List1.Clear
Text1=""
EndSub
PrivateSubCommand4_Click()
End
EndSub
PrivateSubCommand2_Click()
Min=b(0)
Max=b(0)
Sum=b(0)
Fori=1To5
Ifb(i)>MaxThenMax=b(i)
Ifb(i)Sum=Sum+b(i)
Nexti
Sum=Sum-Max-Min
Text1=Str(Sum/4)
EndSub
16.
PrivateSubCommand1_Click()
DimiAsInteger,kAsInteger,mAsInteger
DimsAsSingle,tAsSingle
i=1
Do
t=1
Fork=1Toi
m=2*k
t=t*m
Nextk
s=s+1/t
If1/t<0.00001ThenExitDo
i=i+1
Loop
Prints
EndSub
17.
PrivateSubCommand1_Click()
DimnAsInteger,tAsSingle,sAsSingle,xAsSingle
n=2:
s=0
x=InputBox("inputx:
")
Do
t=(x^f(n))/(f(n-1)*f(n))
s=s+t
Ift<0.00001ThenExitDo
n=n+1
Loop
Prints
EndSub
PrivateFunctionf(nAsInteger)AsInteger
Ifn=1Orn=2Then
f=1
Else
f=f(n-1)+f(n-2)
EndIf
EndFunction
18.
PrivateSubForm_Click()
Dima(10)AsInteger
Randomize
Fori=1To10
a(i)=Int(Rnd*20)+1
Debug.Printa(i);
Nexti
Debug.Print
Callinsertion(a)
Fori=1To10
Debug.Printa(i);
Nexti
Debug.Print
EndSub
PrivateSubinsertion(sort()AsInteger)
DimmidAsInteger,leftAsInteger,rightAsInteger
ub=UBound(sort)
Ifsort
(1)>sort
(2)Then
temp=sort
(1)
sort
(1)=sort
(2)
sort
(2)=temp
EndIf
Fori=3Toub
flg=False
flg1=False
temp=sort(i)
Iftemp(1)Then
flg=True
mid=0
EndIf
left=1
right=i-1
DoWhileleft<=rightAndNotflg
mid=Int((left+right)/2)
Ifsort(mid)=tempThen
flg1=False
ExitDo
ElseIfsort(mid)left=mid+1
flg1=False
Else
right=mid-1
flg1=True
EndIf
Loop
Ifflg1Thenmid=mid-1
Forj=i-1Tomid+1Step-1
sort(j+1)=sort(j)
Nextj
sort(mid+1)=temp
Nexti
EndSub
19.
OptionExplicit
OptionBase1
Dimaa(2,25)AsInteger
Dimbb(25)AsInteger
PrivateSubC1_Click()
DimvAsInteger,yAsInteger
DimsAsString
Forv=1To25
IfXXX(v)=Val(T1)Then
Fory=1To25
s=s&Str(bb(y))
Nexty
EndIf
Nextv
T2=s
EndSub
PublicFunctionXXX(ByValnumAsInteger)AsInteger
DimiAsInteger,jAsInteger,mAsInteger,nAsInteger
DimctAsInteger,cntAsInteger
DimflagAsBoolean
'给数组赋初值
Fori=1To25
aa(2,i)=1
Nexti
Forj=1To25
aa(1,j)=j
Nextj
'依次取数,记录取数的序列,并返回最后取到的数值
m=1
ct=1
Do
Ifaa(2,num)=1Then
bb(m)=aa(1,num)
m=m+1
Ifm>25ThenExitDo
aa(2,num)=0
cnt=0
EndIf
ct=ct+1
DoWhilect<>cnt
num=num+1
Ifnum>25Thennum=numMod25
cnt=aa(2,num)+cnt
Loop
Loop
XXX=bb(25)
EndFunction
20.
OptionExplicit
DimssAsString
PrivateSubC1_Click()
Callgreat_zs
T1=ss
EndSub
'产生6个不同的数
PublicSubgreat_zs()
Dimshu_1AsInteger,shu_2AsInteger,shu_3AsInteger,shu_4AsInteger,shu_5AsInteger,shu_6AsInteger
DimshuAsSingle
Forshu_1=1To9
Forshu_2=1To9
Forshu_3=1To9
Forshu_4=1To9
Forshu_5=1To9
Forshu_6=1To9
shu=Val(CStr(shu_1)&CStr(shu_2)&CStr(shu_3)&CStr(shu_4)&CStr(shu_5)&CStr(shu_6))
Iflala(shu)Andzhishu(shu)Thenss=ss+Str(shu)'把满足要求的数记录到变量ss中
Nextshu_6
Nextshu_5
Nextshu_4
Nextshu_3
Nextshu_2
Nextshu_1
EndSub
'判断相邻的两个数是否为素数
PublicFunctionzhishu(ByValzssAsSingle)AsBoolean
DimiAsInteger,judAsInteger
Fori=1To5
jud=Val(Mid(CStr(zss),i,1))+Val(Mid(CStr(zss),i+1,1))
IfNotPrime(jud)ThenExitFunction
Nexti
IfNotPrime(Val(Mid(CStr(zss),6,1))+Val(Mid(CStr(zss),1,1)))ThenExitFunction
zhishu=True
EndFunction
'判断素数
PublicFunctionPrime(ByValNAsInteger)AsBoolean
DimkAsInteger
Fork=2ToSqr(N)
IfNModk=0ThenExitFunction
Nextk
Prime=True
EndFunction
'判断数列中的数是否各不相同
PublicFunctionlala(ByVallaAsSingle)AsBoolean
DimiAsInteger,jAsInteger
Fori=1To6
Forj=1To6
IfMid(CStr(la),i,1)=Mid(CStr(la),j,1)Andi<>jThenExitFunction
Nextj
Nexti
lala=True
EndFunction
21.
PrivateSubCommand1_Click()
DimiAsIntege