我在为凯撒密码创建循环时遇到问题 [英] I am having trouble making a loop for a Caesar cipher
问题描述
对于学校的课程作业,我必须编写一个简单的凯撒密码,用户可以在其中选择自己的班次(+1、+2 等).例如,如果用户选择 +3(a-->d 等)并输入包含字母 x、y 或 z 的消息,则这些字母必须环绕到字母表的开头,因此 x 变为 a, y变成b,z变成c.我正在使用 ASCII 值来做到这一点.
For a course-work task at school, I have to code a simple Caesar cipher where the user can choose their own shift (+1, +2, etc.). For example, if the user selects +3 (a-->d etc.) and types in a message that contains letters x,y, or z, these letters must wrap around to the start of the alphabet so x becomes a, y becomes b, z becomes c. I am using the ASCII values to do this.
另一个不太重要的问题是,我为每个单选按钮使用了大量 if-else 语句来改变 Caesar 移位.如果可能的话,有没有办法整理一下并使其更有效率.(我刚刚为每个加密和解密按钮添加了三个 if 语句作为示例.)
Another less important issue is that I have used quite a lot of if-else statements for each radio button to alter the Caesar shift. If possible, is there anyway to tidy this up and make it more efficient. (I have just included three if statements for each encrypt and decrypt button for examples.)
我在下面包含了我当前的代码.
I have included my current code below.
Public Class
Dim FnlValue As String = ""
Dim FnlChar As Char
Dim VariableChr As Single
Dim caesar As Integer
Private Sub encrypt_btn_Click(sender As Object, e As EventArgs) Handles encrypt_btn.Click
FnlValue = ""
For VariableChrNo = 0 To (input.Text.Length - 1)
VariableChr = Asc(input.Text.Chars(VariableChrNo))
FnlChar = Chr(VariableChr + caesar)
FnlValue = FnlValue + FnlChar
Next
output.Text = FnlValue
If rad_2.Checked Then
caesar = 2
Else If
If rad_3.Checked Then
caesar = 3
Else If
If rad_4.Checked Then
caesar = 4
Else If
End Sub
Private Sub decrypt_btn_Click(sender As Object, e As EventArgs) Handles decrypt_btn.Click
FnlValue = ""
For VariableChrNo = 0 To (output.Text.Length - 1)
VariableChr = Asc(output.Text.Chars(VariableChrNo))
FnlChar = Chr(VariableChr - caesar)
FnlValue = FnlValue + FnlChar
Next
input.Text = FnlValue
End Sub
If rad_2.Checked Then
caesar = -2
Else If
If rad_3.Checked Then
caesar = -3
Else If
If rad_4.Checked Then
caesar = -4
Else If
End Class
推荐答案
计算机使用数字来表示字符.当您在计算机上执行凯撒密码时,您必须获得字符 ["A","B","C","D"] 的数字表示,例如 [0,1,2,3]->[1,2,3,0],看起来像 ["D","A","B","C"] 如果你把 "A" 看作是 0em>.所以你可以看到,大多数情况下你必须加 1(在这种情况下)才能获得输出值.但是,如果将 1 加到 3,则会得到 4,这不是您想要的值之一.但!如果从 1+3 中减去 4,则得到 0,这就是您想要的.
Computers use numbers for characters. When you're doing a Caesar cipher on a computer, you have to get the number representations of the characters ["A","B","C","D"] to go from, say, [0,1,2,3]->[1,2,3,0], which looks like ["D","A","B","C"] if you regard "A" as being 0. So you can see that mostly you have to add 1 (in this case) to get the output value. However, if you add 1 to 3 you get 4, which is not one of the values you want. But! if you subtract 4 from the 1+3 then you get 0, which is what you want.
ASCII 使用 65 表示A",但您需要将其设为 0 才能使数学运算([0,1,2,3] 事物).所以,如果你得到A"的 ASCII 值并减去 65,那么你得到 0.现在你有一些数学上的工作.数学完成后,您必须将起始编号(大写字母为 65)加回,以便字符编号与字符的表示对应.
ASCII uses 65 for an "A", but you need it to be 0 for the maths to work (the [0,1,2,3] thing). So, if you get the ASCII value for "A" and subtract 65 then you get 0. Now you have something to work with mathematically. After the maths is done, you will have to add back the starting number (65 for uppercase letters) so that the character number corresponds with the representation of the character.
您可能关心的 ASCII 字符集的另一部分是a-z"(即小写字母).你可以用它们做类似的事情,但实际上没有必要知道 Asc("a") 是 97:你可以通过编写 Asc("a")
.
The other part of the ASCII character set you are probably concerned with is "a-z" (i.e. lowercase letters). You can do a similar thing with them, but there isn't really any need to know that Asc("a") is 97: you can get the computer to work that out for you by writing Asc("a")
.
由于您足够精明地意识到重复检查单选按钮可以整理成一段代码(所谓的重构的一部分),我怀疑以下代码可能会回答您的问题:
As you are astute enough to realise that your repeated checks of the radio buttons could do with tidying up into one piece of code (part of what is called refactoring), I suspect that the following code might answer your question:
Function GetCaesarOffset() As Integer
Dim offset As Integer = -1
' put your RadioButton names here in order of 1, 2, ...
' where RadioButton1 represents a shift of 2 (no shift is a bit pointless)
Dim radButtons() As RadioButton = {RadioButton1, RadioButton2}
' iterate over the RadioButtons to find out which one is selected...
For i As Integer = 0 To radButtons.Length - 1
If radButtons(i).Checked Then
' we can return a value from the function right now
Return i + 1
End If
Next
Return -1 ' nothing was selected
End Function
Private Sub encrypt_btn_Click(sender As Object, e As EventArgs) Handles encrypt_btn.Click
Dim caesarOffset As Integer = GetCaesarOffset()
If caesarOffset = -1 Then
' the user has not chosen an offset value... tell them
MessageBox.Show("Please choose a value for the offset.")
' now we don't need to continue in this Sub
Exit Sub
End If
Dim txt As String = input.Text
Dim alphabetLength As Integer = Asc("Z") - Asc("A") + 1 ' this will usually be 26
Dim enciphered As String = ""
For i As Integer = 0 To txt.Length - 1
' get the ASCII character code (a number)
Dim c As Integer = Asc(txt(i))
' Check what range the ASCII code is in and take appropriate action
If c >= Asc("a") AndAlso c <= Asc("z") Then
' Look at lowercase letters
' make it into a number in the range 0-25 by subtracting the
' number which corresponds to the letter "a"
c = c - Asc("a")
c = c + caesarOffset
If c > alphabetLength Then
' oops! it has fallen off the end of the allowed values
' we can correct this by subtracting the length of the alphabet
c -= alphabetLength
End If
c = c + Asc("a")
enciphered &= Chr(c)
ElseIf c >= Asc("A") AndAlso c <= Asc("Z") Then
' Look at uppercase letters
c = c - Asc("A")
' we can use the Mod function instead of the If...Then
c = (c + caesarOffset) Mod alphabetLength
c = c + Asc("A")
enciphered &= Chr(c)
' you could put another ElseIf here for numbers if you wanted to
Else
' it wasn't an uppercase or lowercase character, so
' don't do anything to it.
enciphered &= txt(i)
End If
Next
output.Text = enciphered
End Sub
这篇关于我在为凯撒密码创建循环时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!