初中信息技术vb常用算法(共20张PPT)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。





Private Sub Command1_Click() Dim c(1 To 50) As Integer a = Text1.Text : b = Text2.Text alen = Len(a) : blen = Len(b) jie = 1 For i = 1 To alen If i <= blen Then ad = Val(Mid(a, alen - i + 1, 1)) bd = Val(Mid(b, blen - i + 1, 1)) e = ad + 10 - bd - 1 + jie Else ad = Val(Mid(a, alen - i + 1, 1)) e = ad + 10 + jie - 1 End If c(i) = e Mod 10 jie = e \ 10 Next i For i = 1 To alen - 1 s = Str(c(i)) + s Next i If c(alen) <> 0 Then s = Str(c(alen)) + s Text3.Text = s End Sub

字符串的插入




Private Sub Command1_Click() s = "a red car" s1 = "nice " i=3
s = Mid(s, 1, i - 1) + s1 + Mid(s, i, Len(s) - i + 1)


Text1.Text = s End Sub

aa = "0123456789ABCDEF" Do While x <> 0 c = x Mod r s = Mid(aa, c + 1, 1)+s x=x\r Loop
二进制运算



Private Sub Command1_Click() Dim c(1 To 10) As Integer Dim x1, y1 As Integer Dim s As String x = Text1.Text y = Text2.Text For i = 1 To Len(x) x1 = Val(Mid(x, Len(x) - i + 1, 1)) y1 = Val(Mid(y, Len(y) - i + 1, 1)) c(i) = x1 + y1 + t If c(i) >= 2 Then c(i) = c(i) Mod 2 : t = 1 Else t = 0 Next i For i = 1 To Len(x) s = Str(c(i)) + s Next i If t = 1 Then s = Str(t) + s Text3.Text = s End Sub
数组元素的删除
Dim a(1 to 10) as integer X=val(text1.text) For i=1 to 10 If x=a(i) then exit for Next i For j= i+1 to 10
a(j-1)=a(j)
Next i

数组元素的插入
Dim a(1 to 10) as integer X=val(text1.text) For i=1 to 9 If x<a(i) then exit for Next i For j=10 to i step-1
后序表达式
运算符放在两个运算对象的后面,所有的 计算按运算符出现的顺序,严格从左向右进 行,不考虑运算符的优先规则
算式9 +(3-1)*3+10/2
9 3 1 - 3 * + 10 2 / + 9 2 3 * + 10 2 / + 9 6 + 10 2 / + 15 10 2 / + 15 5 + 20
加法运算
e = ad + t End If If e >= 10 Then c(i) = e - 10: t = 1 Else c(i) = e: t = 0 Next i For i = 1 To alen s = Str(c(i)) + s Next i If t = 1 Then s = Str(t) + s Text3.Text = s End Sub
VB常用算法
进制转换
(十进制数转换为其它进制)
25D=11001B
Function tran(x As Integer, r As Integer) As String Dim s As String Dim c As Integer Do While x<> 0 c = x Mod r If c >= 10 Then s = Chr(Asc("A") + c - 10) + s Else s = Str(c) + s End If x=x\r Loop tran = s End Function


ห้องสมุดไป่ตู้




Private Sub Command2_Click() Dim a(1 To 11) As String t=n i=t Do While t >= 3 ch2 = a(i): ch1 = a(i - 1): ch3 = a(i - 2) flag = 0 If ch3 = "+" Then a(i - 2) = Str(Val(ch1)+Val(ch2)) For j = i + 1 To t a(j - 2) = a(j) Next j t=t–2:i=t flag = 1 End If If flag = 0 Then i = i - 1 Loop Text2.Text = a(1) End Sub



Private Sub Command1_Click() Dim a(1 To 11) As String t = n: i = 1: k = 0 Do While t >= 3 ch1 = a(i): ch2 = a(i + 1): ch3 = a(i + 2) flag = 0 If ch3 = “+" Then a(i + 2) = Str(Val(ch1) + Val(ch2)) For j = k To 1 Step -1 a(i - j + 2) = a(i - j) Next j t = t - 2: flag = 1: k = 0 End If If flag = 0 Then k = k + 1 i=i+1 Loop Text1.Text = a(i + 1) End Sub
a(j+1)=a(j)
Next j a(i)=x

高精度运算
高精度运算,是指参与运算的数 值的范围大大超出了标准数据类型, 改用字符串或数组来表示数据,拆成 一位一位或一个个数组来进行运算



Private Sub Command1_Click() Dim c(1 To 20) As Integer a = Text1.Text : b = Text2.Text alen = Len(a) : blen = Len(b) t=0 For i = 1 To alen If i <= blen Then ad = Val(Mid(a, alen - i + 1, 1)) bd = Val(Mid(b, blen - i + 1, 1)) e = ad + bd + t Else ad = Val(Mid(a, alen - i + 1, 1))
减法运算

前序表达式
运算符放在两个运算对象的前面,所有的 计算按运算符出现的顺序,严格从右向左进 行,不考虑运算符的优先规则
算式9 +(3-1)*3+10/2
+ 9 + * - 3 1 3 / 10 2 + 9 + * - 3 1 3 5
+ 9 + * 2 3 5
+ 9 + 6 5 + 9 11 20
表示一个字符是字母: a>=“A” and a<=“Z” or a>=“a” and a<=“z” 表示一个字符是非字母: a<“A” or a>“Z” and a<“a” or a>“z” 或 Not (a>=“A” and a<=“Z” or a>=“a” and a<=“z”)


字符串的删除




Private Sub Command2_Click() s = "a red car" s1 = "red " i=3
s = Mid(s, 1, i - 1) + Mid(s, i + Len(s1), Len(s) - Len(s1)-i+1)


Text1.Text = s End Sub
相关文档
最新文档