/**********************Begin**********************/
#include <stdio.h>
#include <stdlib.h>
//(学号 姓名 英语 数学 物理 C语言成绩 总成绩 平均成绩)
struct list_of_class
{
char number[11];
char name[10];
int mark_of_english;
int mark_of_math;
int mark_of_physic;
int mark_of_language;
double sum;
double average;
struct list_of_class *next;
};
struct list_of_class *makelist(struct list_of_class *head, int n)
{
int i;
struct list_of_class *p, *later;
if (head == (struct list_of_class *)NULL) //看是否是新建链表
{
head = (struct list_of_class *)malloc(sizeof(struct list_of_class));
p = head;
}
else //如果不是则在之后插入链表
{
p = head->next;
while (p != (struct list_of_class *)NULL)
p = p->next;
p = (struct list_of_class *)malloc(sizeof(struct list_of_class));
head->next = p;
}
for (i = 0; i < n; i++)
{
scanf("%s", p->number);
scanf("%s", p->name);
scanf("%d", &(p->mark_of_english));
scanf("%d", &(p->mark_of_math));
scanf("%d", &(p->mark_of_physic));
scanf("%d", &(p->mark_of_language));
p->next = (struct list_of_class *)malloc(sizeof(struct list_of_class));
later = p;
p = p->next;
}
free(p);
later->next = (struct list_of_class *)NULL;
return head;
}
void printlist(struct list_of_class *head)
{
struct list_of_class *p;
int i;
p = head;
while (p != NULL)
{
printf("%s ", p->number);
printf("%s ", p->name);
printf("%d ", p->mark_of_english);
printf("%d ", p->mark_of_math);
printf("%d ", p->mark_of_physic);
printf("%d\n", p->mark_of_language);
p = p->next;
}
}
/*修改指定学生的指定数据项的内容:
1修改英语成绩
2修改高等数学成绩
3修改普通物理成绩
4修改C语言成绩*/
void change_list(struct list_of_class *head, char number[], int function, int mark)
{
int j;
struct list_of_class *p = head;
while (p != NULL)
{
for (j = 0; (number[j] == *((p->number) + j)) && (j < 10); j++)
;
if (j == 10)
{
switch (function)
{
case (1):
{
p->mark_of_english = mark;
break;
}
case (2):
{
p->mark_of_math = mark;
break;
}
case (3):
{
p->mark_of_physic = mark;
break;
}
case (4):
{
p->mark_of_language = mark;
break;
}
}
return;
}
else
{
p = p->next;
}
}
}
void each_average(struct list_of_class *head)
{
struct list_of_class *p = head;
while (p != NULL)
{
p->sum = (double)(p->mark_of_english + p->mark_of_language + p->mark_of_math + p->mark_of_physic);
p->average = (p->sum / 4);
p = p->next;
}
}
void print_sum_average(struct list_of_class *head)
{
struct list_of_class *p = head;
while (p != NULL)
{
printf("%s ", p->number);
printf("%s ", p->name);
printf("%d ", (int)(p->sum));
printf("%.2lf\n", p->average);
p = p->next;
}
}
void relist(struct list_of_class **headp)
{
struct list_of_class *p = *headp, *min, *tempp, *former = NULL, *minformer, *tempformer;
int minimum = 0xffff;
tempp = p; //暂存当下p
/*对头链表做特殊处理begin*/
while (p != NULL)
{
if (p->average < minimum)
{
minimum = p->average;
minformer = former;
min = p;
}
former = p;
p = p->next;
}
p = tempp;
if (p != min) //如果相同则不必要交换
{
if (minformer != NULL) //如果minformer为空指针,则说明最小的就是头链表,则无需交换
{
if (p->next == min) //交换相邻链表
{
*headp = min;
p->next = min->next;
min->next = p;
}
else //交换不相邻链表
{
*headp = min;
tempp = p->next;
p->next = min->next;
min->next = tempp;
minformer->next = p;
}
}
}
tempformer = min;
p = min->next;
former = tempformer;
/*对头链表做特殊处理end*/
while (p != NULL) //选择排序
{
minimum = 0xffff;
tempp = p;//暂存当下p
while (p != NULL)
{
if (p->average < minimum)
{
minimum = p->average;
minformer = former;
min = p;
}
former = p;
p = p->next;
}
p = tempp;
if (p != min)//如果相同则不必要交换
{
if (p->next == min)//交换相邻链表
{
tempformer->next = min;
p->next = min->next;
min->next = p;
}
else//交换不相邻链表
{
tempformer->next = min;
tempp = p->next;
p->next = min->next;
min->next = tempp;
minformer->next = p;
}
}
tempformer = min;
p = min->next;
former = tempformer;
}
}
int main()
{
int function, n;
struct list_of_class *head = NULL;
while (scanf("%d", &function) != EOF)
{
switch (function)
{
case (1):
{
scanf("%d", &n);
head = makelist(head, n);
//建立链表后计算平均值并排序
each_average(head);
relist(&head);
break;
}
case (2):
{
printlist(head);
break;
}
case (3):
{
char number[11];
int f, mark;
scanf("%s", number);
scanf("%d", &f);
scanf("%d", &mark);
change_list(head, number, f, mark);
//修改成绩后再次计算平均值,并重新排序
each_average(head);
relist(&head);
break;
}
case (4):
{
each_average(head);
struct list_of_class *p = head;
while (p != NULL)
{
printf("%s ", p->number);
printf("%s ", p->name);
printf("%.2lf\n", p->average);
p = p->next;
}
break;
}
case (5):
{
each_average(head);
print_sum_average(head);
return 0;
}
}
}
return 0;
}
/**********************End**********************/