前言:
为了帮助同学们完成痛苦的实验课程设计,本作者将其作出的实验结果及代码贴至CSDN中,供同学们学习参考。如有不足或描述不完善之处,敬请各位指出,欢迎各位的斧正!
一、实验内容描述
设计一个Windows应用程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号、姓名、语文、数学、和英语3门课程的期末考试成绩,要求:
1.能根据姓名查询指定学生的总成绩
2.能统计全班学生的平均成绩
3.能统计单科成绩最高分
4.能统计全班前3名的名单
5.能统计指定课程不及格的学生名单
6.能统计指定课程在不同分数段的学生人数百分比
设计提示:
1.定义一个Student学生类,包含字段(学号、姓名、语文成绩、数学成绩、英语成绩)和属性(总成绩)等。
2.定义一个Grade班级类,包含一个Student类型的数组(用来保存全班学生的信息)以及若干个实现上述要求的方法等。
3.设计用户操作界面,首先让用户能输入一个学生的信息,当单击“添加”按钮时把这些信息添加到班级对象的学生数组中。单击“完成”按钮调用班级类的方法来显示各种统计结果。当用户输入了学生姓名并且单击“查询”按钮时显示该学生的总成绩。
二、实验环境
三、实验目的
1.理解面向对象的抽象过程
2.掌握c#对类的定义
3.掌握对编译工具的使用
4.掌握对类的使用
注:这个各位改着写写就行,在这里的四条主要用于示例,请按照不同的设计要求进行修改
四、算法设计
老师对我们的要求是展示类图,具体的操作方法是:
最上方菜单中视图
类视图
然后在类视图菜单中逐个右键查看类图
然后点击类名右边的两个箭头的符号,结果如下
五、实验代码
// An highlighted block
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 第一次实验
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{}
Student[] st = new Student[50];
Grade grade = new Grade();
int i = 0;
private void button1_Click_1(object sender, EventArgs e)//输入学生信息
{
try
{
st[i] = new Student(Convert.ToInt32(textBox1.Text), textBox2.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox5.Text));
grade.Add(st[i]);i++;
textBox6.Text = Convert.ToString(Convert.ToInt32(textBox3.Text)+ Convert.ToInt32(textBox4.Text)+Convert.ToInt32(textBox5.Text));
MessageBox.Show("学生信息添加成功!");
label8.Text = "已成功添加" + i + "个学生的信息";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}
catch
{
MessageBox.Show("学生信息输入缺损!请检查后重新输入!");
}
}
private void button2_Click_1(object sender, EventArgs e)//消息输出区输出
{
label7.Text = "";
if (comboBox1.Text.Trim() != "")
{
label7.Text += "平均成绩:" + Convert.ToString(grade.getAverage());
label7.Text += " 语文最高分:" + Convert.ToString(grade.getChineseMax());
label7.Text += "\n数学最高分:" + Convert.ToString(grade.getMathMax());
label7.Text += " 英语最高分:" + Convert.ToString(grade.getEnglishMax());
label7.Text += "\n\n全班前三名分别是:" + grade.getNames();
label7.Text += "\n\n" + comboBox1.Text + "不及格:" + grade.getStudentMenu(comboBox1.Text);
label7.Text += "\n\n" + comboBox1.Text + "不同分数段的百分比:" + grade.getStudentBFB(comboBox1.Text);
}
else
{
MessageBox.Show("请选择您要查询的课程名称:");
}
}
private void button3_Click_1(object sender, EventArgs e)//学生信息查询
{
if (textBox2.Text.Trim() != "")
{
int result = grade.GetSum(textBox2.Text);
if (result == -1)
{
MessageBox.Show("该学生不存在!");
}
else
{
textBox1.Text= Convert.ToString(grade.Getno(textBox2.Text));
textBox3.Text = Convert.ToString(grade.Getname(textBox2.Text));
textBox3.Text = Convert.ToString(grade.Getch(textBox2.Text));
textBox4.Text = Convert.ToString(grade.Getma(textBox2.Text));
textBox5.Text = Convert.ToString(grade.Geten(textBox2.Text));
textBox6.Text =Convert.ToString(result);
}
}
else
{
MessageBox.Show("请输入您要查询的学生姓名");
}
}
}
public class Student
{
public int sno;
public string name;
public int chinese;
public int math;
public int english;
public Student(int sno, string name, int chinese, int math, int english)
{
this.sno = sno;
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public int Sum
{
get
{
return chinese + math + english;
}
}
}
public class Grade
{
Student[] stu = new Student[50]; //存放班级中的每个同学的信息
int[] sum = new int[50]; //存放每个同学的总成绩
int i = 0; //学生人数
public Grade() { }
public void Add(Student s)//增加学生人数
{
stu[i] = s;
sum[i] = stu[i].Sum;
i++; //学生人数加1
}
//查询指定学生的总成绩(1)
int x = 0;
int k = 0;
public int GetSum(string s)
{
for (x = 0; x < i; x++)
{
if (stu[x].name == s)
break;
}
if (stu[x].name == s)
{
return stu[x].chinese+stu[x].english+stu[x].math;
}
else
return -1;
}
public int Getno(string s)
{
for (x = 0; x < i; x++)
{
if (stu[x].name == s)
break;
}
return stu[x].sno;
}
public string Getname(string s)
{
for (x = 0; x < i; x++)
{
if (stu[x].name == s)
break;
}
return stu[x].name;
}
public int Getch(string s)
{
for (x=0;x<i;x++)
{
if (stu[x].name == s)
break;
}
return stu[x].chinese;
}
public int Getma(string s)
{
for (x = 0; x < i; x++)
{
if (stu[x].name == s)
break;
}
return stu[x].math;
}
public int Geten(string s)
{
for (x = 0; x < i; x++)
{
if (stu[x].name == s)
break;
}
return stu[x].english;
}
double avg = 0;
public double getAverage()//班级平均分
{
for (int aa = 0; aa < i; aa++)
{
avg += sum[aa];
}
return avg / i;
}
int maxChinese = 0;
public int getChineseMax()
{
for (int z = 0; z < i; z++)
{
if (stu[z].chinese > maxChinese)
{
maxChinese = stu[z].chinese;
}
}
return maxChinese;
}//语文最高分
int maxMath = 0;
public int getMathMax()
{
for (int z = 0; z < i; z++)
{
if (stu[z].math > maxMath)
{
maxMath = stu[z].math;
}
}
return maxMath;
}//数学最高分
int maxEnglish = 0;
public int 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, 0);
int t2 = 0;
if (i == 0) return "第一名:" + stu[0].name;
else if (i == 1)
{
for (int p = 0; p < i - 1; p++)
{
for (int q = p + 1; q < i; q++)
{
if (sum[q] > sum[p])
{
t2 = sum[q];
sum[q] = sum[p];
sum[p] = t2;
t[0] = stu[q];
stu[q] = stu[p];
stu[p] = t[0];
}
}
}
return "第一名:" + stu[0].name + " 第二名:" + stu[1].name; }
else
{
for (int p = 0; p < i - 1; p++)
{
for (int q = p + 1; q < i; q++)
{
if (sum[q] > sum[p])
{
t2 = sum[q];
sum[q] = sum[p];
sum[p] = t2;
t[0] = stu[q];
stu[q] = stu[p];
stu[p] = 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; double yw2 = 0; double yw3 = 0; double yw4 = 0; double 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 "\n优秀(90<=x<=100):" + (yw1 / i) * 100.0 + "% 良好(80<=x<90):" + (yw2 / i) * 100.0 + "%\n中等(70<=x<80):" +
(yw3 / i) * 100.0 + "% 及格(60<=x<70):" + (yw4 / i) * 100.0 + "%\n不及格(x<60):" + (yw5 / i) * 100.0 + "%";
}
else if (s == "数学")
{
double yw1 = 0; double yw2 = 0; double yw3 = 0; double yw4 = 0; double 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 "\n优秀(90<=x<=100):" + (yw1 / i) * 100.0 + "% 良好(80<=x<90):" + (yw2 / i) * 100.0 + "%\n中等(70<=x<80):" +
(yw3 / i) * 100.0 + "% 及格(60<=x<70):" + (yw4 / i) * 100.0 + "%\n不及格(x<60):" + (yw5 / i) * 100.0 + "%";
}
else if (s == "英语")
{
double yw1 = 0; double yw2 = 0; double yw3 = 0; double yw4 = 0; double 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 "\n优秀(90<=x<=100):" + (yw1 / i) * 100.0 + "% 良好(80<=x<90):" + (yw2 / i) * 100.0 + "%\n中等(70<=x<80):" +
(yw3 / i) * 100.0 + "% 及格(60<=x<70):" + (yw4 / i) * 100.0 + "%\n不及格(x<60):" + (yw5 / i) * 100.0 + "%";
}
else
{
return "error!";
}
}
}
}
设计操作界面上图所示
自学号往下分别是textBox1到6,comboBox1中的编辑项设定为
语文
数学
英语
按钮控件从左到右分别是button1到3
注意:消息输出区有一个label7,我将其Text全部删除了,切勿忽略
所有选中的控件如图所示
六、实验结果截图
实验代码下载链接:
链接:https://pan.baidu.com/s/1VDTxUWX3x4g9UrVIqvd8CA
提取码:zd7r
作者:Knight Schumacher
首次编辑日期:2020.11.23
诚挚的希望能帮到各位同学,祝各位学业有成!