#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct st
{
int ID;
char name[
10];
int score[
4];
struct st* next;
};
char *name[
5] =
{
"刘德华",
"张学友",
"郭富城",
"黎明",
"石洛"
};
char *print_model[] =
{
"\n姓名:\t学号:\t语文:\t数学:\t英语:\t理综:\t\n",
"\n%s\t%d\t%d\t%d\t%d\t%d\t\n",
"\n姓名:\t学号:\n",
"\n%s\t%d\n",
"\n姓名:\t语文:\n",
"\n%s\t%d\n",
"\n姓名:\t数学:\n",
"\n%s\t%d\n",
"\n姓名:\t英语:\t\n",
"\n%s\t%d\n",
"\n姓名:\t理综:\t\n",
"\n%s\t%d\n",
};
struct st * list_build(
struct st **arr,
struct st* head)
{
int i,j;
for (i =
0; i <
5; i++)
{
arr[i] = (
struct st*)
malloc(
sizeof(
struct st));
strcpy(arr[i]->name, name[i]);
arr[i]->ID = rand() %
100 +
10;
for (j =
0; j<
4; j++)
{
arr[i]->score[j] = rand() %
100 +
50;
}
if (i>
0)
{
arr[i -
1]->next = arr[i];
}
}
head = arr[
0];
arr[i -
1]->next = NULL;
return head;
}
void list_print(
struct st* head,
struct st* p,
int n)
{
printf(print_model[
2*n]);
while (head != NULL)
{
switch(n)
{
case 0:
printf(print_model[
2 * n +
1], head->name, head->ID, head->score[
0], head->score[
1], head->score[
2], head->score[
3]);
break;
case 1:
printf(print_model[
2 * n +
1], head->name, head->ID);
break;
case 2:
printf(print_model[
2 * n +
1], head->name, head->score[
0]);
break;
case 3:
printf(print_model[
2 * n +
1], head->name, head->score[
1]);
break;
case 4:
printf(print_model[
2 * n +
1], head->name, head->score[
2]);
break;
case 5:
printf(print_model[
2*n +
1], head->name, head->score[
3]);
break;
}
head = head->next;
}
head = p;
}
void list_sort(
struct st* head,
struct st* p,
int n)
{
struct st *start = NULL, *temp = NULL, *temp1, *temp2;
int choice_1, choice_2;
temp = (
struct st *)
malloc(
sizeof(
struct st));
while (head != NULL)
{
start = head;
while (start->next != NULL)
{
switch (n)
{
case 1:
choice_1 = head->ID;
choice_2 = start->next->ID;
break;
case 2:
choice_1 = head->score[
0];
choice_2 = start->next->score[
0];
break;
case 3:
choice_1 = head->score[
1];
choice_2 = start->next->score[
1];
break;
case 4:
choice_1 = head->score[
2];
choice_2 = start->next->score[
2];
break;
case 5:
choice_1 = head->score[
3];
choice_2 = start->next->score[
3];
break;
}
if (choice_1 < choice_2)
{
temp1 = head->next;
temp2 = start->next->next;
*temp = *(start->next);
*(start->next) = *head;
*head = *temp;
head->next = temp1;
start->next->next = temp2;
}
start = start->next;
}
head = head->next;
}
head = p;
list_print(head, p, n);
}
void main()
{
int n;
struct st *arr[
5], *head = NULL;
head = list_build(arr, head);
list_print(head, arr[
0],
0);
printf(
"\n请选择排序方式:1,按ID\t2,按语文成绩\t3,按数学成绩\t4按英语成绩\t5,按理综成绩\t\n");
scanf(
"%d", &n);
getchar();
list_sort(head, arr[
0],n);
getchar();
}