VB常见常用的编程题代码.docx

上传人:b****8 文档编号:27827534 上传时间:2023-07-05 格式:DOCX 页数:16 大小:17.54KB
下载 相关 举报
VB常见常用的编程题代码.docx_第1页
第1页 / 共16页
VB常见常用的编程题代码.docx_第2页
第2页 / 共16页
VB常见常用的编程题代码.docx_第3页
第3页 / 共16页
VB常见常用的编程题代码.docx_第4页
第4页 / 共16页
VB常见常用的编程题代码.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

VB常见常用的编程题代码.docx

《VB常见常用的编程题代码.docx》由会员分享,可在线阅读,更多相关《VB常见常用的编程题代码.docx(16页珍藏版)》请在冰豆网上搜索。

VB常见常用的编程题代码.docx

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

Ifkey

high=mid-1

Else:

low=mid+1

EndIf

EndIf

EndWhile

Return-1

EndFunction

EndModule

6.顺序查找

SubMain()

Console.WriteLine("输入n个整数并以逗号隔开")

DimhAsString=Console.ReadLine()

Dimm()AsString=h.Split(",")

Dimgroup(m.Length-1)AsInteger

ForiAsInteger=0Tom.Length-1

group(i)=Convert.ToInt32(m(i))

Next

Dimk=search(group,6)

Ifk>=0Then

Console.WriteLine("查找的位置在{0}",k)

Else:

Console.WriteLine("不存在")

EndIf

EndSub

Functionsearch(ByVals()AsInteger,ByValkeyAsInteger)AsInteger

ForiAsInteger=0Tos.Length-1

Ifs(i)=keyThen

Returni

EndIf

Next

Return-1

EndFunction

7.冒泡排序

SubMain()

Dims()AsInteger={1,2,3,4,5,6,7}

show(s)

bubble(s)

show(s)

EndSub

Subshow(ByVals()AsInteger)

ForEachkAsIntegerIns

Console.Write(k.ToString()+"")

Next

EndSub

Subbubble(ByVals()AsInteger)

DimlenghthAsInteger=s.Length

ForiAsInteger=0Tolenghth-1

ForjAsInteger=lenghth-1Toi+1Step-1

Ifs(j)

DimtempAsInteger=s(j)

s(j)=s(j-1)

s(j-1)=temp

EndIf

Next

Next

EndSub

8.选择排序

SubMain()

Dims()AsInteger={2,9,7,5}

show(s)

Console.WriteLine("排序结果是")

Selectsort(s)

show(s)

EndSub

Subshow(ByVals()AsInteger)

ForEachkAsIntegerIns

Console.Write(k.ToString()+"")

Next

EndSub

Subselectsort(ByVals()AsInteger)

DimlenghthAsInteger=s.Length

ForiAsInteger=0Tolenghth-2

ForjAsInteger=i+1Tolenghth-1

Ifs(j)

DimtempAsInteger=s(j)

s(j)=s(i)

s(i)=temp

EndIf

冒泡排序

DimlenghthAsInteger=s.Length

ForiAsInteger=0Tolenghth-1

ForjAsInteger=lenghth-1Toi+1Step-1

Ifs(j)

DimtempAsInteger=s(j)

s(j)=s(j-1)

s(j-1)=temp

EndIf

Next

Next

   快速排序

DimlenghthAsInteger=s.Length

ForiAsInteger=0Tolenghth-2

ForjAsInteger=i+1Tolenghth-1

Ifs(j)

DimtempAsInteger=s(j)

s(j)=s(i)

s(i)=temp

EndIf

Next

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1