C#語言程式設計第一次試驗
-
- 前言:
- 一、實驗内容描述
-
-
-
- 設計提示:
-
-
- 二、實驗環境
- 三、實驗目的
- 四、算法設計
- 五、實驗代碼
- 六、實驗結果截圖
前言:
為了幫助同學們完成痛苦的實驗課程設計,本作者将其作出的實驗結果及代碼貼至CSDN中,供同學們學習參考。如有不足或描述不完善之處,敬請各位指出,歡迎各位的斧正!
一、實驗内容描述
設計一個Windows應用程式,在該程式中定義一個學生類和班級類,以處理每個學生的學号、姓名、國文、數學、和英語3門課程的期末考試成績,要求:
1.能根據姓名查詢指定學生的總成績
2.能統計全班學生的平均成績
3.能統計單科成績最高分
4.能統計全班前3名的名單
5.能統計指定課程不及格的學生名單
6.能統計指定課程在不同分數段的學生人數百分比
設計提示:
1.定義一個Student學生類,包含字段(學号、姓名、國文成績、數學成績、英語成績)和屬性(總成績)等。
2.定義一個Grade班級類,包含一個Student類型的數組(用來儲存全班學生的資訊)以及若幹個實作上述要求的方法等。
3.設計使用者操作界面,首先讓使用者能輸入一個學生的資訊,當單擊“添加”按鈕時把這些資訊添加到班級對象的學生數組中。單擊“完成”按鈕調用班級類的方法來顯示各種統計結果。當使用者輸入了學生姓名并且單擊“查詢”按鈕時顯示該學生的總成績。
二、實驗環境
Visual Studio 2019
三、實驗目的
1.了解面向對象的抽象過程
2.掌握c#對類的定義
3.掌握對編譯工具的使用
4.掌握對類的使用
注:這個各位改着寫寫就行,在這裡的四條主要用于示例,請按照不同的設計要求進行修改
四、算法設計
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TRq5EeNRUZx50MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2EjNyEzMykTMzITMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
老師對我們的要求是展示類圖,具體的操作方法是:
最上方菜單中視圖
類視圖
然後在類視圖菜單中逐個右鍵檢視類圖
然後點選類名右邊的兩個箭頭的符号,結果如下
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TRq5EeNRUZx50MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2EjNyEzMykTMzITMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
五、實驗代碼
// 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
誠摯的希望能幫到各位同學,祝各位學業有成!