#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <string.h>
typedef struct data {
int number;
char name[20];
char id[20];
double score[3];
}dataType;
typedef struct list {
dataType pauline;
struct list *next;
}*LinkList,*pNode,Node;
void *getMemory(size_t size) {
return malloc(size);
}
LinkList getEmptyList() {
LinkList head = (pNode)getMemory(sizeof(Node));
memset(head,0,sizeof(Node));
return head;
}
int addNode(LinkList head,pNode pnode) {
pNode q;
for(q = head; q->next; q = q->next)
if(q->next->pauline.number == pnode->pauline.number) {
printf("重复的学号:%d\n",pnode->pauline.number);
return 0;
}
q->next = pnode;
pnode->next = NULL;
return 1;
}
// 按学号升排序
void sortNumber(LinkList head) {
pNode p,q,pt,qt;
p = head;
while(p->next) {
qt = p;
q = p->next;
while(q->next) {
if(qt->next->pauline.number > q->next->pauline.number)
qt = q;
q = q->next;
}
if(qt != p) {
pt = p->next;
p->next = qt->next;
qt->next = qt->next->next;
p->next->next = pt;
}
p = p->next;
}
}
// 按第th门成绩降排序,th = 1,2,3
void sortScore(LinkList head,int th) {
pNode p,q,pt,qt;
int i;
if(th < 1 || th > 3) return;
i = th - 1;
for(p = head; p->next; p = p->next) {
qt = p;
q = p->next;
while(q->next) {
if(qt->next->pauline.score[i] < q->next->pauline.score[i])
qt = q;
q = q->next;
}
if(qt != p) {
pt = p->next;
p->next = qt->next;
qt->next = qt->next->next;
p->next->next = pt;
}
}
}
void show(LinkList head) {//显示学生信息
pNode p;
for(p = head->next; p; p = p->next ) {
printf("%d\t%s\t%s\t%.2lf\t%.2lf\t%.2lf\n",
p->pauline.number,p->pauline.name,p->pauline.id,
p->pauline.score[0],p->pauline.score[1],p->pauline.score[2]);
}
}
pNode readData() {
pNode pnode = (pNode)getMemory(sizeof(Node));
int i;
printf("学 号:");
scanf("%d",&pnode->pauline.number);
printf("姓 名:");
scanf("%s",pnode->pauline.name);
printf("身份证:");
scanf("%s",pnode->pauline.id);
for(i = 0; i < 3; ++i) {
printf("第%d门成绩:",i + 1);
scanf("%lf",&pnode->pauline.score[i]);
}
return pnode;
}
void menu() {
printf("********************************\n");
printf("****** 学生成绩管理系统 ******\n");
printf("********************************\n");
printf("* 1、添加学生信息 *\n");
printf("* 2、显示学生信息 *\n");
printf("* 3、按学号排序 *\n");
printf("* 4、按成绩排序 *\n");
printf("********************************\n");
printf("* 0、退出 *\n");
printf("********************************\n");
}
int main() {
char op[20];
int select;
LinkList head = getEmptyList();
do {
menu();
printf("请选择:");
fflush(stdin);
fgets(op,20,stdin);
fflush(stdin);
switch(op[0] - '0') {
case 1 : addNode(head,readData()); break;
case 2 : show(head); break;
case 3 : sortNumber(head); break;
case 4 : printf("按第几门功课排序;");
scanf("%d",&select);
sortScore(head,select);
break;
}
}while(op[0] - '0');
printf("END\n");
return 0;
}
追问太谢谢了,我去调试下,谢谢大神
在吗,这个代码怎么加一个save函数用来保存数据,现在输入信息关闭后数据直接就消失了。急求,大神
来自:求助得到的回答