NET程序设计实验-1

发布时间 2024-01-09 15:20:02作者: wardream

实验一  语言基础

一、实验目的

1. 熟悉Visual Stido.NET 实验环境;

2. 掌握控制台程序的编写方法;

3. 掌握C#程序设计语言的语法基础;

4. 掌握控制语句和数组的使用。

二、实验要求

  根据题目要求,编写 C#程序,并将程序代码和运行结果写入实验报告。

三、实验内容

由于没有事先看清实验要求,我将实验一的五个实验写到了一个程序内并能循环运行,所以这里先附上代码,每个实验会单独截图

// See https://aka.ms/new-console-template for more information

//Console.WriteLine("Hello, World!");

using System;

 

class Program

{

    static void Main()

    {

        while (true)

        {

            Console.WriteLine("请选择实验内容:");

            Console.WriteLine("    1.计算边长面积");

            Console.WriteLine("    2.判断月份");

            Console.WriteLine("    3.最少鸡蛋数量");

            Console.WriteLine("    4.奇数和与偶数和");

            Console.WriteLine("    5.二维数组鞍点");

            Console.WriteLine("****输入0以退出程序****");

            int choice = int.Parse(Console.ReadLine());

 

            switch (choice)

            {

                case 0:

                    return;

                case 1:

                    test1();

                    break;

                case 2:

                    test2();

                    break;

                case 3:

                    test3();

                    break;

                case 4:

                    test4();

                    break;

                case 5:

                    test5();   

                    break;

                default:

                    Console.WriteLine("无效的选择");

                    break;

            }

 

           

        }

 

    }

 

    static void test1()

    {

        Console.WriteLine("请选择形状(1. 三角形  2. 长方形):");

        int choice1 = int.Parse(Console.ReadLine());

 

        switch (choice1)

        {

            case 1:

                ProcessTriangle();

                break;

            case 2:

                ProcessRectangle();

                break;

            default:

                Console.WriteLine("无效的选择");

                break;

        }

 

        //Console.ReadLine(); // 保持控制台窗口打开

    }

    static void ProcessTriangle()

    {

        Console.WriteLine("请输入三角形的三边长:");

        double sideA = double.Parse(Console.ReadLine());

        double sideB = double.Parse(Console.ReadLine());

        double sideC = double.Parse(Console.ReadLine());

 

        double perimeter = sideA + sideB + sideC;

 

        // 使用海伦公式计算三角形面积

        double s = perimeter / 2;

        double area = Math.Sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));

 

        Console.WriteLine($"三角形的周长为: {perimeter}");

        Console.WriteLine($"三角形的面积为: {area}");

    }

 

    static void ProcessRectangle()

    {

        Console.WriteLine("请输入长方形的长度和宽度:");

        double length = double.Parse(Console.ReadLine());

        double width = double.Parse(Console.ReadLine());

 

        double perimeter = 2 * (length + width);

        double area = length * width;

 

        Console.WriteLine($"长方形的周长为: {perimeter}");

        Console.WriteLine($"长方形的面积为: {area}");

    }

 

 

    static void test2()

    {

        Console.WriteLine("请输入月份(1-12):");

        int month = int.Parse(Console.ReadLine());

 

        string season = GetSeason(month);

 

        if (season != null)

        {

            Console.WriteLine($"该月份处于{season}季节。");

        }

        else

        {

            Console.WriteLine("无效的月份。");

        }

 

       // Console.ReadLine();

    }

 

    static string GetSeason(int month)

    {

        switch (month)

        {

            case 1:

            case 2:

            case 12:

                return "冬季";

            case 3:

            case 4:

            case 5:

                return "春季";

            case 6:

            case 7:

            case 8:

                return "夏季";

            case 9:

            case 10:

            case 11:

                return "秋季";

            default:

                return null;

        }

    }

 

 

    static void test3()

    {

        int eggs = FindMinimumEggs();

        Console.WriteLine($"这篮鸡蛋至少有 {eggs} 个。");

    }

    static int FindMinimumEggs()

    {

        int eggs = 2;

 

        while (true)

        {

            if (CheckConditions(eggs))

            {

                return eggs;

            }

 

            eggs ++; // 优化:每次递增4,因为条件是每4个数为一个循环

        }

    }

    static bool CheckConditions(int n)

    {

        return n % 2 == 1 && n % 3 == 1 && n % 4 == 1;

    }

 

 

    static void test4()

    {

        Console.WriteLine("请输入整数数组(以空格分隔):");

        string input = Console.ReadLine();

 

        // 将输入字符串拆分成整数数组

        int[] numbers = Array.ConvertAll(input.Split(' '), int.Parse);

 

        int oddSum = CalculateSum(numbers, true);

        int evenSum = CalculateSum(numbers, false);

 

        Console.WriteLine($"奇数之和: {oddSum}");

        Console.WriteLine($"偶数之和: {evenSum}");

    }

    static int CalculateSum(int[] array, bool isOdd)

    {

        int sum = 0;

 

        foreach (var number in array)

        {

            if (isOdd && number % 2 != 0)

            {

                sum += number;

            }

            else if (!isOdd && number % 2 == 0)

            {

                sum += number;

            }

        }

 

        return sum;

    }

 

 

    static void test5()

    {

        Console.WriteLine("请输入二维数组的行数和列数(以空格分隔):");

        string[] sizes = Console.ReadLine().Split(' ');

 

        int rows = int.Parse(sizes[0]);

        int cols = int.Parse(sizes[1]);

 

        int[,] matrix = new int[rows, cols];

 

        Console.WriteLine($"请输入 {rows} 行 {cols} 列的二维数组元素(以空格分隔):");

 

        // 获取用户输入的二维数组元素

        for (int i = 0; i < rows; i++)

        {

            string[] elements = Console.ReadLine().Split(' ');

 

            for (int j = 0; j < cols; j++)

            {

                matrix[i, j] = int.Parse(elements[j]);

            }

        }

 

        FindSaddlePoint(matrix);

    }

    static void FindSaddlePoint(int[,] matrix)

    {

        int rows = matrix.GetLength(0);

        int cols = matrix.GetLength(1);

        bool found = false;

 

        for (int i = 0; i < rows; i++)

        {

            int maxInRow = matrix[i, 0];

            int colIndex = 0;

 

            // 找到行中的最大值

            for (int j = 1; j < cols; j++)

            {

                if (matrix[i, j] > maxInRow)

                {

                    maxInRow = matrix[i, j];

                    colIndex = j;

                }

            }

 

            // 检查最大值是否为列中的最小值

            bool isSaddlePoint = true;

            for (int k = 0; k < rows; k++)

            {

                if (matrix[k, colIndex] < maxInRow)

                {

                    isSaddlePoint = false;

                    break;

                }

            }

 

            // 如果是鞍点,输出位置和值

            if (isSaddlePoint)

            {

                Console.WriteLine($"鞍点位置: ({i + 1}, {colIndex + 1}), 值: {maxInRow}");

                found = true;

            }

        }

 

        if (!found)

        {

            Console.WriteLine("数组中不存在鞍点。");

        }

    }

}

 

  1. 编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出。

 

 

2.编写一个控制台应用程序,可根据输入的月份判断所在季节。

 

3. 编写程序,用 while 循环语句实现下列功能:有一篮鸡蛋,不止一个,有人两个两

个数,多余一个,三个三个数,多余一个,再四个四个地数,也多余一个,请问这篮鸡蛋至

少有多少个。

 

4.编写程序,计算数组中奇数之和和偶数之和。

 

5. 编写程序,找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该

列上最小。有可能数组没有鞍点)。要求:

u   二维数组的大小、数组元素的值在运行时输入;

u   程序有友好的提示信息。

 

四、实验总结

控制台程序基本使用起来和之前学过的C++很像,可以轻松上手,中途遇到的第一个问题是程序的结合,也就是多个实验汇总到一个程序,因为我嫌弃每个实验都弄一个程序太麻烦,用到了之前用过的while循环,之后的几个实验出现的问题主要集中在输入上,学习了一下C#如何一次性输入未知大小的数组,整个实验就结束了。需要注意的是程序还有bug,比如说数组最后多输入了一个空格就会报错的问题,目前还没找到好的解决办法。

注:本部分写本次实验过程中出现的问题、如何解决、注意事项、以及自己的经 验体会。