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

举报
小哈里 发表于 2022/05/10 23:20:32 2022/05/10
5k+ 0 0
【摘要】 1、断点调试 快捷键 F9:对选中的行设置断点F5:进入调试状态(不加断点时运行完程序不会直接退出)F11:逐语句调试,即进入下一个语句。F10:逐过程调试,即进入下一个过程(执行完整个函数)SHIT...

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 = daxie(plaintext);
                                break;
                            }
                        }
                        Console.WriteLine("\n请输入密钥(连续字母串):");
                        key = Console.ReadLine();
                        while (true)
                        {
                            if (daxie(key) == "!")
                            {
                                Console.WriteLine("\n输入错误,请重新输入密钥:");
                                key = Console.ReadLine();
                            }
                            else
                            {
                                key = daxie(key);
                                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请输入密钥(连续字母串):");
                        key = Console.ReadLine();
                        while (true)
                        {
                            if (daxie(key) == "!")
                            {
                                Console.WriteLine("\n输入错误,请重新输入密钥:");
                                key = Console.ReadLine();
                            }
                            else
                            {
                                key = daxie(key);
                                break;
                            }
                        }
                        jiemi(ciphertext, key);
                        break;
                    case "2":                                      //退出程序
                        Console.WriteLine("\n程序退出!!!");
                        return 0;
                        break;
                    default:
                        Console.WriteLine("输入错误,请重新选择!!!\n");
                        break;
                }
            }
        }
    }
}
  
 

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/120690535

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。