| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1072 人关注过本帖
标题:c#排序不够精简,谁有更好的方法?
只看楼主 加入收藏
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:8 
c#排序不够精简,谁有更好的方法?
假设有5个学生,每个学生有6门功课,请设计程序求出各位学生的总分,平均分,求算出谁总分第一,谁总分最后。

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

namespace ConsoleApplication1
{
   
    public class Student
    {
        string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        double chinese;

        public double Chinese
        {
            get { return chinese; }
            set { chinese = value; }
        }
        double math;

        public double Math
        {
            get { return math; }
            set { math = value; }
        }
        double english;

        public double English
        {
            get { return english; }
            set { english = value; }
        }
        double politic;

        public double Politic
        {
            get { return politic; }
            set { politic = value; }
        }
        double chemistry;

        public double Chemistry
        {
            get { return chemistry; }
            set { chemistry = value; }
        }
        double biology;

        public double Biology
        {
            get { return biology; }
            set { biology = value; }
        }
        double sum;

        public double Sum
        {
            get { return sum; }
            set { sum = value; }
        }
        double pin;

        public double Pin
        {
            get { return pin; }
            set { pin = value; }
        }
        public Student(string name, double chinese, double math, double english, double politic, double chemistry, double biology)
        {
            this.name = name;
            this.chinese = chinese;
            this.math = math;
            this.english = english;
            this.politic =politic ;
            this.chemistry =chemistry ;
            this.biology =biology ;
        }
    }
    class Program   
    {        
        static void Main(string[] args)      
        {
            Student a = new Student("张一", 58, 52, 58, 96, 54, 56);
            Student b = new Student("李二", 50, 52, 58, 96, 54, 56);
            Student c = new Student("周三", 58, 52, 68, 96, 54, 56);
            Student d = new Student("马四", 58, 52, 58, 96, 54, 96);
            Student e = new Student("王五", 58, 32, 58, 96, 54, 56);
            double sum = 0;
            a.Sum  = a.Chinese + a.English + a.Biology + a.Chemistry + a.Math + a.Politic;
            a.Pin = a.Sum / 6;
            b.Sum  = b.Chinese + b.English + b.Biology + b.Chemistry + b.Math + b.Politic;
            b.Pin =  b.Sum / 6;
            c.Sum  = c.Chinese + c.English + c.Biology + c.Chemistry + c.Math + c.Politic;
            c.Pin = c.Sum / 6;
            d.Sum  = d.Chinese + d.English + d.Biology + d.Chemistry + d.Math + d.Politic;
            d.Pin = d.Sum / 6;
            e.Sum  = e.Chinese + e.English + e.Biology + e.Chemistry + e.Math + e.Politic;
            e.Pin = e.Sum  / 6;
            double[] mx = new double[] { a.Sum, b.Sum , c.Sum , d.Sum , e.Sum  };
            Array.Sort(mx);
            double[] mn = new double[] { a.Pin, b.Pin, c.Pin, d.Pin, e.Pin};
            Array.Sort(mn);
            Console.WriteLine("最高分{0},最低分{1},平均分最低是{2},平均分最高是{3}",mx[4],mx[0],mn[0],mn[4]);
            Console.ReadLine();
        }   
    }
}

想了半天,想用构造函数解决这个问题,最后还是写了这么多,谁有更好的方法?
搜索更多相关主题的帖子: namespace 设计程序 Chinese public 
2013-03-19 22:07
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
已经很不错了,不过你到是可以把求总分和平均分的方法放到Student类中,Main里面调用方法就可以了
2013-03-19 22:53
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
收藏
得分:0 
回复 2楼 yhlvht
调到Student方法里,和在Main差不多,可是得出的最高分,最低分不知道是谁的啊?你有什么好方法没?
2013-03-20 11:01
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
收藏
得分:0 
a.Sum  = a.Chinese + a.English + a.Biology + a.Chemistry + a.Math + a.Politic;
a.Pin = a.Sum / 6;
以上2行放入构造函数中
然后把5个student放入List<Student>中,再对总分和平均分进行排序。如果会LINQ就更方便了,里面有现成排序的扩展函数可用
2013-03-20 13:02
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:5 
namespace ConsoleApplication1
{
    public class Student
    {
        string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        double chinese;

        public double Chinese
        {
            get { return chinese; }
            set { chinese = value; }
        }
        double math;

        public double Math
        {
            get { return math; }
            set { math = value; }
        }
        double english;

        public double English
        {
            get { return english; }
            set { english = value; }
        }
        double politic;

        public double Politic
        {
            get { return politic; }
            set { politic = value; }
        }
        double chemistry;

        public double Chemistry
        {
            get { return chemistry; }
            set { chemistry = value; }
        }
        double biology;

        public double Biology
        {
            get { return biology; }
            set { biology = value; }
        }
        double sum;

        public double Sum
        {
            get { return sum; }
            set { sum = value; }
        }
        double pin;

        public double Pin
        {
            get { return pin; }
            set { pin = value; }
        }
        public Student(string name, double chinese, double math, double english, double politic, double chemistry, double biology)
        {
            this.name = name;
            this.chinese = chinese;
            this.math = math;
            this.english = english;
            this.politic = politic;
            this.chemistry = chemistry;
            this.biology = biology;
            sum = Chinese + English + Biology + Chemistry + Math + Politic;
            pin = Sum / 6;
        }
    }

    class program
    {
        static void Main(string[] args)
        {
            Student a = new Student("张一", 58, 52, 58, 96, 54, 56);
            Student b = new Student("李二", 50, 52, 58, 96, 54, 56);
            Student c = new Student("周三", 58, 52, 68, 96, 54, 56);
            Student d = new Student("马四", 58, 52, 58, 96, 54, 96);
            Student e = new Student("王五", 58, 32, 58, 96, 54, 56);
            double[] mx = new double[] { a.Sum, b.Sum, c.Sum, d.Sum, e.Sum };
            Array.Sort(mx);
            double[] mn = new double[] { a.Pin, b.Pin, c.Pin, d.Pin, e.Pin };
            Array.Sort(mn);
            Console.WriteLine("最高分{0},最低分{1},平均分最低是{2},平均分最高是{3}", mx[4], mx[0], mn[0], mn[4]);
            Console.ReadLine();
        }
    }
}
2013-03-20 14:32
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
收藏
得分:0 
回复 5楼 yhlvht
这个我知道,但是还是不知道谁是最高分啊
2013-03-20 16:10
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
回复 6楼 qq645485165
不是用Array.Sort(mx);排过序了么,跟你最原始的代码输出是一样的呀
2013-03-20 16:14
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
收藏
得分:5 
            sum = Chinese + English + Biology + Chemistry + Math + Politic;
            pin = Sum / 6;
上面2行放入构造函数,下面代码替换到main中        

var ls=new List<Student>{
            new Student("张一", 58, 52, 58, 96, 54, 56),
            new Student("李二", 50, 52, 58, 96, 54, 56),
            new Student("周三", 58, 52, 68, 96, 54, 56),
            new Student("马四", 58, 52, 58, 96, 54, 96),
            new Student("王五", 58, 32, 58, 96, 54, 56)
        };
            var ls1=ls.OrderBy(x=>x.Sum);
            var ls2=ls.OrderBy(x=>x.Pin);
            Console.WriteLine("{0} 最高分{1}.",ls1.Last().Name,ls1.Last().Sum);
            Console.WriteLine("{0} 最低分{1}.",ls1.First().Name,ls1.First().Sum);
            Console.WriteLine("{0} 平均分最低{1}.",ls2.First().Name,ls2.First().Pin);
            Console.WriteLine("{0} 平均分最低{1}.",ls2.Last().Name,ls2.Last().Pin);
            Console.ReadLine();
2013-03-20 16:40
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
收藏
得分:0 
谢谢啊!
2013-03-21 12:02
快速回复:c#排序不够精简,谁有更好的方法?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018503 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved