求助大神,我们老师让做一个学生信息管理系统,我看了看你解答的,感觉和我们老师布置的作业差不多

2024-11-13 09:00:51
推荐回答(1个)
回答1:

#include
#include
#include
#include

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;
}