我正在为我的编程韵母编码器程序做最后的润色,我已经构建了一个旋转函数,使所有不是字母的字符保持原样(所以基本上所有其他字符,数字、短划线、空格都保持不变,所有字母上下都正常旋转)
n = 13
For i = 1 To Len(stinput)
If (i >= 65 And i <= 90) Or (i >= 97 And i <= 122) Then
Mid(stinput, i, 1) = Chr(Asc(Mid(stinput, i, 1)) - n)
End If
Next i
使用if函数,我试图分离不是字母的ascii代码,唯一的问题是我在让它工作时有点困难,上面的代码不起作用。
感谢您的帮助!
试试这个:
Dim rot13c As Func(Of Char, Char, Char) = Function(b, c) Chr(If(c >= b And Asc(c) <= Asc(b) + 25, If(Asc(c) + 13 > Asc(b) + 25, Asc(c) - 13, Asc(c) + 13), Asc(c)))
Dim rot13 As Func(Of String, String) = Function(s) New String(s.Select(Function (c) rot13c("a"C, rot13c("A"C, c))).ToArray())
然后你可以这样称呼它:
Dim text = "Hello, there. This is a test!"
Dim text13 = rot13(text) ' = "Uryyb, gurer. Guvf vf n grfg!"
Dim text1313 = rot13(text13) ' = "Hello, there. This is a test!"
'这是我设计的程序的加密部分,以下是如何在旋转时只更改选定的字符
Dim stinput As String = txtinput.Text
Dim i, n, m, o As Integer
Dim inleftover As Integer
If RBRot13.Checked = False Then 'if neither rotation button is selected, a message will tell user to select one
If RBActiveSelect.Checked = False Then
MsgBox("Vous devez choisir le nombre de rotation afin de poursuivre a l'encryption")
End If
End If
txtoutput.Text = "" 'Clears output box when button is pressed to only show the result
If RBRot13.Checked = True Then
For i = 1 To stinput.Length
n = Asc(Mid(stinput, i, 1)) 'Defines the position of the caracter in the ascii table
Select Case n
Case 65 To 90 : o = n + 13 'From A(65) to Z(90), adds 13 to their respective ascii number
If o > 90 And o <= 103 Then 'if the addition is between 90 and 103(90 +13)
inleftover = o - 91 'Calculate de difference as a leftover
o = 65 + inleftover 'Apply leftover to A(65) so as to not give anything other than a letter
End If
Case 97 To 122 : o = n + 13 'From a(97) to z(122), adds 13 to their respective ascii number
If o > 122 And o <= 135 Then 'if the addition is between 122 and 135(122 +13)
inleftover = o - 123 'Calculate de difference as a leftover
o = 97 + inleftover 'Apply leftover to a(97) so as to not give anything other than a letter
End If
Case 192 To 219 : o = n + 13 'From À(192) to Z(219), adds 13 to their respective ascii number
If o > 219 And o <= 232 Then 'if the addition is between 219 and 232(219 +13)
inleftover = o - 220 'Calculate de difference as a leftover
o = 192 + inleftover 'Apply leftover to À(192) so as to not give anything other than an accented letter
End If
Case 224 To 251 : o = n + 13 'From à(224) to Z(251), adds 13 to their respective ascii number
If o > 251 And o <= 264 Then 'if the addition is between 251 and 264(251 +13)
inleftover = o - 225 'Calculate de difference as a leftover
o = 224 + inleftover 'Apply leftover to à(224) so as to not give anything other than an accented letter
End If
Case Else : o = n
End Select
txtoutput.Text = txtoutput.Text & Chr(o)
Next i
ElseIf RBActiveSelect.Checked = True Then
m = Me.NUDChoice.Value 'Defines the value of the rotation set by the user
For i = 1 To stinput.Length
n = Asc(Mid(stinput, i, 1)) 'Defines the position of the caracter in the ascii table
Select Case n
Case 65 To 90 : o = n + m 'From A(65) to Z(90), adds m to their respective ascii number
If o > 90 Then 'if the addition is over 90
inleftover = o - 91 'Calculate de difference as a leftover
o = 65 + inleftover 'Apply leftover to A(65) so as to not give anything other than a letter
End If
Case 97 To 122 : o = n + m 'From A(65) to Z(90), adds m to their respective ascii number
If o > 122 Then 'if the addition is over 122
inleftover = o - 123 'Calculate de difference as a leftover
o = 97 + inleftover 'Apply leftover to A(65) so as to not give anything other than a letter
End If
Case 192 To 219 : o = n + m 'From À(192) to Z(219), adds m to their respective ascii number
If o > 219 Then 'if the addition is over 219
inleftover = o - 220 'Calculate de difference as a leftover
o = 192 + inleftover 'Apply leftover to A(192) so as to not give anything other than an accented letter
End If
Case 224 To 251 : o = n + m 'From à(224) to Z(251), adds m to their respective ascii number
If o > 251 Then 'if the addition is over 251
inleftover = o - 225 'Calculate de difference as a leftover
o = 224 + inleftover 'Apply leftover to à(224) so as to not give anything other than an accented letter
End If
Case Else : o = n
End Select
txtoutput.Text = txtoutput.Text & Chr(o)
Next i
End If
End Sub
'这里开始解密部分的2个按钮使用
Dim stinput As String = txtinput.Text
Dim i, n, m, o As Integer
Dim inleftover As Integer
If RBRot13.Checked = False Then 'if neither rotation button is selected, a message will tell user to select one
If RBActiveSelect.Checked = False Then
MsgBox("Vous devez choisir le nombre de rotation afin de poursuivre a la décryption")
End If
End If
txtoutput.Text = "" 'Clears output box when button is pressed to only show the result
If RBRot13.Checked = True Then 'If Rotation 13 selected do
For i = 1 To stinput.Length
n = Asc(Mid(stinput, i, 1)) 'Defines the position of the caracter in the ascii table
Select Case n
Case 65 To 90 : o = n - 13 'From A(65) to Z(90), deducts 13 to their respective ascii number
If o < 65 And o >= 52 Then 'if the addition is between 65 and 52(65 - 13)
inleftover = o - 64 'Calculate de difference as a leftover
o = 90 + inleftover 'Apply leftover to A(65) so as to not give anything other than a letter
End If
Case 97 To 122 : o = n - 13 'From a(97) to z(122), deducts 13 to their respective ascii number
If o < 97 And o >= 84 Then 'if the addition is between 97 and 84(97 - 13)
inleftover = o - 96 'Calculate de difference as a leftover
o = 122 + inleftover 'Apply leftover to a(97) so as to not give anything other than a letter
End If
Case 192 To 219 : o = n - 13 'From À(192) to Z(219), deducts 13 to their respective ascii number
If o < 192 And o >= 179 Then 'if the addition is between 192 and 179(192 - 13)
inleftover = o - 191 'Calculate de difference as a leftover
o = 219 + inleftover 'Apply leftover to À(192) so as to not give anything other than an accented letter
End If
Case 224 To 251 : o = n - 13 'From à(224) to Z(251), deducts 13 to their respective ascii number
If o < 224 And o >= 211 Then 'if the addition is between 224 and 211(224 - 13)
inleftover = o - 223 'Calculate de difference as a leftover
o = 251 + inleftover 'Apply leftover to à(224) so as to not give anything other than an accented letter
End If
Case Else : o = n 'everything else stays the same
End Select
txtoutput.Text = txtoutput.Text & Chr(o) 'Prints the result into the output box
Next i
ElseIf RBActiveSelect.Checked = True Then 'If Choice rotation selected do
m = Me.NUDChoice.Value 'Defines the value of the rotation set by the user
For i = 1 To stinput.Length
n = Asc(Mid(stinput, i, 1)) 'Defines the position of the caracter in the ascii table
Select Case n
Case 65 To 90 : o = n - m 'From A(65) to Z(90), deducts m to their respective ascii number
If o < 65 Then 'if the addition is under 65
inleftover = o - 64 'Calculate de difference as a leftover
o = 65 + inleftover 'Apply leftover to z(90) so as to not give anything other than a letter
End If
Case 97 To 122 : o = n - m 'From a(97) to z(122), deducts m to their respective ascii number
If o < 97 Then 'if the addition is under 97
inleftover = o - 96 'Calculate de difference as a leftover
o = 90 + inleftover 'Apply leftover to z(122) so as to not give anything other than a letter
End If
Case 192 To 219 : o = n - m 'From À(192) to Û(219), deducts m to their respective ascii number
If o < 192 Then 'if the addition is under 192
inleftover = o - 191 'Calculate de difference as a leftover
o = 219 + inleftover 'Apply leftover to Û(219) so as to not give anything other than an accented letter
End If
Case 224 To 251 : o = n - m 'From à(224) to û(251), deducts m to their respective ascii number
If o < 224 Then 'if the addition is under 224
inleftover = o - 223 'Calculate de difference as a leftover
o = 251 + inleftover 'Apply leftover to û(251) so as to not give anything other than an accented letter
End If
Case Else : o = n 'everything else stays the same
End Select
txtoutput.Text = txtoutput.Text & Chr(o) 'Prints the result to the output box
Next i
End If
End Sub
它的功能很长,顺便说一句,m功能链接到一个列表按钮,可以选择在1到25 之间旋转
感谢