|
|
#include<stdio.h>
|
|
|
#include<string.h>
|
|
|
#include<malloc.h>
|
|
|
#include<windows.h>
|
|
|
|
|
|
#define BUFSIZE 20
|
|
|
|
|
|
struct node //结构体
|
|
|
{
|
|
|
int num; //学号
|
|
|
char name[15]; // 姓名
|
|
|
int gs; //高数成绩
|
|
|
int yy; //英语成绩
|
|
|
int c; //c语言成绩
|
|
|
node *next;//指针
|
|
|
};
|
|
|
|
|
|
node *create()//创建学生信息
|
|
|
{
|
|
|
struct node * head,* p,* q; //*p指向头,而*q指向尾。
|
|
|
int number;
|
|
|
char sname[15];
|
|
|
int sgs,syy,sc;
|
|
|
head=(node *)malloc(sizeof(node)); //动态分配内存
|
|
|
if(head==NULL)
|
|
|
{printf("内存分配失败!创建失败!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
printf("请输入学号,姓名,高数,英语,C语言\n");
|
|
|
scanf("%d",&number);//输入学号
|
|
|
scanf("%s",&sname);//输入姓名
|
|
|
scanf("%d",&sgs);//输入高数成绩
|
|
|
scanf("%d",&syy);//输入英语成绩
|
|
|
scanf("%d",&sc);//输入c语言成绩
|
|
|
head->num=number;//赋值
|
|
|
head->gs=sgs;//赋值
|
|
|
head->yy=syy;//赋值
|
|
|
head->c=sc;//赋值
|
|
|
strcpy(head->name,sname);//赋值
|
|
|
head->next=NULL;//指针域为空
|
|
|
p=head;
|
|
|
while(1)
|
|
|
{
|
|
|
q=(node *)malloc(sizeof(node));//动态分配内存
|
|
|
printf("请输入学号,姓名,高数,英语,C语言(输入0时结束)\n");
|
|
|
scanf("%d",&number);//输入学号
|
|
|
if(number==0)
|
|
|
{
|
|
|
break;//输入0跳出信息输入
|
|
|
}
|
|
|
scanf("%s",&sname);//输入姓名
|
|
|
scanf("%d",&sgs);//输入高数成绩
|
|
|
scanf("%d",&syy);//输入英语成绩
|
|
|
scanf("%d",&sc);//输入c语言成绩
|
|
|
q->num=number;//赋值
|
|
|
q->gs=sgs;//赋值
|
|
|
q->yy=syy;//赋值
|
|
|
q->c=sc;//赋值
|
|
|
strcpy(q->name,sname);//赋值
|
|
|
q->next=NULL;//q指向下个结构体的尾部,指针域为空
|
|
|
p->next=q; //
|
|
|
p=q;
|
|
|
|
|
|
}
|
|
|
return head;//返回头结点
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
node *jiaru(node *head)//添加学生信息
|
|
|
{
|
|
|
node *p,* q;
|
|
|
int number;
|
|
|
char sname[15];
|
|
|
int sgs,syy,sc;
|
|
|
p=head;
|
|
|
while(p->next!=NULL)
|
|
|
{
|
|
|
p=p->next;
|
|
|
}
|
|
|
while(1)
|
|
|
{
|
|
|
q=(node *)malloc(sizeof(node));//动态分配内存
|
|
|
printf("请输入学号,姓名,高数,英语,C语言(输入0时结束)\n");
|
|
|
scanf("%d",&number);//输入学号
|
|
|
if(number==0)
|
|
|
{
|
|
|
break;//输入0跳出信息输入
|
|
|
}
|
|
|
scanf("%s",&sname);//输入姓名
|
|
|
scanf("%d",&sgs);//输入高数成绩
|
|
|
scanf("%d",&syy);//输入英语成绩
|
|
|
scanf("%d",&sc);//输入c语言成绩
|
|
|
q->num=number;//赋值
|
|
|
q->gs=sgs;//赋值
|
|
|
q->yy=syy;//赋值
|
|
|
q->c=sc;//赋值
|
|
|
strcpy(q->name,sname);//赋值
|
|
|
q->next=NULL;//指针域为空
|
|
|
p->next=q;
|
|
|
p=q;
|
|
|
|
|
|
}
|
|
|
return head;//返回头结点
|
|
|
}
|
|
|
|
|
|
|
|
|
node * delete_name(node *head)//按姓名删除学生信息
|
|
|
{
|
|
|
node *b,*p;
|
|
|
p=head;
|
|
|
char qname[15];
|
|
|
char c;
|
|
|
printf("请输入要删除学生姓名:");
|
|
|
scanf("%s",&qname);//输入要删除学生姓名
|
|
|
while(1)
|
|
|
{
|
|
|
if(p==NULL)
|
|
|
{
|
|
|
return head;//若信息为空则返回原头结点
|
|
|
}
|
|
|
if(strcmp(p->name,qname)==0)
|
|
|
{
|
|
|
printf("确认删除y/n:");
|
|
|
scanf("%s",&c);
|
|
|
if(c=='y')
|
|
|
{
|
|
|
if(p==head)//判断头指针
|
|
|
{
|
|
|
head=p->next;
|
|
|
free(p);//释放内存空间
|
|
|
printf("删除成功!\n");
|
|
|
return head;//返回头结点
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
b->next=p->next;//使p结点后面的链表和前面的链表连接
|
|
|
free(p);//释放内存空间
|
|
|
printf("删除成功!\n");
|
|
|
return head;//返回头结点
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
printf("退出删除!\n");
|
|
|
}
|
|
|
b=p;
|
|
|
p=p->next;//向下一个结点
|
|
|
}
|
|
|
printf("删除出错!\n");
|
|
|
return head;//返回头结点
|
|
|
}
|
|
|
|
|
|
node * delete_i(node *head)//按学号删除学生信息
|
|
|
{
|
|
|
node *b,*p;
|
|
|
p=head;
|
|
|
int i;
|
|
|
char c;
|
|
|
printf("请输入要删除学生学号:");
|
|
|
scanf("%d",&i);//输入要删除学生学号
|
|
|
while(1)
|
|
|
{
|
|
|
if(p==NULL)
|
|
|
{
|
|
|
return head;//若信息为空则返回原头结点
|
|
|
}
|
|
|
if(p->num==i)
|
|
|
{
|
|
|
printf("确认删除y/n:");
|
|
|
scanf("%s",&c);
|
|
|
if(c=='y')
|
|
|
{
|
|
|
if(p==head)
|
|
|
{
|
|
|
head=p->next;
|
|
|
free(p);//释放内存空间
|
|
|
printf("删除成功!\n");
|
|
|
return head;//返回头结点
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
b->next=p->next;//使p结点后面的链表和前面的链表连接
|
|
|
free(p);//释放内存空间
|
|
|
printf("删除成功!\n");
|
|
|
return head;//返回头结点
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
printf("退出删除!\n");
|
|
|
}
|
|
|
b=p;
|
|
|
p=p->next;//向下一个结点
|
|
|
}
|
|
|
printf("删除出错!\n");
|
|
|
return head;//返回头结点
|
|
|
}
|
|
|
|
|
|
|
|
|
int query_name(node * head)//按姓名查找学生信息
|
|
|
{
|
|
|
struct node * p;
|
|
|
char sname[20];
|
|
|
p=head;
|
|
|
printf("请输入你要查找的姓名:");
|
|
|
scanf("%s",&sname);//输入要查找学生姓名
|
|
|
while(p!=NULL)//循环条件p结点不为NULL
|
|
|
{
|
|
|
if(strcmp(p->name,sname)==0)
|
|
|
{
|
|
|
printf("---------------------------------------------\n");
|
|
|
printf(" %d | %s | %d | %d | %d\n",p->num,p->name,p->gs,p->yy,p->c);
|
|
|
printf("---------------------------------------------\n");
|
|
|
return 1;
|
|
|
}
|
|
|
p=p->next;//向下一个结点
|
|
|
}
|
|
|
printf("没有找到!!!\n");
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int query_i(node * head)//按学号查找学生信息
|
|
|
{
|
|
|
struct node * p;
|
|
|
int i;
|
|
|
p=head;
|
|
|
printf("请输入你要查找的学号:");
|
|
|
scanf("%d",&i);//输入要查找学生姓学号
|
|
|
while(p!=NULL)//循环条件p结点不为NULL
|
|
|
{
|
|
|
|
|
|
if(p->num==i)
|
|
|
{
|
|
|
printf(" 学号 姓名 高数 英语 c语言\n");
|
|
|
printf("---------------------------------------------\n");
|
|
|
printf(" %d | %s | %d | %d | %d\n",p->num,p->name,p->gs,p->yy,p->c);
|
|
|
printf("---------------------------------------------\n");
|
|
|
return 1;
|
|
|
}
|
|
|
p=p->next;//向下一个结点
|
|
|
}
|
|
|
printf("没有找到!!!\n");
|
|
|
return 0;
|
|
|
}
|
|
|
int install_name(node * head)//插入学生信息到某姓名学生之后
|
|
|
{
|
|
|
struct node *p,*n;
|
|
|
p=head;
|
|
|
n=(node *)malloc(sizeof(node));//动态分配存储空间
|
|
|
int number;
|
|
|
char sname[15],qname[15];
|
|
|
int sgs,syy,sc;
|
|
|
printf("请输入学号,姓名,高数,英语,C语言\n");
|
|
|
scanf("%d",&number);//输入学号
|
|
|
scanf("%s",&sname);//输入姓名
|
|
|
scanf("%d",&sgs);//输入高数成绩
|
|
|
scanf("%d",&syy);//输入英语成绩
|
|
|
scanf("%d",&sc);//输入c语言成绩
|
|
|
n->num=number;//赋值
|
|
|
n->gs=sgs;//赋值
|
|
|
n->yy=syy;//赋值
|
|
|
n->c=sc;//赋值
|
|
|
strcpy(n->name,sname);//赋值
|
|
|
n->next=NULL;//指针域为空
|
|
|
printf("请输入要插入到那位学生姓名后面:");
|
|
|
scanf("%s",&qname);
|
|
|
while(p!=NULL)//循环条件p结点不为NULL
|
|
|
{
|
|
|
if(strcmp(p->name,qname)==0)
|
|
|
{
|
|
|
n->next=p->next;//连接新结点
|
|
|
p->next=n;//连接新结点
|
|
|
return 1;
|
|
|
}
|
|
|
p=p->next;//向下一个结点
|
|
|
}
|
|
|
printf("没有找到该学生!!\n");
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int install_i(node * head)//插入学生信息到第几位学生之后
|
|
|
{
|
|
|
struct node *p,*n;
|
|
|
p=head;
|
|
|
n=(node *)malloc(sizeof(node));// 动态分配存储空间
|
|
|
int number,inno,j=1;
|
|
|
char sname[15]; ;
|
|
|
int sgs,syy,sc;
|
|
|
printf("请输入学号,姓名,高数,英语,C语言\n");
|
|
|
scanf("%d",&number);//输入学号
|
|
|
scanf("%s",&sname);//输入姓名
|
|
|
scanf("%d",&sgs);//输入高数成绩
|
|
|
scanf("%d",&syy);//输入英语成绩
|
|
|
scanf("%d",&sc);//输入c语言成绩
|
|
|
n->num=number;//赋值
|
|
|
n->gs=sgs;//赋值
|
|
|
n->yy=syy;//赋值
|
|
|
n->c=sc;//赋值
|
|
|
strcpy(n->name,sname);//赋值
|
|
|
n->next=NULL;//指针域为空
|
|
|
printf("请输入要插入到第几位学生姓名后面:");
|
|
|
scanf("%d",&inno);
|
|
|
while(p!=NULL&&j<inno)//
|
|
|
{
|
|
|
p=p->next;
|
|
|
j++;
|
|
|
}
|
|
|
if(j==inno)
|
|
|
{
|
|
|
n->next=p->next;
|
|
|
p->next=n;
|
|
|
return 1;
|
|
|
}
|
|
|
printf("没有找到该学生!!\n");
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int xiugai_name(node *head)//通过姓名修改学生信息
|
|
|
{
|
|
|
node *p;
|
|
|
p=head;
|
|
|
char sname[15];
|
|
|
int sgs,syy,sc,number;
|
|
|
if(p==NULL)//判断是否为空
|
|
|
{
|
|
|
printf("无学生信息\n");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("请输入你需要修改的学生姓名:\n");
|
|
|
scanf("%s",&sname);
|
|
|
while(p!=NULL)
|
|
|
{
|
|
|
if(strcmp(p->name,sname)==0)//找到该同学
|
|
|
{
|
|
|
printf("请修改该生学号:\n");
|
|
|
scanf("%d",&number);
|
|
|
p->num=number;
|
|
|
printf("请修改该生姓名:\n");
|
|
|
scanf("%s",&sname);
|
|
|
strcpy(p->name,sname);
|
|
|
printf("请修改该生三科成绩:\n");
|
|
|
scanf("%d%d%d",&sgs,&syy,&sc);
|
|
|
p->gs=sgs;
|
|
|
p->yy=syy;
|
|
|
p->c=sc;
|
|
|
return 1;
|
|
|
}
|
|
|
p=p->next;
|
|
|
}
|
|
|
printf("无该学生信息!!!\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int xiugai_i(node *head)//通过学号修改学生信息
|
|
|
{
|
|
|
node *p;
|
|
|
p=head;
|
|
|
char sname[15];
|
|
|
int sgs,syy,sc,number;
|
|
|
if(p==NULL)//判断是否为空
|
|
|
{
|
|
|
printf("无学生信息\n");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("请输入你需要修改的学生学号:\n");
|
|
|
scanf("%d",&number);
|
|
|
while(p!=NULL)
|
|
|
{
|
|
|
if(p->num==number)//找到该同学
|
|
|
{
|
|
|
printf("请修改该生学号:\n");
|
|
|
scanf("%d",&number);
|
|
|
p->num=number;
|
|
|
printf("请修改该生姓名:\n");
|
|
|
scanf("%s",&sname);
|
|
|
strcpy(p->name,sname);
|
|
|
printf("请修改该生三科成绩:\n");
|
|
|
scanf("%d%d%d",&sgs,&syy,&sc);
|
|
|
p->gs=sgs;
|
|
|
p->yy=syy;
|
|
|
p->c=sc;
|
|
|
return 1;
|
|
|
}
|
|
|
p=p->next;
|
|
|
}
|
|
|
printf("无该学生信息!!!\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void printf(node * head)//输出所有信息
|
|
|
{
|
|
|
FILE *fp;
|
|
|
char buf[BUFSIZE];
|
|
|
//打开文件
|
|
|
fp=fopen("D:\\1.txt","r");
|
|
|
//判断是否成功
|
|
|
if(NULL==fp)
|
|
|
{
|
|
|
printf("打开文件失败!");
|
|
|
}
|
|
|
//获取文件
|
|
|
while(fgets(buf,BUFSIZE,fp))
|
|
|
{
|
|
|
printf("%s",buf);
|
|
|
}
|
|
|
//关闭文件
|
|
|
fclose(fp);
|
|
|
struct node *p;
|
|
|
p=head;
|
|
|
printf("\n\n新添加的学生成绩信息!");
|
|
|
printf("\n 学号 姓名 高数 英语 c语言\n");
|
|
|
while(p!=NULL)//循环条件p结点不为NULL
|
|
|
{
|
|
|
printf("---------------------------------------------\n");
|
|
|
printf(" %d | %s | %d | %d | %d\n",p->num,p->name,p->gs,p->yy,p->c);
|
|
|
p=p->next;
|
|
|
}
|
|
|
printf("---------------------------------------------\n");
|
|
|
}
|
|
|
|
|
|
int main()//主函数
|
|
|
{
|
|
|
|
|
|
char name[15];
|
|
|
int n;
|
|
|
struct node * head;
|
|
|
system("color 4f");
|
|
|
printf("\n*************学生成绩管理系统****************\n");
|
|
|
printf("*********************************************\n");
|
|
|
while(1)
|
|
|
{
|
|
|
printf(" * 1.输入信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 2.添加学生信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 3.根据姓名查找学生信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 4.根据学号查找学生信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 5.根据姓名删除学生信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 6.根据学号删除学生信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 7.根据姓名修改学生信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 8.根据学号修改学生信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 9.插入学生信息到某姓名学生之后 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 10.插入学生信息到第几位学生之后 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 11.输出所有学生信息 *\n");
|
|
|
printf(" * *\n");
|
|
|
printf(" * 0.退出 *\n\n");
|
|
|
printf("请选择操作:");
|
|
|
scanf("%d",&n); //判断操作
|
|
|
if(n==1)
|
|
|
head=create();
|
|
|
if(n==2)
|
|
|
head=jiaru(head);
|
|
|
if(n==3)
|
|
|
query_name(head);
|
|
|
if(n==4)
|
|
|
query_i(head);
|
|
|
if(n==5)
|
|
|
head=delete_name(head);
|
|
|
if(n==6)
|
|
|
head=delete_i(head);
|
|
|
if(n==7)
|
|
|
xiugai_name(head);
|
|
|
if(n==8)
|
|
|
xiugai_i(head);
|
|
|
if(n==9)
|
|
|
install_name(head);
|
|
|
if(n==10)
|
|
|
install_i(head);
|
|
|
if(n==11)
|
|
|
printf(head);
|
|
|
if(n==0)
|
|
|
{
|
|
|
printf("谢谢使用!\n");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|