当前位置 : 主页 > 编程语言 > c语言 >

C#实验三和四

来源:互联网 收集:自由互联 发布时间:2021-06-25
//实验四using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp3{ class Program { static int add(int x = 0, int y = 0) { int z = x + y; return z; } static int mu

 

 

//实验四
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static int add(int x = 0, int y = 0)
        {
            int z = x + y;
            return z;
        }
        static int mul(int x = 0, int y = 0)
        {
            int z = x * y;
            return z;
        }

        static double add(double x = 0, double y = 0)
        {
            double z = x + y;
            return z;
        }

        static double mul(double x = 0, double y = 0)
        {
            double z = x * y;
            return z;
        }
        delegate int My(int x, int y);

        static void Main(string[] args)
        {
            Console.WriteLine("请输入两个整数:");
            int x = int.Parse(Console.ReadLine());
            int y = int.Parse(Console.ReadLine());
            if (x < 10 && y < 10)
            {
                My h = new My(add);
                Console.WriteLine(h(x, y));
            }
            else
            {
                My h = new My(mul);
                Console.WriteLine(h(x, y));
            }
            Console.WriteLine(mul(2, 7));
            Console.WriteLine(mul(2.0, 7.2));
        }
    }
}

 

实验三 1
  string number;
            string name;
            string educationBackground;
            string[] classRegular = new string[100];
            string[] classMaster = new string[100];
            string[] classDoctor = new string[100];
            int op = 0;
            classRegular[0] = "高级程序设计语言";
            classRegular[1] = "面向对象的程序设计";
            classRegular[2] = "计算机导论";

            classMaster[0] = "数据结构与算法";
            classMaster[1] = ".NET应用与开发";
            classMaster[2] = "计算机系统基础";

            classDoctor[0] = "计算机组成原理";
            classDoctor[1] = "编译原理";
            classDoctor[2] = "计算机网络";

            int classNumber = 0;
            int[] classHaveChosen = new int[3];

            Console.WriteLine("请输入您的学号:");
            number = Console.ReadLine().ToString();
            Console.WriteLine("请输入您的姓名:");
            name = Console.ReadLine().ToString();
            Console.WriteLine("请入您的学历:");
            educationBackground = Console.ReadLine().ToString();
            if(educationBackground == "本科")
            {
                op = 1;
                int j = 0;
                Console.WriteLine("本科可选的课程为:");
                for (int i = 0; i < 3; ++i)
                {
                    Console.WriteLine(i + "." + classRegular[i]);
                } 
                Console.WriteLine("请输入您要选择的课程的门数:");
                classNumber = int.Parse(Console.ReadLine());
                for(int i = 0; i < classNumber; i++)
                {
                    Console.WriteLine("请输入您要选择的课程前的序号:");
                    classHaveChosen[j++] = int.Parse(Console.ReadLine());
                }
            }
            else if(educationBackground == "硕士")
            {
                op = 2;
                int j = 0;
                Console.WriteLine("硕士可选的课程为:");
                for (int i = 0; i < 3; ++i)
                {
                    Console.WriteLine(i + "." + classMaster[i]);
                }
                Console.WriteLine("请输入您要选择的课程的门数:");
                classNumber = int.Parse(Console.ReadLine());
                for (int i = 0; i < classNumber; i++)
                {
                    Console.WriteLine("请输入您要选择的课程前的序号:");
                    classHaveChosen[j++] = int.Parse(Console.ReadLine());
                }
            }
            else if(educationBackground == "博士")
            {
                op = 3;
                int j = 0;
                Console.WriteLine("博士可选的课程为:");
                for (int i = 0; i < 3; ++i)
                {
                    Console.WriteLine(i + "." + classDoctor[i]);
                }
                Console.WriteLine("请输入您要选择的课程的门数:");
                classNumber = int.Parse(Console.ReadLine());
                for (int i = 0; i < classNumber; i++)
                {
                    Console.WriteLine("请输入您要选择的课程前的序号:");
                    classHaveChosen[j++] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("您的基本信息为:");
            Console.WriteLine("姓名:" + name);
            Console.WriteLine("学号:" + number);
            Console.WriteLine("学历:" + educationBackground);
            Console.WriteLine("您的选课信息为:");
            if(op == 1)
            {
                for(int i = 0; i < classNumber; i++)
                {
                    Console.WriteLine(i + "." + classRegular[classHaveChosen[i]]);
                }
            }
            else if(op == 2)
            {
                for (int i = 0; i < classNumber; i++)
                {
                    Console.WriteLine(i + "." + classMaster[classHaveChosen[i]]);
                }
            }
            else if(op == 3)
            {
                for (int i = 0; i < classNumber; i++)
                {
                    Console.WriteLine(i + "." + classDoctor[classHaveChosen[i]]);
                }
            }

 

//实验三 2
int[] xx = new int[100];
            int[] yy = new int[100];
            int[] zz = new int[100];
            int x = 0, y = 0;
            int i = 0;
            for (x = 0; x  <= 20; x++)
            {
                for (y = 0; y <= 33; y++)
                {
                    if (15 * x + 9 * y  + (100 - x - y) == 300)
                    {
                        xx[i] = x;
                        yy[i] = y;
                        zz[i] = 100 - x - y;
                        i++;
                    }
                }
            }
            Console.WriteLine("可能的解:");
            for(int j = 0; j < i; j++)
            {
                Console.WriteLine("x:" + xx[j] + " y:" + yy[j] + " z:" + zz[j]);
            }
网友评论