
打开Vb,添加2个label控件,2个text控件 1个command按钮
代码如下:
Private Function f(ByVal a As String, k As Integer, n As Integer) As String
If ((Asc(a) >= 65 And Asc(a) <= 97) Or (Asc(a) >= 97 And Asc(a) <= 122)) And Len(a) = 1 Then '判断是否为一个字母
'利用公式计算
If Asc(a) >= 65 And Asc(a) <肆燃好= 97 Then f = Chr((Asc(a) - 64 + k) Mod n + 64) '当为大写的时候
If Asc(a) >= 97 And Asc(a) <= 122 Then f = Chr((Asc(a) - 96 + k) Mod n + 96) '当为小写的时候
Else
f = "error" '若不满足要求,则返回错误裂铅
End If
End Function
Private Sub Command1_Click()
Dim strold As String
Dim strnew As String
Dim k As Integer
Dim n As Integer
Dim i As Long
Dim tmp As String
k = 3
n = 26
strold = Text1.Text '要加密的字符串
For i = 1 To Len(strold)
tmp = Mid(strold, i, 1)
tmp = f(tmp, k, n)
If tmp <>"error" Then
strnew = strnew + tmp
Else
MsgBox "字符串中含有非法段缺字符"
Exit Sub
End If
Next i
Text2.Text = strnew
End Sub
Private Sub Form_Load()
Text1.Text = "PROGRAM"
Text2.Text = ""
Command1.Caption = "加密"
Label1.Caption = "源字符串:"
Label2.Caption = "加密字符串:"
End Sub
解:Private Sub cmdCode_Click()
Dim pt As String' 明文
Dim ct As String' 密文
Dim n As Integer' 字母在字母表中的序号
Dim ch As String
Dim i As Integer
pt = txtPt.Text
ct = "搭枣"知做拆
For i = 1 To Len(pt)
ch = Mid(pt, i, 1)
Select Case ch
Case "A"胡陪 To "Z"
n = Asc(ch) - Asc("A")
n = (n + 3) Mod 26
ch = Chr(n + 65)
Case "a" To "z"
n = asc(ch)-asc("A")
n = (n + 3) Mod 26
ch = Chr(n + 97)
End Select
ct = trim(ct &ch)
Next i
txtCt.Text = ct
End Sub
Private Sub Command1_Click()
n = CInt(Text3)
s = Text1
For i = 1 To Len(s)
c = UCase(Mid(s, i, 1))
If c >= "A" And c <= "Z" Then
If Asc(c) >Asc("Z") - n Then
c = Chr(Asc(c) + n - 26)
Else
c = Chr(Asc(c) + n)
End If
Mid(s, i, 1) = c
裤正 End If
Next i
Text2 = s
End Sub
Private Sub Command2_Click()
n = CInt(Text3)
s = Text2
For i = 1 To Len(s)
c = UCase(Mid(s, i, 1))
If c >= "A" And c <= "Z" Then
If Asc(c) <Asc("A"液野) + n Then
c = Chr(Asc(c) - n + 26)
闹纯喊 Else
c = Chr(Asc(c) - n)
End If
Mid(s, i, 1) = c
End If
Next i
Text1 = s
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
Text3 = ""
End Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)