数据结构答辩(大二)
#include "stdio.h"
struct students{
int id;
char name[50];
char major[50];
int classes;
};
struct students student[5] = {
{ 1001, "小白", "计算机科学与技术", 1906014210 },
{ 1005, "乔碧萝", "智能科学与技术", 1906034210 },
{ 1004, "小明", "物联网工程", 1906044110 },
{ 1002, "小王", "网络工程", 1906024110 },
{ 1003, "小李", "外语", 1907014210 }
};
struct students tmp;
void Init(){
for (int i = 0; i < 5; i++){
printf("%d\t %s\t %s\t %d\n", student[i].id, student[i].name, student[i].major, student[i].classes);
}
}
void Bubblesort(){
for (int i = 1; i < 5; i++){
for (int j = 0; j < 5 - i; j++){
if (student[j].id > student[j + 1].id){
tmp = student[j];
student[j] = student[j + 1];
student[j + 1] = tmp;
}
}
}
for (int i = 0; i < 5; i++){
printf("%d\t %s\t %s\t %d\n", student[i].id, student[i].name, student[i].major, student[i].classes);
}
}
void QuickSort(students *arr, int low, int high)
{
if (low < high)
{
int i = low;
int j = high;
students k = arr[low];
while (i < j)
{
while(i < j && arr[j].id >= k.id) // 从右向左找第一个小于k的数
{
j--;
}
if(i < j)
{
arr[i++] = arr[j];
}
while(i < j && arr[i].id < k.id) // 从左向右找第一个大于等于k的数
{
i++;
}
if(i < j)
{
arr[j--] = arr[i];
}
}
arr[i] = k;
// 递归调用
QuickSort(arr, low, i - 1); // 排序k左边
QuickSort(arr, i + 1, high); // 排序k右边
}
}
void Search(){
printf("请输入需要查找学生的学号:");
int math;
scanf("%d", &math);
for (int i = 0; i < 5; i++){
if (math == student[i].id){
printf("%d %s %s %d\n", student[i].id, student[i].name, student[i].major, student[i].classes);
}
}
}
int main(){
printf(" ******************学生的借书证信息系统***********************\n");
printf("\n");
printf("*********************************************************************\n");
printf("*** 输入0:代表查看借书学生系统 **************\n");
printf("*** 输入1:借书学生系统排序后的结果 **************\n");
printf("*** 输入2:输入学号或姓名即可查看相关信息 **************\n");
printf("*********************************************************************\n");
printf("\n");
printf("*********************************************************************\n");
int inputmath;
for (int i = 0; i < 10; i++){
printf("请输入你需要进入哪个功能系统:");
scanf("%d", &inputmath);
if (inputmath == 0){
Init();
}
else if (inputmath == 1){
//Bubblesort(); //注释掉冒泡排序
QuickSort(student,1,4); // 调用快速排序
for (int i = 0; i < 5; i++){
printf("%d\t %s\t %s\t %d\n", student[i].id, student[i].name, student[i].major, student[i].classes);
}
}
else if (inputmath == 2){
Search();
}
}
}