一、毕业设计指导老师和学生信息系统
要求:
1、一共有三名老师,每名老师带五名学生;
2、老师的名字和学生的名字可用“ABCDE”代替;
3、学生的成绩用“60~99”的随机整数代替;
4、老师姓名、学生姓名和学生成绩均工整地显示出来。
#include<iostream>
#include<string>
using namespace std;
// 学生的结构体
struct STUDENT
{
// 学生姓名
string StudentName;
// 学生成绩
int StudentScore;
};
// 老师的结构定义
struct TEACHER
{
// 老师姓名
string TeacherName;
// 老师所带的学生
struct STUDENT StudentArray[5];
};
// 为老师和同学赋予相关信息
void AllocateSpace(struct TEACHER teacher_array[],int array_length)
{
char nameseed[5] = {'A','B','C','D','E'};
// 为了规范命名,这边先起了一些“模板名字”以便于使用
string name_t_const = "Teacher_";
string name_s_const = "Student_";
for (int i = 0; i < array_length; i++)
{
teacher_array[i].TeacherName = name_t_const + nameseed[i];
for (int j = 0; j < 5; j++)
{
int randvalue = rand() % 41 + 60;
teacher_array[i].StudentArray[j].StudentName = teacher_array[i].TeacherName +"_" + name_s_const + nameseed[j];
teacher_array[i].StudentArray[j].StudentScore = randvalue;
}
}
}
// 打印老师和同学的相关信息
void PrintInform(struct TEACHER teacher_array[], int array_length)
{
for (int i = 0; i < 3; i++)
{
cout << "老师姓名:"
<< teacher_array[i].TeacherName
<< endl;
cout << endl;
for (int j = 0; j < 5; j++)
{
cout << "学生姓名:" << endl;
cout << teacher_array[i].StudentArray[j].StudentName
<< "\t"
<< teacher_array[i].StudentArray[j].StudentScore
<< endl;
cout << endl;
}
}
}
int main()
{
// 生成随机数种子,以便重复实现随机数
srand((unsigned int)time(NULL));
// 创建三位老师的具体变量
struct TEACHER teacher_array[3];
// 通过函数给三位老师和他们的学生进行赋值
AllocateSpace(teacher_array, 3);
// 打印老师和学生的信息
PrintInform(teacher_array, 3);
system("pause");
return 0;
}
显示结果如下:
老师姓名:Teacher_A
学生姓名:
Teacher_A_Student_A 62
学生姓名:
Teacher_A_Student_B 66
学生姓名:
Teacher_A_Student_C 77
学生姓名:
Teacher_A_Student_D 88
学生姓名:
Teacher_A_Student_E 91
老师姓名:Teacher_B
学生姓名:
Teacher_B_Student_A 64
学生姓名:
Teacher_B_Student_B 77
学生姓名:
Teacher_B_Student_C 97
学生姓名:
Teacher_B_Student_D 73
学生姓名:
Teacher_B_Student_E 67
老师姓名:Teacher_C
学生姓名:
Teacher_C_Student_A 84
学生姓名:
Teacher_C_Student_B 85
学生姓名:
Teacher_C_Student_C 65
学生姓名:
Teacher_C_Student_D 62
学生姓名:
Teacher_C_Student_E 85
请按任意键继续. . .
二、英雄年龄排名
有如下五个英雄:
刘备——男——23;关羽——男——22
赵云——男——21;张飞——男——20
貂蝉——女——19
要求:
1.以结构体的形式存储这五个英雄的信息;
2.对年龄用冒泡算法进行升序排列;
3.显示出排序前和排序后的信息。
#include<iostream>
#include<string>
using namespace std;
// 设计英雄结构体
struct HERO {
string name;
int age;
string sex;
};
// 冒泡排序
void BubbleSort(struct HERO array[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len-i-1; j++)
{
if (array[j].age > array[j + 1].age)
{
struct HERO temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
// 打印数据
void PrintHero(struct HERO array[], int len)
{
for (int i =0; i < len; i++)
{
cout << array[i].age << "\t" << array[i].name << "\t" << array[i].sex << endl;
}
}
int main()
{
// 设计英雄结构体的数组
struct HERO HeroArray[5] = {
{"刘备",23,"male"},
{"关羽",22,"male"},
{"张飞",20,"male"},
{"赵云",21,"male"},
{"貂蝉",19,"female"}
};
// 输出结构体数组看看是否正确
int length = sizeof(HeroArray) / sizeof(HeroArray[0]);
for (int i = 0; i < length; i++)
{
cout << HeroArray[i].age << "\t" << HeroArray[i].name << "\t" << HeroArray[i].sex << endl;
}
cout << endl;
// 对年龄进行排序
BubbleSort(HeroArray,length);
// 对英雄数据打印输出
PrintHero(HeroArray, length);
system("pause");
return 0;
}
显示的结果是:
23 刘备 male
22 关羽 male
20 张飞 male
21 赵云 male
19 貂蝉 female
19 貂蝉 female
20 张飞 male
21 赵云 male
22 关羽 male
23 刘备 male
请按任意键继续. . .