C#NET程序设计教程实验指导(清华大学江红,余青松)实验源码第四章


 

 

4.1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c4._1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program test = new Program();
            double [] height = new double[10]{156, 150, 167, 178, 180, 176, 173, 154, 155, 158 };
            Console.WriteLine("学生身高如下");
            test.show(height);
            double max = 0.0;
            double min = 0.0;//C#不允许未初始化的变量!
            test.maxWithMin(height,ref max,ref min);//必须用引用传递
            double avg=test.avg(height);
            Console.Write("平均身高={0},最高身高={1},最低身高={2}", max, min, avg);
            int n = test.heigher(height, avg);
            Console.WriteLine("\n高于平均身高的学生人数={0}", n);
            Console.ReadKey();
        }
        public void show(double[] arr)
        {
            foreach (double i in arr)
            {
                Console.Write(" {0}", i);
            }
            Console.WriteLine();
        
        }
        public void maxWithMin(double []arr,ref double min,ref double max)
        {
             max=arr[0];
             min=arr[0];
            //int size = arr.Length;
            for (int i = 0; i < arr.Length; i++)
            {
                if (max < arr[i])
                    max = arr[i];
                if (min > arr[i])
                    min = arr[i];
            }
        }
        public double avg(double[] arr)
        { 
            double average;
            double sum=0;
            for(int i=0;i<arr.GetLength(0);i++)
                sum+=arr[i];
            average=sum*1.0/arr.GetLength(0);

            return average;
        }
        public int heigher(double[] arr, double avg)
        {
            int count = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] > avg)
                    count++;
            }
            return count;
        }
    }
}

4.2

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

namespace c4._2
{
    class Program
    {
        static void Main(string[] args)
        {
            int []score = new int[]{ 80, 90, 67, 89, 78, 85, 45, 69, 77, 95 };
            (new Program()).show(score);
            Console.ReadKey();
        }
        public void show(int[] score)
        {
            int bad = 0;
            int min = 0;
            int good = 0;
            int well=0;//90-100
            foreach (int i in score)
            {
                if (i>=90)
                    well++;
                else if(i>=80)
                    good++;
                else if(i>=60)
                    min++;
                else
                    bad++;
            }
            int size=score.Length;
            Console.WriteLine("优(90-100)分数段的学生人数={0},所占百分比={1}%",well,well*100.0/size);
            Console.WriteLine("良(80-89)分数段的学生人数={0},所占百分比={1}%",good,good*100.0/size);
            Console.WriteLine("中(60-79)分数段的学生人数={0},所占百分比={1}%",min,min*100.0/size);
            Console.WriteLine("差(0-59)分数段的学生人数={0},所占百分比={1}%",bad,bad*100.0/size);

        
        
        
        
        }
    }
}

4.3

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

namespace c4._3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = new int[10];
            Program p=new Program();
            Console.WriteLine("原始数组:");
            p.randIt(num);
            p.show(num);
            p.sort(num);
            Console.WriteLine();
            Console.WriteLine("降序数组");
            p.show(num);
            Console.ReadKey();


        }
        public void randIt(int[] num)
        {
            Random randNum = new Random();
            for (int i = 0; i < num.Length; i++)
            {
                
                num[i]= randNum.Next(101);//101??
            }
        }
        public void sort(int[] arr)
        { 
            for(int i=0;i<arr.Length;i++)
                for (int j = 0; j < arr.Length-i-1; j++)//看清楚j的终止条件
                {
                    if (arr[j]< arr[j+1])
                    {
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j +1] = temp;
                    }
                }
        
        }
        public void show(int[] arr)
        {
            foreach (int i in arr)
            {
                Console.Write("{0} ", i);
            }
        
        }
    }
}

4.4

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

namespace c4._4
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = new int[10];
            Program p = new Program();
            Console.WriteLine("原始数组:");
            p.randIt(num);
            p.show(num);
            p.sort(num);
            Console.WriteLine();
            Console.WriteLine("降序数组");
            p.show(num);
            Console.ReadKey();
        }
        public void randIt(int[] num)
        {
            Random randNum = new Random();
            for (int i = 0; i < num.Length; i++)
            {

                num[i] = randNum.Next(101);//101??  
            }
        }
        //这是选择排序的算法  
        public void sort(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
                for (int j = i; j < arr.Length; j++)//看清楚j的终止条件  
                {  //选择跟冒泡有点不同,是先排第一个,然后往后排
                    if (arr[i] < arr[j])
                    {
                        int temp = arr[j];
                        arr[j] = arr[i];
                        arr[i] = temp;
                    }
                }

        }
        public void show(int[] arr)
        {
            foreach (int i in arr)
            {
                Console.Write("{0} ", i);
            }

        }
    }
}
    

4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Array;
namespace c4._5
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] A = new int[4,4];
            int[,] B = new int[4, 4];
            int[,] C = new int[4, 4];
            int[,] D = new int[4, 4];

            Program p = new Program();
            p.RandIt(A, 50, 100);
            p.RandIt(B, 0, 10);
            Console.WriteLine("数组A的内容:");
            p.show(A);
            Console.WriteLine("数组B的内容:");
            p.show(B);
            Console.WriteLine("上三角显示数组A的内容:");
            p.showTriangle(A,0);
            Console.WriteLine("下三角显示数组B的内容:");
            p.showTriangle(B,1);
            p.add(A, B, C);
            Console.WriteLine("数组A+B的结果:");
            p.show(C);
           
            Console.WriteLine("数组A-B的结果:");
            p.dec(A, B, C);
            p.show(C);


            Console.ReadKey();

        }
        public void dec(int[,] a, int[,] b, int[,] c)
        {
            int ax = a.GetLength(0);
            int ay = a.GetLength(1);
            int bx = b.GetLength(0);
            int by = b.GetLength(1);
            int cx = c.GetLength(0);
            int cy = c.GetLength(1);

            if (ax == bx && ax == cx && ay == by && by == cx)
            {
                for (int i = 0; i < ax; i++)
                    for (int j = 0; j < bx; j++)
                        c[i, j] = a[i, j] - b[i, j];
            }
            else
                Console.WriteLine("Can't add it !");
        }
        public void add(int[,] a, int[,] b, int[,] c)
        {
            int ax = a.GetLength(0);
            int ay = a.GetLength(1);
            int bx = b.GetLength(0);
            int by = b.GetLength(1);
            int cx = c.GetLength(0);
            int cy = c.GetLength(1);

            if (ax == bx && ax == cx && ay == by && by == cx)
            {
                for (int i = 0; i < ax; i++)
                    for (int j = 0; j < bx; j++)
                        c[i, j] = a[i, j] + b[i, j];               
            }
            else
                Console.WriteLine("Can't add it !");
        }


        public void RandIt(int[,] arr, int randNumMin,int randNumMax)
        {
            int sizeX = arr.GetLength(0);
            int sizeY = arr.GetLength(1);
            Random rand=new Random();
            for (int i = 0; i < sizeX; i++)
            {
                for (int j = 0; j < sizeY; j++)
                {
                    arr[i,j] = rand.Next(randNumMin, randNumMax);
                }
            }
            
        
        }
        public void showTriangle(int[,] arr, int flag)//flag=0,上三角,=1,下三角
        {

            int sizeX = arr.GetLength(0);
            int sizeY = arr.GetLength(1);
            string ch=" ";
         
            for (int i = 0; i < sizeX; i++)
            {
                for (int j = 0; j <sizeY; j++)
                {
                    if(flag==0)//上三角
                    { 
                        
                        if (i<=j)
                            Console.Write("{0,4}", arr[i, j]);
                        else { 
                            //for (int f = 0; f < j * 10;f++ )
                                Console.Write("{0,4}",ch);
                        
                        }
                    }
                    else if(flag==1)//下三角
                    {
                        if (i>=j)
                            Console.Write("{0,4}", arr[i, j]);
                        else
                            Console.Write("{0,4}",ch);
                    
                    }
                }
                Console.WriteLine();
            }
            }
           
        
        public void show(int [,]num)
        {
            int sizeX = num.GetLength(0);
            int sizeY = num.GetLength(1);
            
            for (int i = 0; i < sizeX; i++)
            {
                for (int j = 0; j < sizeY; j++)
                {
                    Console.Write(" {0}", num[i,j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }


    }
}

4.6

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

namespace c4._6
{
    class Program
    {
        const int MAX= 10;
        static void Main(string[] args)
        {
            Program.showTriangle(MAX);
            Console.ReadKey();
        }
        public static void showTriangle(int LINE)
        { 
            int [,]triangle=new int[LINE,LINE];
            for (int i = 0; i < LINE; i++)//先令每一个元素都为1
                for (int j = 0; j < LINE; j++)
                    triangle[i, j] = 1;
            for (int i = 0; i < LINE; i++)//构造杨辉三角
                for (int j = 1; j < i; j++)//每排第一个和最后一个不变            
                    triangle[i, j] = triangle[i - 1, j] + triangle[i - 1, j - 1];
            //其他的为上一层头顶和左边一个之和

            for (int i = 0; i < LINE; i++)
            {
                for (int j = 0; j <= i; j++)
                    Console.Write(" {0,4}", triangle[i, j]);
                Console.WriteLine();
            }
        }
    }
}

4.7

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

namespace c4._7
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new[] { 80, 90, 67, 89, 78, 85, 45, 69, 77, 95 };
            Console.WriteLine("数组的维数:{0}", a.Rank);
            Console.WriteLine("数组的长度(元素总数):{0}", a.Length);
            Program p = new Program();
            Console.WriteLine("排序前的内容:");
            p.show(a);
            Console.WriteLine("排序后的内容:");
            Array.Sort(a);
            p.show(a);
            Console.WriteLine("反转后的内容:");
            Array.Reverse(a);//利用反向迭代器
            p.show(a);
            Console.ReadKey();

        }
        public void show(int[] a)
        { 
            foreach(int i in a)
                Console.Write("{0,4}",i);
            Console.WriteLine();
        }
    }
}

4.8

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

namespace c4._8
{
    unsafe class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            int *pa,pb,pc;//三个都是指针?c#真是个神奇的东东啊
            Console.Write("请输入整数a:");
            String s=Console.ReadLine();
            a = int.Parse(s);
            Console.Write("请输入整数b:");
             s = Console.ReadLine();
             b = int.Parse(s);
            Console.Write("请输入整数c:");
             s = Console.ReadLine();
             c = int.Parse(s);
             Console.WriteLine("原始数据:a={0},b={1},c={2}", a, b, c);
             pa = &a;
             pb = &b;
             pc = &c;
             if (a > b) swap(pa,pb);
             if (a > c) swap(pa, pc);
             if (b > c) swap(pb, pc);
             Console.WriteLine("升序排序:a={0},b={1},c={2}", a, b, c);
             Console.ReadKey();

        }
        public static void swap(int* a, int* b)
        {
            int temp = *a ;
            *a = *b;
            *b = temp;
            
        }
    }
}

——————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:https://www.royalchen.com/

author:royalchen

Email:royalchen@royalchen.com

———————————————————————————————————————————————————

 

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注