码迷,mamicode.com
首页 > 其他好文 > 详细

结构体集合面向对象

时间:2015-05-21 23:57:04      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

namespace wwz
{
    class Program
    {
        struct student
        {
            public string name;
            public int chengji;
            public int cno;
        
        }
        public ArrayList paixu(ArrayList a)   //排序函数
        {
            int n = a.Count;
            for (int i = 1; i <= a.Count; i++)
            {
                for (int j = 1; j <= a.Count - i; j++)
                {
                 
                    if (((student)a[j - 1]).chengji < ((student)a[j]).chengji)
                    {   student temp ;
                        temp = (student)a[j - 1];
                        a[j - 1] = (student)a[j];
                        a[j] = temp;
                    }
                }
            }
            return a;
        }
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            student a = new student();
            Console.WriteLine("请输入人数");
            int b = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= b; i++)
            {
                Console.WriteLine("请输入您的名字");
                a.name = Console.ReadLine();
                Console.WriteLine("请输入您的成绩");
                a.chengji = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入您的学号");
                a.cno = Convert.ToInt32(Console.ReadLine());
                al.Add(a);
            }

             new Program().paixu(al);//调用函数进行排序
        

            for (int i = 0; i < al.Count; i++)
            {
                Console.WriteLine("名字为" + ((student)al[i]).name + "成绩为" + ((student)al[i]).chengji + "学号为" + ((student)al[i] ).cno);
            }
            double n = 0;  //定义总分
            for (int i = 0; i < b; i++)
            {
                n +=((student)al[i]).chengji; //总分=总分+每个人的成绩
            } 
            Console.WriteLine(n); //输出总分
            double avg = n / b;//定义平均分的公式
            Console.WriteLine(avg);//输出平均分
            Console.ReadLine();
        }
    }
}

 

结构体集合面向对象

标签:

原文地址:http://www.cnblogs.com/lk-kk/p/4520936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!