VB常见常用的编程题代码.docx
- 文档编号:27827534
- 上传时间:2023-07-05
- 格式:DOCX
- 页数:16
- 大小:17.54KB
VB常见常用的编程题代码.docx
《VB常见常用的编程题代码.docx》由会员分享,可在线阅读,更多相关《VB常见常用的编程题代码.docx(16页珍藏版)》请在冰豆网上搜索。
VB常见常用的编程题代码
常见常用的程序代码
1.输入n个整数求最大值
SubMain()
Console.WriteLine("输入n个整数")
DimsAsString=Console.ReadLine()
DimA()AsString=s.Split("")
Dimaa(A.Length-1)AsInteger
ForiAsInteger=0ToA.Length-1
aa(i)=Convert.ToInt32(A(i))
Next
DimmaxAsInteger=aa(0)
ForiAsInteger=1Toaa.Length-1
Ifaa(i)>maxThen
max=aa(i)
EndIf
Next
Console.WriteLine("最大数为{0}",max)
EndSub
2.输出2~n之间的素数
Submain()
DimnAsInteger=Convert.ToInt32(Console.ReadLine())
Ifn<2Then
Console.WriteLine("输入错误")
EndIf
ForiAsInteger=2Ton
Ifisprime(i)Then
Console.Write(i)
Console.Write("")
EndIf
Next
Console.WriteLine()
EndSub
Functionisprime(ByValnAsInteger)AsBoolean
Ifn=2Then
ReturnTrue
Else
DimiAsInteger=2
Whilei<=n/i
IfnModi=0Then
ReturnFalse
EndIf
i=i+1
EndWhile
ReturnTrue
EndIf
EndFunction
3.字符串也有求长度和按(i)输出的功能
SubMain()
DimexpressionAsString
Console.WriteLine("输入一个字符串")
expression=Console.ReadLine
ForiAsInteger=0Toexpression.Length-1
Console.Write(expression(i))
Console.Write("")
Next
EndSub
4.输入年月日输出这是该年中的第几天
闰年的条件:
能被4整除但不能被100整除或能被400整除
Submain()
Dima()AsInteger={31,28,31,30,31,30,31,31,30,31,30,31}
Dimb()AsInteger={31,29,31,30,31,30,31,31,30,31,30,31}
Console.WriteLine("输入年月日")
DimmAsString=Console.ReadLine()
Dimmsplit()AsString=m.Split(",")
Dimn,month,dataAsInteger
DimsumAsInteger=0
n=Convert.ToInt32(msplit(0))
month=Convert.ToInt32(msplit
(1))
data=Convert.ToInt32(msplit
(2))
Ifn=1Then
sum=data
EndIf
If(nMod4=0)And(nMod100<>0)Or(nMod400=0)Then
ForiAsInteger=0Tomonth-2
sum=sum+b(i)
Next
Else:
ForiAsInteger=0Tomonth-2
sum=sum+a(i)
Next
EndIf
sum=sum+data
Console.Write(sum)
EndSub
线性表和栈.队列题型
1.电话号码本
Structurepeople
DimnameAsString
DimnumberAsInteger
EndStructure
Structurelisttype
Dimdata()Aspeople
DimnAsInteger
EndStructure
SubMain()
DimlistAslisttype
DimmaxAsInteger=1000
ReDimlist.data(max)
Dimp1,p2,p3Aspeople
p1.name="haha"
p1.number="23456"
Subinsert(ByReflAslisttype,ByValiAsInteger,ByValxAspeople)
Ifi>=0Andi<=l.nThen
DimjAsInteger=l.n
Whilej>i
l.data(j)=l.data(j-1)
j=j-1
EndWhile
l.data(i)=x
l.n=l.n+1
Else:
Console.WriteLine("位置不正确")
EndIf
EndSub
Subdelete(ByReflAslisttype,ByValiAsInteger)
Ifi>=0Andi Whilei l.data(i)=l.data(i+1) i=i+1 EndWhile l.n=l.n-1 Else: Console.WriteLine("位置不正确") EndIf EndSub Subprint(ByReflAslisttype) ForiAsInteger=0Tol.n-1 Console.WriteLine(l.data(i).name) Console.WriteLine(l.data(i).number) Next EndSub 2.检查括号是否匹配 a.用newstcak实现 SubMain() DimexpressionAsString Console.WriteLine("输入一个字符串") expression=Console.ReadLine() DimsAsNewStack(OfString) s.Clear() DimchAsString ForiAsInteger=0Toexpression.Length-1 ch=expression(i) Ifch="("Then s.Push(ch) EndIf Ifch=")"Then Ifs.Count<0Then Console.WriteLine("不匹配") Else: s.Pop() EndIf EndIf Endif Ifs.Count=0Then Console.WriteLine("匹配") Else Console.WriteLine("不匹配") EndIf EndSub B.用数组实现 Structurestack Dimch()AsString DimtopAsInteger DimbottomAsInteger EndStructure SubMain() DimexampAsstack DimmaxAsInteger=10000 ReDimexamp.ch(max) Console.WriteLine("输入字符串") DimexpressionAsString=Console.ReadLine() ForiAsInteger=0Toexpression.Length-1 Ifexpression(i)="("Then push(examp,expression(i)) EndIf Ifexpression(i)=")"Then Ifexamp.top<=0Then Console.WriteLine("不匹配") Else Console.Write(pop(examp)+"") EndIf EndIf Next Ifexamp.top=0Then Console.WriteLine("匹配") Else Console.WriteLine("不匹配") EndIf EndSub Subpush(ByRefsAsstack,ByValitemAsString) s.ch(s.top)=item s.top=s.top+1 EndSub Functionpop(ByRefsAsstack)AsString Ifs.top>s.bottomThen s.top=s.top-1 Returns.ch(s.top) Else: ReturnvbNull EndIf EndFunction 3.将一组字符串进行反序 SubMain() Console.WriteLine("输入字符串") DimexpressionAsString=Console.ReadLine DimexamAsNewStack(OfString) exam.Clear() DimchAsString ForiAsInteger=0Toexpression.Length-1 ch=expression(i) exam.Push(ch) Next Whileexam.Count>0 Console.Write(exam.Pop()) EndWhile EndSub 4.判断是否为回文序列 SubMain() Console.WriteLine("输入字符串") DimexpressionAsString=Console.ReadLine() Dimexam1AsNewStack(OfString) Dimexam2AsNewStack(OfString) exam1.Clear() exam2.Clear() DimchAsString ForiAsInteger=0Toexpression.Length-1 ch=expression(i) exam1.Push(ch) ch=expression(expression.Length-1-i) exam2.Push(ch) Next DimflagAsBoolean=True ForiAsInteger=0Toexpression.Length-1 Ifexam1.Pop()<>exam2.Pop()Then flag=False ExitFor EndIf Next Ifflag=TrueThen Console.WriteLine("y") Else: Console.WriteLine("n") EndIf EndSub 5.折半查找 SubMain() Dima()AsInteger={1,2,3,4,5,6,7,8} Dimk=binsearch(a,4) Ifk>=0Then Console.WriteLine("查找的数据在数组中的位置为: "+k.ToString()) Else Console.WriteLine("数据没有找到") EndIf EndSub Functionbinsearch(ByVala()AsInteger,ByValkeyAsInteger)AsInteger DimlowAsInteger=0 DimhighAsInteger=a.Length-1 Whilelow<=high DimmidAsInteger=(low+high)/2 Ifkey=a(mid)Then Returnmid
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB 常见 常用 编程 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)