c#程序设计第四章练习

例题:

proj4-1

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

namespace proj4_1
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] a = new double[10] { 0, 1.2, 2.5, 3.1, 4.6, 5.0, 6.7, 7.6, 8.2, 9.8 };
            double k;
            int low = 0, high = 9, mid;
            Console.Write("k:");
            k = double.Parse(Console.ReadLine());
            while (low <= high)
            {
                mid = (low + high) / 2;
                if (a[mid] == k)
                {
                    Console.WriteLine("a[{0}]={1}", mid, k);
                    return;        //返回
                }
                else if (a[mid] > k)
                    high = mid - 1;
                else
                    low = mid + 1;
            }
            Console.WriteLine("未找到{0}", k);
        }
    }
}

proj4-2

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

namespace proj4_2
{
    class Program
    {
        const int N = 10;
        static void Main(string[] args)
        {
            int i, j;
            int[,] a = new int[N, N];
            for (i = 1; i < N; i++)            //1列和对角线元素均为1
            { a[i, i] = 1; a[i, 1] = 1; }
            for (i = 3; i < N; i++)            //求第3~N行的元素值
                for (j = 2; j <= i - 1; j++)
                    a[i, j] = a[i - 1, j - 1] + a[i - 1, j];
            for (i = 1; i < N; i++)            //输出数序
            {
                for (j = 1; j <= i; j++)
                    Console.Write("{0,-2} ", a[i, j]);
                Console.WriteLine();
            }
        }
    }
}

proj4-3

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

namespace proj4_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, k;
            int[] myarr = new int[10];        //定义一个一维数组
            Random randobj = new Random();    //定义一个随机对象
            for (i = myarr.GetLowerBound(0);
                    i <= myarr.GetUpperBound(0); i++)
            {
                k = randobj.Next() % 20;        //返回一个0~19的正整数
                myarr.SetValue(k, i);        //给数组元素赋值
            }
            Console.Write("随机数序:");
            for (i = myarr.GetLowerBound(0); i <= myarr.GetUpperBound(0); i++)
                Console.Write("{0} ", myarr.GetValue(i));
            Console.WriteLine();
            Array.Sort(myarr);                //数组排序
            Console.Write("排序数序:");
            for (i = myarr.GetLowerBound(0); i <= myarr.GetUpperBound(0); i++)
                Console.Write("{0} ", myarr.GetValue(i));
            Console.WriteLine();
        }
    }
}

proj4-4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;    //新增

namespace proj4_4
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList myarr = new ArrayList();
            myarr.Add("Smith");
            myarr.Add("Mary");
            myarr.Add("Dava");
            myarr.Add("John");
            Console.Write("排序前序列:");
            foreach (String sname in myarr)
                Console.Write(sname + " ");
            Console.WriteLine();
            myarr.Sort();
            Console.Write("排序后序列:");
            foreach (String sname in myarr)
                Console.Write(sname + " ");
            Console.WriteLine();
        }
    }
}

proj4-5

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

namespace proj4_5
{
    struct Stud                        //结构类型声明
    {
        public int sno;                //学号
        public string sname;            //姓名
    };
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            List<Stud> myset = new List<Stud>();    //定义一个集合myset
            Stud s1 = new Stud();
            s1.sno = 101;s1.sname = "李明";
            myset.Add(s1);                //向集合myset中添加一个结构变量
            Stud s2 = new Stud();
            s2.sno = 103; s2.sname = "王华";
            myset.Add(s2);             //向集合myset中添加一个结构变量
            Stud s3 = new Stud();
            s3.sno = 108; s3.sname = "张英";
            myset.Add(s3);             //向集合myset中添加一个结构变量
            Stud s4 = new Stud();
            s4.sno = 105; s4.sname = "张伟";
            myset.Add(s4);             //向集合myset中添加一个结构变量
            Console.WriteLine("元素序列:");
            Console.WriteLine("  下标  学号  姓名");
            i = 0;
            foreach (Stud st in myset)    //输出集合myset中所有元素
            {    Console.WriteLine("   {0}    {1}   {2}",i,st.sno, st.sname);
                i++;
            }
            Console.WriteLine("容量: {0}", myset.Capacity);
            Console.WriteLine("元素个数: {0}", myset.Count);
            Console.WriteLine("在索引2处插入一个元素");
            Stud s5 = new Stud();
            s5.sno = 106; s5.sname = "陈兵";
            myset.Insert(2, s5);        //向集合myset中插入一个结构变量
            Console.WriteLine("元素序列:");
            Console.WriteLine("  下标  学号  姓名");
            i = 0;
            foreach (Stud st in myset)    //输出集合myset中所有元素
            {    Console.WriteLine("   {0}    {1}   {2}", i, st.sno, st.sname);
                i++;
            }
        }
    }
}

编程题:

4-1

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

namespace exci4_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[10] { 1, 8, 3, 4, 7, 9, 6, 10, 2, 5 };
            int n = 10, max1, max2, i;
            max1 = a[0] > a[1] ? a[0] : a[1];
            max2 = a[0] > a[1] ? a[1] : a[0];
            for (i = 2; i < n; i++)
                if (max1 < a[i])
                {
                    max2 = max1;
                    max1 = a[i];
                }
            Console.WriteLine("max1={0},max2={1}", max1, max2);
        }
    }
}

4-2

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

namespace exci4_2
{
    class Program
    {
        static void Main(string[] args)
        {
            const int Max = 5;                          //考生数
            int[] Ave = new int[Max];                   //定义一个一维数组存储考生的总成绩
            int[,] grade ={{88,75,62,84},{96,85,75,92},  //定义二维数组存储考生成绩
                          {68,63,72,78},{95,89,76,98},
                          {76,65,72,63}};
            for (int i = 0; i < Max; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Ave[i] += grade[i, j];       //累加考生成绩
                }
            }
            for (int k = 0; k < Max; k++)
                Console.WriteLine("考生{0}平均成绩={1} ", k + 1, Ave[k] / 4.0);
        }
    }
}

实验题:

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

namespace experment4
{
    class Program
    {
        const int Max = 5;
        static void disp(int[] no, string[] name, string str)
        {
            Console.WriteLine(str);
            Console.Write("学号:\t");
            for (int i = 0; i < no.Length; i++)
                Console.Write("{0}\t", no[i]);
            Console.WriteLine();
            Console.Write("姓名:\t");
            for (int i = 0; i < name.Length; i++)
                Console.Write("{0}\t", name[i]);
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            int[] no = new int[] { 2, 4, 5, 1, 3 };
            string[] name = new string[] { "Smith", "John", "Mary", "Cherr", "Tomn" };
            disp(no, name, "排序前:");
            Array.Sort(no, name);
            disp(no, name, "按学号排序后:");
            Array.Sort(name, no);
            disp(no, name, "按姓名排序后:");
        }
    }
}

 

posted @ 2020-09-28 09:09  ~fyd~  阅读(75)  评论(0编辑  收藏  举报