基本任务:
创建一个Score类,完成以下功能:
①.连续输入多位学生的float成绩(成绩=科目A成绩+科目B成绩+科目C成绩);
②.学生数目可以由用户自定义(默认为2个),最多为100个;
③.显示每位学生的每科成绩和平均分;
④.显示每门科目的平均成绩;
⑤.对每门成绩进行排序并由高到低显示;
⑥.对整个文件进行打包;
程序代码:
.h 文件
#include<iostream>
#include<string>;
#include<stdio.h>;
using namespace std;
.cpp 文件
#include "homework4.h";
class Score {
public:
Score()
{
student_nums = 2;
cout << "构造函数1被调用" << endl;
}
Score(int subject_num)
{
student_nums = subject_num;
cout << "构造函数2被调用" << endl;
}
~Score()
{
cout << "析构函数被调用" << endl;
}
void InputStudentInformation() //学生姓名、成绩输入
{
int i = 0;
for (i = 0; i < student_nums; i++)
{
cout << "请输入学生姓名:" << endl;
cin >> name[i];
cout << "请输入科目A的成绩:" << endl;
cin >> score[i][0];
cout << "请输入科目B的成绩:" << endl;
cin >> score[i][1];
cout << "请输入科目C的成绩:" << endl;
cin >> score[i][2];
}
}
void ShowStudentScore() //学生各科成绩
{
int i;
float student_ave;
cout << "" << endl; //分割行,便于排版
for (i = 0; i < student_nums; i++)
{
cout << "姓名:" << name[i] << " 科目A成绩:" << score[i][0];
cout << " 科目B成绩:" << score[i][1];
cout << " 科目C成绩:" << score[i][2] << endl;
student_ave = (score[i][0] + score[i][1] + score[i][2]) / 3;
cout << "姓名:" << name[i] << " 平均成绩为:" << student_ave << endl << endl;
}
}
void ShowClassScore() //各科平均成绩
{
float a_ave = 0, b_ave = 0, c_ave = 0;
int i;
for (i = 0; i < student_nums; i++)
{
a_ave = a_ave + score[i][0];
b_ave = b_ave + score[i][1];
c_ave = c_ave + score[i][2];
}
a_ave = a_ave / student_nums;
b_ave = b_ave / student_nums;
c_ave = c_ave / student_nums;
cout << "科目A平均成绩为:" << a_ave << endl
<< "科目B平均成绩为:" << b_ave << endl
<< "科目C平均成绩为:" << c_ave << endl << endl; //分割行,便于排版
}
void ScoreSort() //单科成绩排序
{
int i, j, k, end = student_nums;
string mingzi_temp;
float score_temp;
string mingzi[50];
for (j = 0; j < 3; j++) //冒泡法排序
{
for (i = 0; i < student_nums; i++) //姓名初始化
{
mingzi[i] = name[i];
}
for (i = 0; i < student_nums - 1; i++)
{
for (k = i + 1; k < student_nums; k++)
{
if (score[i][j] < score[k][j])
{
score_temp = score[i][j];
score[i][j] = score[k][j];
score[k][j] = score_temp;
mingzi_temp = mingzi[i];
mingzi[i] = mingzi[k];
mingzi[k] = mingzi_temp;
}
}
}
end = student_nums;
switch (j)
{
case 0:cout << "课程名称 A:" << endl << endl; break; //分割行,便于排版
case 1:cout << "课程名称 B:" << endl << endl; break; //分割行,便于排版
case 2:cout << "课程名称 C:" << endl << endl; break; //分割行,便于排版
}
for (i = 0; i < student_nums; i++)
{
cout << "姓名:" << mingzi[i] << " 成绩:" << score[i][j] << endl;
if (i == student_nums - 1)
{
cout << "" << endl; //分割行,便于排版
}
}
}
}
private:
int student_nums;
float score[100][3] = {};
string name[100] = {};
};
int main()
{
int num;
cout << "是否需要改变输入学生数目(默认值为2,最多可以为100个)?需要改变请按1,无需改变请按0:";
cin >> num;
while ((num != 0) && (num != 1))
{
cout << "输入错误!!需要改变请按1,无需改变请按0" << endl;
cin >> num;
}
if (num == 1)
{
int student_numebr;
cout << "请输入要更改的学生数目:";
cin >> student_numebr;
Score x(student_numebr);
x.InputStudentInformation();
x.ShowStudentScore();
x.ShowClassScore();
x.ScoreSort();
}
if (num == 0)
{
Score x;
x.InputStudentInformation();
x.ShowStudentScore();
x.ShowClassScore();
x.ScoreSort();
}
}
Debug过程遇到的困难及解决方法:
一、对于学生姓名的存储,一开始想用char类型的二维数组进行存储,发现char类型的数组有点混乱,于是改用string类型的一维数组。
二、排序法的方法: 参考文献:六大排序算法
1.插入排序 :时间复杂度:最坏情况下为O(N*N),此时待排序列为逆序,或者说接近逆序,最好情况下为O(N),此时待排序列为升序,或者说接近升序。空间复杂度:O(1);
2.希尔排序:时间复杂度平均:O(N^1.3),空间复杂度:O(1);
3.选择排序:时间复杂度:最坏情况:O(N^2),最好情况:O(N^2),空间复杂度:O(1);
4.冒泡排序:时间复杂度:最坏情况:O(N^2),最好情况:O(N),空间复杂度:O(1);
5.堆排序:时间复杂度为O(N*log2N);
6.快速排序:时间复杂度为O(N*log2N);
三、一开始当拍完一科成绩后并没有给姓名初始化,导致在进行第二科成绩排序时,学生姓名与考试成绩不符,出现错误情况;
错误情况:
解决方法:
程序运行结果:
一、使用默认值(2)时:
二、用户自己设定学生数时(以4为例)
实验总结:
本次实验学习到的东西还是挺多的,有string数组的定义,用string数组来存放姓名,冒泡法排序,将学生成绩存到对象中,以及当出现两组内容都要变换时,要将其中的一个最开始的状态保存下来,方便之后初始化用,此次实验用时较长,学到的东西也是成正比上升,在实验中也认识到了自己的不足,在之后的时间中还得强化学习,不断扩充自己。