c#实验一中闪亮F5

  实验一 C#编程基础

一、实验目的

 1.掌握Visual StudioC#编程界面)和调试的基本方法;

 2.掌握C#类型系统;

 3.掌握C#控制语句用法;

 4.掌握数组的用法;

 

二、实验内容

 

(实验过程中编写的程序复制到本文件中,下课整理后上交)

1.  有个小学生的数学题目,啤酒2元钱一瓶,4个瓶盖换1瓶,2个空瓶子换1瓶。现利用程序来计算。每个金额可购买的多少啤酒。(不允许从别处借瓶盖和瓶子)

定义方法:int CanBuy(int money)

 

在主方法调用它,由用户输入金额,对返回的啤酒数量用Debug.Assert()进行判定,假如输入10元钱,可判定输出是15瓶。

using System;
using System.Diagnostics;

namespace ConsoleApp2
{
    class Program
    {
        public static int maxCount = 0;

          public static void CanBuy(int money, int cap, int bottle, int count) {
        if (cap < 4 && bottle < 2 && money < 2) {
            if (maxCount < count) {
                maxCount = count;
            }
            return;
        }
        if (cap >= 4) {
            //Debug.WriteLine($"{cap}");
            CanBuy(money, cap - 4 + 1, bottle + 1, count + 1);
        }
        if (bottle >= 2) {
            //Debug.WriteLine($"{bottle}");
            CanBuy(money, cap + 1, bottle - 2 + 1, count + 1);
        }
        if (money >= 2) {
            //Debug.WriteLine($"{money}");
            CanBuy(money - 2, cap + 1, bottle + 1, count + 1);
        }
        return;
 
    }
            static void Main(string[] args)
        {
            int money = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(money);
            CanBuy(money,0,0,0);
            //Debug.Assert(maxCount == 15, "10元钱可以买15瓶");
            Console.WriteLine("总数:" + maxCount);

        }
    }
}

 

3.  字符与字符串操作。

1)创建控制台应用程序,在程序主方法中,由用户指定一个字符串text(可以是名字、可以是一段话,中文英文均可),以及一个加密字符key(如果给中文加密,key最好是数字或英文字符)。

(提示:加密字符通过Console.ReadKey().KeyChar输入)

2)进行字符串加密。此处采用的加密方法是:将密钥字符与字符串中的每个字符进行异或运算。如’a’’l’异或结果为字符’P’。注意类型转换。

编译运行程序,查看字符串加密结果。

3)进行字符串解密。

原理:异或运算具有可逆性,如字符’P’’l’的异或仍为字符’a’。编写代码取回原字符串并输出。

输出格式参考:

即将加密以下文字:Console.WriteLine

请输入加密字符:

加密后的字符串为:扒找承扢找扽扴房扆扣扸扥扴扝扸承扴

 

解码后的字符串为:Console.WriteLine

 

using System;
using System.Diagnostics;

namespace ConsoleApp3
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("请输入一个字符串:");
            String str1 = Console.ReadLine();
            Console.WriteLine("请输入加密字符:");
            char protect = Console.ReadKey().KeyChar;
            Console.WriteLine();
            string str2 = "";
            for(int i = 0; i < str1.Length; i++)
                str2 += (char)(str1[i] ^ protect);
            Console.WriteLine("加密后的字符串:" + str2);
            str1 = "";
            for (int i = 0; i < str2.Length; i++)
                str1 += (char)(str2[i] ^ protect);
            Console.WriteLine("解密后的字符串:" + str1);

        }
    }
}

 

4.  写一个方法,可对两个矩阵求和。方法定义如下:

double[,] AddMatrix(double[,] a, double[,] b)

方法首先要判断矩阵的维度是一样的。假设和为矩阵c,则cij = aij + bij。写一个测试程序,随机生成矩阵中的数据,将结果矩阵输出。样例如下:

1.0 2.0   |     0.0 2.0   |     1.0 4.0

 

3.0 4.0   |     4.0 1.0   |     7.0 5.0

 

using System;

namespace ConsoleApp2
{
    class Program
    {
        static Random rm = new Random();
        static int bian = rm.Next(3) + 1;

        static double[,] AddMatrix(double[,] a, double[,] b)
        {

            double[,] c = new double[bian, bian];
            for(int i = 0; i < bian; i++)
            {
                for(int j = 0; j < bian; j++)
                {
                    c[i, j] = a[i, j] + b[i, j];
                }
            }
            return c;
        }
        static void Main(string[] args)
        {

            double[,] a = new double[bian, bian];
            double[,] b = new double[bian, bian];
            double[,] d = new double[bian, bian];
            for (int i = 0; i < bian; i++)
            {
                for (int j = 0; j < bian; j++)
                {
                    a[i, j] = Math.Round( rm.Next(5,10)+rm.NextDouble(),2);//结合整数和小数
                    b[i, j] = Math.Round(rm.Next(5, 10) + rm.NextDouble(), 2);//结合整数和小数
                }
            }

            d = AddMatrix(a, b);

            for (int i = 0; i < bian; i++)
            {
                for (int j = 0; j < bian; j++)
                {
                    Console.Write(a[i, j] + " ");
                }
                Console.Write("|");
                for (int j = 0; j < bian; j++)
                {
                    Console.Write(a[i, j] + " ");
                }
                Console.Write("|");
                for (int j = 0; j < bian; j++)
                {
                    Console.Write(a[i, j] + " ");
                }
                Console.Write("|\n");
            }
        }

 

posted @ 2021-03-28 16:37  焕不涣  阅读(137)  评论(0编辑  收藏  举报