笔者最近在学校开始学习数据结构,这门课程作为计算机专业的最核心课程需要重点掌握,下面是数据结构实验课上的第一次作业内容,笔者在这里分享一下自己的想法和见解
设计程序实现统计一个班的学生成绩(学生的人数可以设置3000、5000、8000、10000等测试数据),要求自行设计至少两种排序算法,实现如下四个功能;记录每种算法的耗时,结合数据结构的知识对两种算法的时间复杂度进行比较说明。
(1) 输入每个学生的学号,并随机生成2门课程的成绩;
(2) 计算每个学生的平均成绩和总成绩;
(3) 按总成绩从高到低排名,并按名次输出每个学生情况,包括:
学号,各科成绩,平均成绩,总成绩,排名
(4) 由键盘输入课程号,输出该课程成绩在90分(含90分)以上且总分在前3名的学生情况,要求能多次查询。
作为数据结构的入门课程,这道题其实可以看作多个小的知识点构成的一个简单系统,衔接了C++学习和数据结构的入门内容。涉及下面这些知识点:
①C++中随机数的使用
②C++中组合数据类型的使用和相关处理
③排序算法
④简单的数据统计
每个知识点内容具体如下:
①随机数的使用即要调用系统库cstdlib中的srand()和rand()函数,即要使用两个随机数函数
②组合数据类型即包括结构体和类,此处由于基本上只有数据成员,因此使用结构体更加适合
③由C++的学习内容,已知的排序算法可以分为交换排序、选择排序和插入排序,此处选择其中的两到三种排序方法即可
④数据统计涉及的内容较为琐碎,此处不详细展开
#include
#include
#include
using namespace std;
struct Student//定义一个学生结构体,有六个数据成员
{
unsigned number;
unsigned DataStruction;
unsigned Programing;
unsigned OverallGoal;
double AverageGoal;
unsigned rank;
Student& operator=(const Student& s);
};
Student& Student::operator=(const Student& s)
{
this->number = s.number;
this->DataStruction = s.DataStruction;
this->Programing = s.Programing;
this->OverallGoal = s.OverallGoal;
this->AverageGoal = s.AverageGoal;
return *this;
}
void swap(Student* x, Student* y)//这个函数用来交换两个学生的数据
{
unsigned temp1 = x->OverallGoal;
x->OverallGoal = y->OverallGoal;
y->OverallGoal = temp1;
double temp2 = x->AverageGoal;
x->AverageGoal = y->AverageGoal;
y->AverageGoal = temp2;
unsigned temp3 = x->number;
x->number = y->number;
y->number = temp3;
unsigned temp4 = x->DataStruction;
x->DataStruction = y->DataStruction;
y->DataStruction = temp4;
unsigned temp5 = x->Programing;
x->Programing = y->Programing;
y->Programing = temp5;
}
void SwapSort(Student* p, const unsigned& n)
{
cout << "本次采用交换排序(起泡法):" << endl;
clock_t start, end;//用来进行程序计时
start = clock();
unsigned i = n - 1;
while (i > 0)
{
unsigned lastExchangeIndex = 0;
for (unsigned j = 0; j < i; j++)
{
if (p[j + 1].OverallGoal > p[j].OverallGoal)
{
swap(&p[j + 1], &p[j]);
lastExchangeIndex = j;
}
}
i = lastExchangeIndex;
}
end = clock();
cout << "本次排序用时:" << (double)end - (double)start << " ms." << endl;//为了避免时间过短,选择将排序循环了一百次
}
void InsertSort(Student* p, const unsigned& n)
{
cout << "本次采用插入排序:" << endl;
clock_t start, end;//用来进行程序计时
start = clock();
for (unsigned i = 1; i < n; i++)
{
unsigned j = i;
Student temp = p[i];
while (j > 0 && p[j - 1].OverallGoal < temp.OverallGoal)
{
p[j] = p[j-1];
j--;
}
p[j] = temp;
}
end = clock();
cout << "本次排序用时:" << (double)end - (double)start << " ms." << endl;//为了避免时间过短,选择将排序循环了一百次
}
void SelectSort(Student* p, const unsigned& n)
{
cout << "本次采用选择排序(简单排序法):" << endl;
clock_t start, end;//用来进行程序计时
start = clock();
for (unsigned i = 0; i < n - 1; i++)
{
unsigned leastIndex = i;
for (unsigned j = i + 1; j < n; j++)
{
if (p[j].OverallGoal > p[leastIndex].OverallGoal)
{
leastIndex = j;
}
}
swap(p[i], p[leastIndex]);
}
end = clock();
cout << "本次排序用时:" << (double)end - (double)start << " ms." << endl;
}
void CourseSort1(Student* p, const unsigned& n)
{
for (unsigned i = 0; i < n; i++)
{
for (unsigned j = 0; j < n - i - 1; j++)
{
if (p[j].DataStruction < p[j + 1].DataStruction)
{
swap(&p[j], &p[j + 1]);
}
}
}
}
void CourseSort2(Student* p, const unsigned& n)
{
for (unsigned i = 0; i < n; i++)
{
for (unsigned j = 0; j < n - i - 1; j++)
{
if (p[j].Programing < p[j + 1].Programing)
{
swap(&p[j], &p[j + 1]);
}
}
}
}
void HighestGrade(Student* sp, const unsigned& n)
{
string s;
loop:
unsigned grade = 1;
unsigned count = 0;
cout << "请输入您想查看前三名的课程的名称(课程号):(输入exit退出查分)" << endl;
cin >> s;
if (s == "exit")
{
return;
}
else if (s == "DataStruction")
{
CourseSort1(sp, n);
cout << "数据结构课程前三名情况如下:" << endl;
cout << "学号\t" << "数据结构成绩\t" << "编程成绩\t" << "平均成绩\t" << "总成绩\t " << "排名" << endl;
for (unsigned i = 0; i < n; i++)
{
if (sp[i].DataStruction != sp[i - 1].DataStruction && i != 0)
{
grade++;
if (grade > 3)break;
}
if (sp[i].DataStruction >= 90)
{
cout << sp[i].number << "\t" << sp[i].DataStruction << "\t " << sp[i].Programing << "\t "
<< sp[i].AverageGoal << "\t " << sp[i].OverallGoal << "\t " << sp[i].rank << endl;
count++;
}
}
if (count == 0)
{
cout << "很遗憾没有同学能上90分呢!" << endl;
}
else if (count == 1 || count == 2)
{
cout << "只有" << count << "位同学能上90分,他们真厉害!" << endl;
}
else
{
cout << "让我们恭喜上面这些同学!" << endl;
}
}
else if (s == "Programing")
{
CourseSort2(sp, n);
cout << "编程课程前三名情况如下:" << endl;
cout << "学号\t" << "数据结构成绩\t" << "编程成绩\t" << "平均成绩\t" << "总成绩\t " << "排名" << endl;
for (unsigned i = 0; i < n; i++)
{
if (sp[i].Programing != sp[i - 1].Programing && i != 0)
{
grade++;
if (grade > 3)break;
}
if (sp[i].Programing >= 90)
{
cout << sp[i].number << "\t" << sp[i].DataStruction << "\t " << sp[i].Programing << "\t "
<< sp[i].AverageGoal << "\t " << sp[i].OverallGoal << "\t " << sp[i].rank << endl;
count++;
}
}
if (count == 0)
{
cout << "很遗憾没有同学能上90分呢!" << endl;
}
else if (count == 1 || count == 2)
{
cout << "只有" << count << "位同学能上90分,他们真厉害!" << endl;
}
else
{
cout << "让我们恭喜上面这些同学!" << endl;
}
}
else
{
cout << "没有这一门课程哦!" << endl;
}
cout << endl;
goto loop;
}
int main(void)
{
const unsigned NumOfStu = 8000;//通过字符常量来定义学生人数,能够做到一改全改,且字符常量比C语言的宏定义更加安全
Student* sp = new Student[NumOfStu];//用动态内存分配方式创建一个学生的数组
srand(1);//设置随机数种子,方便程序复现调试,在程序的发布版本中可以去掉这一行
for (unsigned i = 0; i < NumOfStu; i++)//对数组中的每一个学生进行初始化,用随机数来给出两门课程的成绩(总分都是100)
{
sp[i].number = i + 1;
sp[i].DataStruction = rand() % 100 + 1;
sp[i].Programing = rand() % 100 + 1;
sp[i].OverallGoal = sp[i].DataStruction + sp[i].Programing;
sp[i].AverageGoal = double(sp[i].OverallGoal)/ 2;
sp[i].rank = 0;//尽管还不能确定排名,但是初始化可以防止出现垃圾数据
}
cout << "请输入排序方式:(Swap交换排序,Select选择排序,Insert插入排序)" << endl;
string cmd;
SortMethod:
cin >> cmd;
if (cmd == "Swap")
{
SwapSort(sp, NumOfStu);
}
else if (cmd == "Select")
{
SelectSort(sp, NumOfStu);
}
else if (cmd == "Insert")
{
InsertSort(sp, NumOfStu);
}
else
{
cout << "您没有输入正确的排序方式哦~,请重新输入一下吧!" << endl;
goto SortMethod;
}
for (unsigned i = 0; i < NumOfStu; i++)
{
if (i == 0 || (i!=0&&(sp[i].OverallGoal < sp[i - 1].OverallGoal)))
{
sp[i].rank = i + 1;
}
else//这种情况下连续两人的总分相同,则排名也相同
{
sp[i].rank = sp[i - 1].rank;
}
}
cout << "排序完成!" << endl;
//下面输出结果
cout << "学号\t" << "数据结构成绩\t" << "编程成绩\t" << "平均成绩\t" << "总成绩\t " << "排名" << endl;
for (int i = 0; i < NumOfStu; i++)
{
cout << sp[i].number << "\t" << sp[i].DataStruction << "\t " << sp[i].Programing << "\t "
<< sp[i].AverageGoal << "\t " << sp[i].OverallGoal << "\t " << sp[i].rank << endl;
}
cout << endl;
HighestGrade(sp, NumOfStu);
delete[] sp;
return 0;
}
如果代码中有不太懂或者有争议的地方,欢迎私信笔者,让我们共同进步~