#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char name[32];
int score;
struct student *next;
};
struct student *head = NULL;
void do_insert()
{
struct student *p = NULL ;
while(1)
{
p = head;
struct student *new = (struct student*)malloc(sizeof(struct student));
new->next = NULL;
printf("请输入姓名,成绩,并通过quit结束输入\n");
printf("请输入姓名:");scanf("%s",new->name);getchar();
if(strstr("quit",new->name) !=NULL)
{
free(new);
new = NULL;
break;
}
printf("请输入成绩:");scanf("%d",&new->score);getchar();
if(p == NULL)
{
head = new;
}
else if(new->score <= head->score)
{
new->next = head;
head = new;
}
else
{
while( (p->next != NULL ) && ( new->score > p->next->score))
p = p->next;
new->next = p->next;
p->next = new;
}
}
}
int do_lookup()
{
struct student *p = head;
char name[32] = {'\0'};
printf("q请输入你要查找的学生名字\n");
scanf("%s",name);
getchar();
if(p == NULL)
{
printf("表格不存在\n");
return 0;
}
while(p!=NULL)
{
if(strcmp(p->name,name) == 0)
{
printf("%s 的成绩等于 %d\n",p->name,p->score);
return 0;
}
p = p->next;
}
if(p == NULL)
{
printf("不存在的学生名字\n");
}
}
int do_lookup1()
{
struct student *p = head;
int pos;
printf("请输入你要查找的学生位置:");
scanf("%d",&pos);
getchar();
if(p == NULL)
{
printf("表格不存在\n");
return 0;
}
else if(pos < 0 )
{
printf("pos error (pos < 0)\n");
return 0;
}
else
{
while( (p!=NULL) && (pos!=0))
{
pos--;
p = p->next;
}
if((pos == 0) && (p!= NULL))
{
printf("%s 的成绩是 %d\n",p->name,p->score);
return 0;
}
else
{
printf("no find\n");
}
}
}
int do_delete()
{
struct student *p = head;
struct student *q = NULL;
char name[32] = {'\0'};
printf("q请输入你要删除的学生名字\n");
scanf("%s",name);
getchar();
if(p == NULL)
{
printf("表格不存在\n");
return 0;
}
if(strcmp(p->name,name) == 0)
{
head = p->next;
free(p);
p = NULL;
return 0;
}
while(p!=NULL)
{
if (strcmp(p->next->name,name) == 0)
{
q = p->next;
p->next = p->next->next;
free(q);
q= NULL;
return 0;
}
p = p->next;
}
printf("此人不存在\n");
return 0;
}
int do_modify()
{
char name[12] = {'\0'};
struct student *p = head;
printf("请输入你要修改的名字:");
scanf("%s",name);
getchar();
if(p == NULL)
{
printf("empty form\n");
return 0;
}
while(p != NULL)
{
if(strcmp(p->name,name) == 0)
{
printf("请要输入修改的成绩:");
scanf("%d",&p->score);
getchar();
}
p = p->next;
}
printf("此人不存在\n");
return 0;
}
int do_findall()
{
struct student *p = head;
if(p == NULL)
{
printf("empty form\n");
return 0;
}
while(p != NULL)
{
printf("%s %d\n",p->name,p->score);
p = p->next;
}
}
void do_sort()
{
struct student *p = head;
struct student *q = head;
int score;
while(p)
{
while(q)
{
if(p->score > q->score)
{
score = q->score;
q->score = p->score;
p->score = score;
}
q = q->next;
}
p = p->next;
}
}
void do_reverseOrder()
{
struct student *p = NULL;
struct student temp;
temp.next = NULL;
while(head!=NULL)
{
p = head->next;
head->next = temp.next;
temp.next = head;
head = p;
}
head = temp.next;
}
int main()
{
int cmd;
printf("*****************************************************************\n");
printf("*******************成绩管理系统**********************************\n");
printf("***0: 遍历1: 插入2:指定查找 3:删除 4:修改 5:逆序 6.排序 7:退出***\n");
printf("***********20: 按位置查找\n");
printf("******************************************************************\n");
putchar(10);
while(1)
{
printf("请输入命令-> ");
scanf("%d",&cmd);
getchar();
switch(cmd)
{
case 0:do_findall();break;
case 1:do_insert();break;
case 2:do_lookup();break;
case 20:do_lookup1();break;
case 3:do_delete();break;
case 4:do_modify();break;
case 5:do_reverseOrder();break;
case 6:do_sort();break;
case 7:exit(0);
default: printf("cmd errror\n");break;
}
}
return 0;
}
{
new= malloc();
new->score = scanf();
new->next = NULL;
if(p == NULL)
{
p = head;
return ;
}
else if(new->score < p->score)
{
new->next = p;
p = new;
return ;
}
else
{
while((new->score > p->next->score )&& (p->next != NULL))
p = p->next;
new->next = p->next;
p->next = new;
}
}