C#实验报告4(含实验小结)

一、实验目的:

1.理解面向对象的概念,掌握C#的定义类和创建对象的方法。

2.区分类的不同数据成员,包括常量、字段和属性的定义方法,并学会控制其可访问性。

3.掌握类的方法成员的声明与调用,理解各种参数在方法中的意义及使用。

4.理解构造函数和终结器的作用机制。 

二、实验设备:

  1. PC一台;
  2. 软件:Windows OS,Visual Studio 2010等。

三、实验内容:(编写程序调试输出结果)

1.设计一个简单的Windows应用程序,在文本框中输入两个点的坐标值,单击“计算”时显示两点之间的距离。

要求定义一个point类。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace sy4_0301

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        class Point

        {

            private int x;

            private int y;

            public Point(int x, int y)

            {

                this.x = x;

                this.y = y;

            }

            public int X

            {

                get

                {

                    return x;

                }

            }

            public int Y

            {

                get

                {

                    return y;

                }

            }

            public double distance(Point p)

            {

                double a = Math.Sqrt((this.x - p.X) * (this.x - p.X) + (this.y - p.Y) * (this.y - p.Y));

                return a;

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {

            int x1 = Convert.ToInt32(textBox1.Text);

            int y1 = Convert.ToInt32(textBox2.Text);

            int x2 = Convert.ToInt32(textBox3.Text);

            int y2 = Convert.ToInt32(textBox4.Text);

            Point p1 = new Point(x1, y1);

            Point p2 = new Point(x2, y2);

            textBox5.Text = p1.distance(p2).ToString();

        }

    }

}

图1.实验结果1

2.自定义一个时间类。该类包含小时、分、秒字段与属性,具有将秒增1操作的方法。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace sy4_0301

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        class Time

        {

            private int hour;

            private int minute;

            private int second;

            public int Hour

            {

                get

                {

                    return hour;

                }

            }

            public int Minute

            {

                get

                {

                    return minute;

                }

            }

            public int Second

            {

                get

                {

                    return second;

                }

            }

            public Time(int hour, int minute, int second)

            {

                this.hour = hour;

                this.minute = minute;

                this.second = second;

            }

            public Time()

            {

                this.hour = System.DateTime.Now.Hour;

                this.minute = System.DateTime.Now.Minute;

                this.second = System.DateTime.Now.Second;

            }

            public void AddSec()

            {

                second++;

                if (second >= 60)

                {

                    second = second % 60;

                    minute++;

                }

                if (minute >= 60)

                {

                    minute = minute % 60;

                    hour++;

                }

            }

        }

        private void Form2_Load(object sender, EventArgs e)

        {

            Time time1 = new Time();

            textBox1.Text = Convert.ToString(time1.Hour);

            textBox2.Text = Convert.ToString(time1.Minute);

            textBox3.Text = Convert.ToString(time1.Second);

        }

        private void button1_Click(object sender, EventArgs e)

        {

            int hour = Convert.ToInt32(textBox1.Text);

            int minute = Convert.ToInt32(textBox2.Text);

            int second = Convert.ToInt32(textBox3.Text);

            Time time2 = new Time(hour, minute, second);

            time2.AddSec();

            textBox1.Text = Convert.ToString(time2.Hour);

            textBox2.Text = Convert.ToString(time2.Minute);

            textBox3.Text = Convert.ToString(time2.Second);

        }

    }

}

图2.实验结果2

3.设计一个简单的Windows程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号、姓名、语文、数学和英语3门课程的期末考试成绩,要求:

(1)能根据姓名查询指定学生的总成绩。

(2)能统计全班学生的平均成绩。

(3)能统计单科成绩最高分。

(4)能统计全班前三名的名单。

(5)能统计指定课程不及格的学生名单。

(6)能统计指定课程在不同分数段的学生人数百分比。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace sy4_0301

{

    public partial class Form3 : Form

    {

        public Form3()

        {

            InitializeComponent();

        }

        private void Form3_Load(object sender, EventArgs e)

        {

            label7.Text = "";

        }

        Student[]st1=new Student[100];

        Grade gr=new Grade();

        int i=0;

        public class Student

        {

            public string sno;

            public string name;

            public double chinese;

            public double math;

            public double english;

            public Student(string sno, string name, double chinese,

                double math, double english)

            {

                this.sno = sno;

                this.name = name;

                this.chinese = chinese;

                this.math = math;

                this.english = english;

            }

            public double Sum

            {

                get

                {

                    return chinese + math + english;

                }

            }

        }

        public class Grade

        {

            Student[] stu=new Student[100];

            double[] sum2=new double[100];

            int i=0;

            public Grade(){}

            public void Add(Student s)

            {

                stu[i]=s;

                sum2[i]=stu[i].Sum;

                i++;

            }

            int x=0;

            int k=0;

            bool flag=false;

            public double getSum(string s)

            {

                for(x=0;x<i;x++)

                {

                    if(stu[x].name==s)

                    {

                        k=x;

                        flag=true;

                    }

                }

                if(flag==true)

                {

                    return sum2[k];

                }

                else

                {

                    return -1;

                }

            }

            double avg=0;

            public double getAverage()

            {

                for(int aa=0;aa<i;aa++)

                {

                    avg+=sum2[aa];

                }

                return avg/i;

            }

            double maxChinese=0;

            public double getChineseMax()

            {

                for(int z=0;z<i;z++)

                {

                    if(stu[z].chinese>maxChinese)

                    {

                        maxChinese=stu[z].chinese;

                    }

                }

                return maxChinese;

            }

            double maxMath=0;

            public double getMathMax()

            {

                for(int z=0;z<i;z++)

                {

                    if(stu[z].math>maxMath)

                    {

                        maxMath=stu[z].math;

                    }

                }

                return maxMath;

            }

            double maxEnglish=0;

            public double getEnglishMax()

            {

                for(int z=0;z<i;z++)

                {

                    if(stu[z].english>maxEnglish)

                    {

                        maxEnglish=stu[z].english;

                    }

                }

                return maxEnglish;

            }

            public string getNames()

            {

                Student[] t=new Student[1];

                t[0]=new Student("","",0,0,0);

                double t2=0;

                for(int xx=0;xx<i-1;xx++)

                {

                    for(int yy=xx+1;yy<i;yy++)

                    {

                        if(sum2[yy]>sum2[xx])

                        {

                            t2=sum2[yy];

                            sum2[yy]=sum2[xx];

                            sum2[xx]=t2;

                            t[0]=stu[yy];

                            stu[yy]=stu[xx];

                            stu[xx]=t[0];

                        }

                    }

                }

                return""+stu[0].name+""+stu[1].name+""+stu[2].name;

            }

            string md="";

            public string getStudentMenu(string s)

            {

                if(s=="语文")

                {

                    for(int x=0;x<i;x++)

                    {

                        if(stu[x].chinese<60)

                        {

                            md+=""+stu[x].name;

                        }

                    }

                    return""+md;

                }

                else if(s=="数学")

                {

                    for(int x=0;x<i;x++)

                    {

                        if(stu[x].math<60)

                        {

                            md+=""+stu[x].name;

                        }

                    }

                    return""+md;

                }

                else if(s=="英语")

                {

                    for(int x=0;x<i;x++)

                    {

                        if(stu[x].english<60)

                        {

                            md+=""+stu[x].name;

                        }

                    }

                    return""+md;

                }

                else

                {

                    return"不存在(你输入的课程信息不正确)";

                }

            }

            public string getStudentBFB(string s)

            {

                if(s=="语文")

                {

                    double yw1=0,yw2=0,yw3=0,yw4=0,yw5=0;

                    for(int z=0;z<i;z++)

                    {

                        if(stu[z].chinese<=100&&stu[z].chinese>=90)

                        {

                            yw1++;

                        }

                        else if(stu[z].chinese<90&&stu[z].chinese>=80)

                        {

                            yw2++;

                        }

                        else if(stu[z].chinese<80&&stu[z].chinese>=70)

                        {

                            yw3++;

                        }

                        else if(stu[z].chinese<70&&stu[z].chinese>=60)

                        {

                            yw4++;

                        }

                        else

                        {

                            yw5++;

                        }

                    }

                    return"\n100-90:"+(yw1/i)*100.0+"%\n90-80:"+(yw2/i)*100.0+"%\n80-70:"+(yw3/i)*100.0+"%\n70-60:"+(yw4/i)*100.0+"%\n60以下:"+(yw5/i)*100.0+"%";

                }

                else if(s=="数学")

                {

                    double yw1=0,yw2=0,yw3=0,yw4=0,yw5=0;

                    for(int z=0;z<i;z++)

                    {

                        if(stu[z].math<=100&&stu[z].math>=90)

                        {

                            yw1++;

                        }

                        else if(stu[z].math<90&&stu[z].math>=80)

                        {

                            yw2++;

                        }

                        else if(stu[z].math<80&&stu[z].math>=70)

                        {

                            yw3++;

                        }

                        else if(stu[z].math<70&&stu[z].math>=60)

                        {

                            yw4++;

                        }

                        else

                        {

                            yw5++;

                        }

                    }

                    return"\n100-90:"+(yw1/i)*100.0+"%\n90-80:"+(yw2/i)*100.0+"%\n80-70:"+(yw3/i)*100.0+"%\n70-60:"+(yw4/i)*100.0+"%\n60以下:"+(yw5/i)*100.0+"%";

                }

                else if(s=="英语")

                {

                    double yw1=0,yw2=0,yw3=0,yw4=0,yw5=0;

                    for(int z=0;z<i;z++)

                    {

                        if(stu[z].english<=100&&stu[z].english>=90)

                        {

                            yw1++;

                        }

                        else if(stu[z].english<90&&stu[z].english>=80)

                        {

                            yw2++;

                        }

                        else if(stu[z].english<80&&stu[z].english>=70)

                        {

                            yw3++;

                        }

                        else if(stu[z].english<70&&stu[z].english>=60)

                        {

                            yw4++;

                        }

                        else

                        {

                            yw5++;

                        }

                    }

                    return"\n100-90:"+(yw1/i)*100.0+"%\n90-80:"+(yw2/i)*100.0+"%\n80-70:"+(yw3/i)*100.0+"%\n70-60:"+(yw4/i)*100.0+"%\n60以下:"+(yw5/i)*100.0+"%";

                }

                else

                {

                    return"不存在(你输入的课程不正确)";

                }

            }

            

        }

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                st1[i]=new Student(textBox1.Text,textBox2.Text,Convert.ToInt32(textBox3.Text),Convert.ToInt32(textBox4.Text),Convert.ToInt32(textBox5.Text));

                gr.Add(st1[i]);

                i++;

                label7.Text="成功添加"+i+"个学生的信息";

                textBox1.Text="";

                textBox2.Text="";

                textBox3.Text="";

                textBox4.Text="";

                textBox5.Text="";

                textBox6.Text="";

            }

            catch

            {

                MessageBox.Show("输入完善的学生信息进行添加!");

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            label7.Text="";

            if(textBox6.Text.Trim()!="")

            {

                label7.Text+="\n\n全班同学的平均成绩为:"+Convert.ToString(gr.getAverage());

                label7.Text+="\n\n语文成绩最高分为:"+Convert.ToString(gr.getChineseMax());

                label7.Text+="\n\n数学成绩最高分为:"+Convert.ToString(gr.getMathMax());

                label7.Text+="\n\n英语成绩最高分为:"+Convert.ToString(gr.getEnglishMax());

                label7.Text+="\n\n全班前三为:"+gr.getNames();

                label7.Text+="\n\n指定科目:"+textBox6.Text+"的不及格的名单"+gr.getStudentMenu(textBox6.Text);

                label7.Text+="\n\n指定科目:"+textBox6.Text+"不同分数段的百分比如下:"+gr.getStudentBFB(textBox6.Text);

                textBox6.Text="";

            }

            else

            {

                MessageBox.Show("请输入你要查询的课程名称:");

            }

        }

        private void button3_Click(object sender, EventArgs e)

        {

            label7.Text="";

            if(textBox2.Text.Trim()!="")

            {

                double result=gr.getSum(textBox2.Text);

                if(result==-1)

                {

                    MessageBox.Show("该学生不存在!");

                }

                else

                {

                    label7.Text="学生"+textBox2.Text+"的总成绩为:"+Convert.ToString(result);

                    textBox2.Text="";

                }

            }

            else

            {

                MessageBox.Show("请输入你要查询的学生姓名:");

            }

        }

    }

}

四、实验小结:

通过此次实验,我更加理解了面向对象的概念,掌握了C#的定义类和创建对象的方法,能区分类的不同数据成员,包括常量、字段和属性的定义方法,并学会控制其可访问性,也掌握了类的方法成员的声明与调用,理解了各种参数在方法中的意义及使用和构造函数和终结器的作用机制。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值