Net Core教程

WUST 何亨 ASP.NET WEB应用开发教程 实验二 C#教程(2学时)

本文主要是介绍WUST 何亨 ASP.NET WEB应用开发教程 实验二 C#教程(2学时),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

实验2  C#编程(2学时)

【实验目的】
        1、了解C#语言的特点。
        2、熟悉C#的开发环境。
        3、掌握用VS2015编写C#基本程序。
【实验内容】

         1、循环实现
        2、求出一维数组中的最大值和最小值,数组自定义。
        3、对一维数组进行冒泡排序,输出排序后的数组,数组自定义。
        4、存储和打印杨辉三角形,运行结果为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



using System;

namespace test_1
{
    class sum
    {
        private int[] array;

        sum(int n)
        {
            if (n <= 2)
                System.Console.WriteLine("请输入大于2的整数字");
            else
            {
                array = new int[n];
                array[0] = 1;
                array[1] = 2;
                for (int i = 2; i < n; i++)
                {
                    array[i] = array[i - 1] + array[i - 2];
                }
                /*System.Console.Write("数组:");
                foreach (int a in array)
                {
                    System.Console.Write(a+"\t");
                }*/
            }

            sum_10();

        }

        internal void sum_10()
        {
            double sum = 0;
            System.Console.Write("sum=");
            System.Console.Write(array[1] + "/" + array[0]);
            sum += array[1]*(1.0) / array[0];
            for (int i = 1; i < 10; i++)
            {
                if (i % 2 == 0)
                {
                    System.Console.Write("+" + array[i + 1] + "/" + array[i]);
                    sum += array[i + 1] * (1.0) / array[i];
                }
                else
                {
                    System.Console.Write("-" + array[i + 1] + "/" + array[i]);
                    sum -= array[i + 1] * (1.0) / array[i];
                }
            }
            System.Console.Write("={0,4:f2}",sum);
            Console.WriteLine();
        }

        static void Main(string[] args)
        {
            //第一题
            Console.WriteLine("第一题:");
            sum sum = new sum(20);


            //第二题
            Console.WriteLine();
            Console.WriteLine("第二题:");
            findMaxAndMin findamxandmin = new findMaxAndMin();


            //第三题
            Console.WriteLine();
            Console.WriteLine("第三题:");
            sortArray sortarray = new sortArray();


            //第四题
            Console.WriteLine();
            Console.WriteLine("第四题:");
            yanghuiTrangle yanghui = new yanghuiTrangle();

            Console.ReadKey();
        }
    }


    class findMaxAndMin{
        int[] array = { 55, 23, 56, -10, 100, 777, -999, 123, 167, 998 };

        public void findMax(){
            int max = array[0];
            for(int i = 0; i < array.Length; i++){
                if (max < array[i])
                    max = array[i];
            }
            Console.WriteLine("最大值为"+max);
        }

        public void findMin()
        {
            int min = array[0];
            for (int i = 0; i < array.Length; i++)
            {
                if (min > array[i])
                    min = array[i];
            }
            Console.WriteLine("最小值为" + min);
        }

        public findMaxAndMin()
        {
            findMax();
            findMin();
        }
    }


    class sortArray{
        int[] array = { 55, 23, 56, -10, 100, 777, -999, 123, 167, 998 };

        public void sortIncreace(){
            int[] array_ = array;
            for(int i = 0; i < array_.Length; i++)
            {
                for(int j = 0; j < array_.Length - i-1; j++)
                {
                    if (array_[j] > array_[j + 1]){
                        int tem;
                        tem = array_[j];
                        array_[j] = array_[j + 1];
                        array_[j + 1] = tem;
                    }
                }
            }
            Console.Write("升序:\t\t");
            foreach(int a in array_)
            {
                Console.Write(a + "\t");
            }
            Console.WriteLine();
        }


        public void sortDecrease()
        {
            int[] array_ = array;
            for (int i = 0; i < array_.Length; i++)
            {
                for (int j = 0; j < array_.Length - i-1; j++)
                {
                    if (array_[j] < array_[j + 1])
                    {
                        int tem;
                        tem = array_[j];
                        array_[j] = array_[j + 1];
                        array_[j + 1] = tem;
                    }
                }
            }
            Console.Write("降序:\t\t");
            foreach (int a in array_)
            {
                Console.Write(a + "\t");
            }
            Console.WriteLine();
        }

        public sortArray()
        {
            Console.Write("原数组:\t");
            foreach (int a in array)
            {
                Console.Write(a + "\t");
            }
            Console.WriteLine();
            sortIncreace();
            sortDecrease();
        }

    }


    class yanghuiTrangle
    {

        int[][] array = new int[10][];

        public void craetYanghui()
        {
            array[0] = new int[1] { 1 };
            for(int i = 1; i < 10; i++)
            {
                array[i] = new int[i + 1];
                array[i][0] = array[i][i] = 1;
                for (int j = 1; j < i; j++)
                {
                    array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
                }
                
            }
        }

        public void print()
        {
            for(int i = 0; i < 10; i++)
            {
                for(int j = 0; j < 10 - i - 1; j++)
                {
                    Console.Write("     ");
                }
                foreach (int a in array[i])
                {
                    string b = a + "";
                    int len = b.Length;
                    for(int k = 0; k < (5 - len) / 2; k++)
                    {
                        b = " " + b;
                    }
                    for (int k = 0; k < 5-(5 - len) / 2-len; k++)
                    {
                        b = b + " ";
                    }

                    Console.Write(b+"     ");
                }

                    
                Console.WriteLine();
            }
        }

        public yanghuiTrangle()
        {
            craetYanghui();
            print();
        }
        
    }
    
}

 

 

这篇关于WUST 何亨 ASP.NET WEB应用开发教程 实验二 C#教程(2学时)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!