Visual Studio 2019 C# 断点调试 & 凯撒密码,单码密码实现

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Visual Studio 2019 C# 断点调试 & 凯撒密码,单码密码实现相关的知识,希望对你有一定的参考价值。

1、断点调试

快捷键

  • F9:对选中的行设置断点
  • F5:进入调试状态(不加断点时运行完程序不会直接退出)
  • F11:逐语句调试,即进入下一个语句。
  • F10:逐过程调试,即进入下一个过程(执行完整个函数)
  • SHITF+F11为跳出调试,即执行当前执行点所在函数的剩下所有行。
  • Ctrl+F5:进入运行状态(不调试)

    调试过程:
    1. 可以看到和修改实时值。
    2. 可以拖动选择下一条执行的语句。
    3. 可以实时编辑和修改代码

using System;

namespace Calculator
{
    
    class Program
    {
      
        static void swap(ref int a, ref int b)//引用
        {
            int t = a;  //断点F9
            a = b;      //逐语句F11
            b = t;      
            Console.WriteLine("swap {0}", a);//SHITF+F11执行当前执行点所在函数的剩下所有行。
            Console.WriteLine("swap {0}", t);
            Console.WriteLine("swap {0}", b);
        }
        static void Main(string[] args)
        {
            int a = 5, b = 10;
            Console.WriteLine("{0} {1}",  a, b);//1. 查看和修改变量
            Console.WriteLine("abcd");          //2.跳过语句
            swap(ref a, ref b);               //3.实时修改
           // Console.WriteLine("{0} {1}", a, b);
            swap(ref a, ref b);  //逐过程F10 (执行完整个函数)
            Console.WriteLine("{0} {1}", a, b);
        }
    }

}

2、凯撒密码、单码密码实现

凯撒密码:

using System;


namespace kaisa
{

    class Program
    {

        public static string daxie(string str)
        {                           //大写化字母以及检查函数
            string strs = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 'a' && str[i] <= 'z')
                {
                    char x;
                    x = (char)(str[i] - 32);
                    strs = strs + x;
                }
                else if (str[i] >= 'A' && str[i] <= 'Z')
                {
                    strs = strs + str[i];
                }
                else
                {
                    return "!";
                    break;
                }
            }
            return strs;
        }

        public static void jiami(string plaintexts, int keys)
        {
            //Console.WriteLine(keys);
            string strs="";
            for (int p = 0; p < plaintexts.Length; p++)
            {          //输出密文
                //Console.WriteLine((plaintexts[p] - 'A' + keys) % 26 + 'A'+'\\n');
                char ch = (char)((plaintexts[p] - 'A' + keys) % 26 + 'A');
                strs = strs+ch;
                       
            }
            Console.WriteLine("得到密文:");
            Console.WriteLine(strs + "\\n");
        }

        public static void jiemi(string plaintexts, int keys)
        {
            string strs = "";
            for(int p = 0; p < plaintexts.Length; p++)
            {
                char ch = (char)((plaintexts[p] - 'A' - keys + 26) % 26 + 'A');
                strs = strs + ch;
            }
            Console.WriteLine("得到明文:");
            Console.WriteLine(strs + "\\n");
        }

        static int Main(string[] args)
        {
            String choice;
            Console.WriteLine("凯撒加密/解密");
            while (true)
            {
                string plaintext;
                string ciphertext;
                int key;
                Console.WriteLine("输入数字选择模式:");
                Console.WriteLine("0.加密模式" + "\\n");
                Console.WriteLine("1.解密模式+\\n");
                Console.WriteLine("2.退出程序");
                choice = Console.ReadLine();
                switch (choice)
                {
                    case "0":                                      //加密
                        Console.WriteLine("\\n请输入明文(连续字母串):");
                        plaintext = Console.ReadLine();
                        while (true)
                        {
                            if (daxie(plaintext) == "!")
                            {
                                Console.WriteLine("输入错误,请重新输入明文:");
                                plaintext = Console.ReadLine();
                            }
                            else
                            {
                                plaintext = daxie(plaintext);
                                break;
                            }
                        }
                        Console.WriteLine("\\n请输入密钥(0-25数字):");
                        key = Convert.ToInt32(Console.ReadLine());
                        while (true)
                        {
                            if (key<0 || key>25)
                            {
                                Console.WriteLine("\\n输入错误,请重新输入密钥:");
                                key = Convert.ToInt32(Console.ReadLine());
                            }
                            else
                            {
                                break;
                            }
                        }
                        jiami(plaintext, key);
                        break;
                    case "1":                                      //解密
                        Console.WriteLine("\\n请输入密文(连续字母串):");
                        ciphertext = Console.ReadLine();
                        while (true)
                        {
                            if (daxie(ciphertext) == "!")
                            {
                                Console.WriteLine("\\n输入错误,请重新输入明文:");
                                ciphertext = Console.ReadLine();
                            }
                            else
                            {
                                ciphertext = daxie(ciphertext);
                                break;
                            }
                        }
                        Console.WriteLine("\\n请输入密钥(0-25数字):");
                        key = Convert.ToInt32(Console.ReadLine());
                        while (true)
                        {
                            if (key < 0 || key > 25)
                            {
                                Console.WriteLine("\\n输入错误,请重新输入密钥:");
                                key = Convert.ToInt32(Console.ReadLine());
                            }
                            else
                            {
                                break;
                            }
                        }
                        jiemi(ciphertext, key);
                        break;
                    case "2":                                      //退出程序
                        Console.WriteLine("\\n程序退出!!!");
                        return 0;
                        break;
                    default:
                        Console.WriteLine("输入错误,请重新选择!!!\\n");
                        break;
                }
            }
        }
    }
}


单码密码(单表代换):

using System;



namespace 单码
{


    class Program
    {
        public static string quchong(string str)
        {                          //密钥去重函数
            string strs = "";
            int[] vis = new int[26];
            for (int i = 0; i < str.Length; i++)
            {
                if (vis[str[i] - 'A'] == 0)
                {
                    strs = strs + str[i];
                }
                vis[str[i] - 'A']++;
            }
            return strs;
        }

        public static string daxie(string str)
        {                           //大写化字母以及检查函数
            string strs = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 'a' && str[i] <= 'z')
                {
                    char x;
                    x = (char)(str[i] - 32);
                    strs = strs + x;
                }
                else if (str[i] >= 'A' && str[i] <= 'Z')
                {
                    strs = strs + str[i];
                }
                else
                {
                    return "!";
                    break;
                }
            }
            return strs;
        }

        public static void jiami(string plaintexts, string keys)
        {         //加密函数
            string miyao;
            miyao = quchong(keys);
            int[] vis = new int[26];
            for (int i = 0; i < miyao.Length; i++)
            {
                vis[miyao[i] - 'A']++;
            }
            string canzhaos = miyao;
            string tmp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for (int i = 0; i < 26; i++)
            {
                if (vis[i] == 0)
                {
                    canzhaos = canzhaos + tmp[i];
                }
            }
            //Console.WriteLine(canzhaos);
            string ans = "";
            for (int p = 0; p < plaintexts.Length; p++)
            {
                ans = ans + canzhaos[(int)(plaintexts[p] - 'A')];
            }
            Console.WriteLine("得到密文:");
            Console.WriteLine(ans + "\\n");
        }

        public static void jiemi(string ciphertext, string keys)
        {         //加密函数
            string miyao;
            miyao = quchong(keys);
            int[] vis = new int[26];
            for (int i = 0; i < miyao.Length; i++)
            {
                vis[miyao[i] - 'A']++;
            }
            string canzhaos = miyao;
            string tmp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for (int i = 0; i < 26; i++)
            {
                if (vis[i] == 0)
                {
                    canzhaos = canzhaos + tmp[i];
                }
            }
            char[] duizhao = new char[26];
            for (int i = 0; i < canzhaos.Length; i++)
            {
                duizhao[canzhaos[i] - 'A'] = tmp[i];
            }
            string duizhaos = "";
            for (int i = 0; i < 26; i++)
            {
                duizhaos = duizhaos + duizhao[i];
            }

            //Console.WriteLine(canzhaos);
            string ans = "";
            for (int p = 0; p < ciphertext.Length; p++)
            {
                ans = ans + duizhaos[(int)(ciphertext[p] - 'A')];
            }
            Console.WriteLine("得到明文:");
            Console.WriteLine(ans + "\\n");
        }

        static int Main(string[] args)
        {
            String choice;
            Console.WriteLine("单码加密/解密");
            while (true)
            {
                string plaintext;
                string ciphertext;
                string key;
                Console.WriteLine("输入数字选择模式:");
                Console.WriteLine("0.加密模式" + "\\n");
                Console.WriteLine("1.解密模式+\\n");
                Console.WriteLine("2.退出程序");
                choice = Console.ReadLine();
                switch (choice)
                {
                    case "0":                                      //加密
                        Console.WriteLine("\\n请输入明文(连续字母串):");
                        plaintext = Console.ReadLine();
                        while (true)
                        {
                            if (daxie(plaintext) == "!")
                            {
                                Console.WriteLine("输入错误,请重新输入明文:");
                                plaintext = Console.ReadLine();
                            }
                            else
                            {
                                plaintext = daxi

以上是关于Visual Studio 2019 C# 断点调试 & 凯撒密码,单码密码实现的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 2010 C# 调试器不会在新断点处停止

Visual Studio:断点排除来自特定函数的调用

在 Visual Studio 中添加运行时断点如何工作?

Visual Studio 中方形/菱形断点的含义是啥?

如何在 Visual Studio 2017 中修复断点表达式

为啥 Visual Studio 有时会使用旧代码运行?