在C#中编写Shift Cypher的代码

时间:2015-04-19 21:01:58

标签: c# string

我的班级实验室让我们编写一个Shift Cypher和Sub Cypher方法,这些方法在按钮单击事件中调用。您将在下面找到此问题的代码。

有人可以帮助我解决有问题的两种方法,并在事件处理程序中调用这些方法吗?

在这里输入代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace String_Stuff
{
    public partial class stringForm : Form
    {
      public stringForm()
      {
          InitializeComponent();
        }

        private string SwitchCase(string input)
        {
            string str1 = input;
            string output = str1.ToUpper();

            return output;

        }


       private string Reverse(string input)
        {
            string str1 = input;
            char[] inputArray = input.ToCharArray();
            Array.Reverse(inputArray);
            string output = new string(inputArray);

            return output;
      }

       private string PigLatin(string input)
       {
           // rules: words that begin with a consinant-
           //is moved to the end and "ay" is added.
           // words that begin with a vowel-
           // the sylable "ay" is added to the end of the word.
           // e.g. eagle -> eagleay, apple->appleay.
           string str1 = input;           
           string ay = "ay";
           input = inputTextBox.Text; // already a string, no need to convert
           input = input.Substring(1, input.Length - 1) + input.Substring(0, 1);
           str1 = " the word in Pig Latin is " + input + ay; // need to use Text property to display string
           string output= str1  ;
           return output;
       }                    



        private string ShiftCypher(string input, int shift)
        {
            //Each letter in the word is replaced by a letter some fixed number of positions down the alphabet.
            //For this lab, shift by 3. Example: program, shifted by 3 will become surjudp

            string output = null;
            char[] A = null;
            A = input.ToCharArray();
            int temp;
            for (int i = 0; i < input.Length; i++)
            {
                temp = (int) (A[i] + shift);
                output += (char)temp;
            }


            return output;
        }

        private string SubCypher(string input, string charsToSub)

        {
            //Each letter in the word will be replaced by a letter from the corresponding position 
            //in a substitution alphabet. For this lab use this substitution alphabet: zeroabcdfghijklmnpqstuvwxy. 
            //Example: disk would become ofqh.
            string subAlpha = "zeroabcdfghijklmnpqstuvwxy";
            string str1 = input;
            string subCypher = subAlpha;



            string output = "";
            return output;
        }



        private void transformButton_Click_1(object sender, EventArgs e)
        {
            string input = inputTextBox.Text;

            switchCaseTextBox.Text = SwitchCase(input);
            reverseTextBox.Text = Reverse(input);
            pigLatinTextBox.Text = PigLatin(input);
            shiftTextBox.Text = ShiftCypher(input,shift);
            subTextBox.Text = SubCypher(input, charsToSub);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

请尝试以下代码。要修复该按钮,请按照以下步骤进行操作

  1. 点击注释点击事件代码。
  2. 从表单
  3. 中删除按钮
  4. 添加新按钮。
  5. 将按钮名称更改为transformButton
  6. 双击按钮创建新的点击事件。
  7. 将原始代码添加到新点击事件

    private string SubCypher(string input, string charsToSub)
    {
        //Each letter in the word will be replaced by a letter from the corresponding position 
        //in a substitution alphabet. For this lab use this substitution alphabet: zeroabcdfghijklmnpqstuvwxy. 
        //Example: disk would become ofqh.
        input = input.ToLower();
        string alphabet = "abcdefghijklmnopqrstuvwxyz";
        string subAlpha = "zeroabcdfghijklmnpqstuvwxy";
        string str1 = input;
        string subCypher = subAlpha;
    
        string output = "";
        for (int i = 0; i < input.Length; i++)
        {
            string chr = input.Substring(i, 1);
            int index  = alphabet.IndexOf(chr);
            if (index >= 0)
            {
                output += subAlpha[index];
            }
            else
            {
                //character is not a letter
                output += chr;
            }
        }
        return output;
    }​