#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
int shoudsave=0; /* */
struct student
{
char num[10];/* 学号 */
char name[20];
char sex[4];
int cgrade;
int mgrade;
int egrade;
int totle;
int ave;
char neartime[10];/* 最近更新时间 */
};
typedef struct node
{
struct student data;
struct node *next;
}Node,*Link;
void menu()
{
printf("********************************************************************************");
printf("\t1登记学生资料\t\t\t\t\t2删除学生资料\n");
printf("\t3查询学生资料\t\t\t\t\t4修改学生资料\n");
printf("\t5保存学生资料\t\t\t\t\t0退出系统\n");
printf("********************************************************************************\n");
}
void printstart()
{
printf("-----------------------------------------------------------------------\n");
}
void Wrong()
{
printf("\n=====>提示:输入错误!\n");
}
void Nofind()
{
printf("\n=====>提示:没有找到该学生!\n");
}
void printc() /* 本函数用于输出中文 */
{
printf(" 学号\t 姓名 性别 英语成绩 数学成绩 C语言成绩 总分 平均分\n");
}
void printe(Node *p)/* 本函数用于输出英文 */
{
printf("%-12s%s\t%s\t%d\t%d\t%d\t %d\t %d\n",p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade,p->data.totle,p->data.ave);
}
Node* Locate(Link l,char findmess[],char nameornum[]) /* 该函数用于定位连表中符合要求的接点,并返回该指针 */
{
Node *r;
if(strcmp(nameornum,"num")==0) /* 按学号查询 */
{
r=l->next;
while(r!=NULL)
{
if(strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
}
else if(strcmp(nameornum,"name")==0) /* 按姓名查询 */
{
r=l->next;
while(r!=NULL)
{
if(strcmp(r->data.name,findmess)==0)
return r;
r=r->next;
}
}
return 0;
}
void Add(Link l) /* 增加学生 */
{
Node *p,*r,*s;
char num[10];
r=l;
s=l->next;
while(r->next!=NULL)
r=r->next; /* 将指针置于最末尾 */
while(1)
{
printf("请你输入学号(以'0'返回上一级菜单:)");
scanf("%s",num);
if(strcmp(num,"0")==0)
break;
while(s)
{
if(strcmp(s->data.num,num)==0)
{
printf("=====>提示:学号为'%s'的学生已经存在,若要修改请你选择'4 修改'!\n",num);
printstart();
printc();
printe(s);
printstart();
printf("\n");
return;
}
s=s->next;
}
p=(Node *)malloc(sizeof(Node));
strcpy(p->data.num,num);
printf("请你输入姓名:");
scanf("%s",p->data.name);
getchar();
printf("请你输入性别:");
scanf("%s",p->data.sex);
getchar();
printf("请你输入c语言成绩:");
scanf("%d",&p->data.cgrade);
getchar();
printf("请你输入数学成绩:");
scanf("%d",&p->data.mgrade);
getchar();
printf("请你输入英语成绩:");
scanf("%d",&p->data.egrade);
getchar();
p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=p->data.totle / 3;
/* 信息输入已经完成 */
p->next=NULL;
r->next=p;
r=p;
shoudsave=1;
}
}
void Qur(Link l) /* 查询学生 */
{
int sel;
char findmess[20];
Node *p;
if(!l->next)
{
printf("\n=====>提示:没有资料可以查询!\n");
return;
}
printf("\n=====>1按学号查找\n=====>2按姓名查找\n");
scanf("%d",&sel);
if(sel==1)/* 学号 */
{
printf("请你输入要查找的学号:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
printf("\t\t\t\t查找结果\n");
printstart();
printc();
printe(p);
printstart();
}
else
Nofind();
}
else if(sel==2) /* 姓名 */
{
printf("请你输入要查找的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
{
printf("\t\t\t\t查找结果\n");
printstart();
printc();
printe(p);
printstart();
}
else
Nofind();
}
else
Wrong();
}
void Del(Link l) /* 删除 */
{
int sel;
Node *p,*r;
char findmess[20];
if(!l->next)
{
printf("\n=====>提示:没有资料可以删除!\n");
return;
}
printf("\n=====>1按学号删除\n=====>2按姓名删除\n");
scanf("%d",&sel);
if(sel==1)
{
printf("请你输入要删除的学号:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>提示:该学生已经成功删除!\n");
shoudsave=1;
}
else
Nofind();
}
else if(sel==2)
{
printf("请你输入要删除的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>提示:该学生已经成功删除!\n");
shoudsave=1;
}
else
Nofind();
}
else
Wrong();
}
void Modify(Link l)
{
Node *p;
char findmess[20];
if(!l->next)
{
printf("\n=====>提示:没有资料可以修改!\n");
return;
}
printf("请你输入要修改的学生学号:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
printf("请你输入新学号(原来是%s):",p->data.num);
scanf("%s",p->data.num);
printf("请你输入新姓名(原来是%s):",p->data.name);
scanf("%s",p->data.name);
getchar();
printf("请你输入新性别(原来是%s):",p->data.sex);
scanf("%s",p->data.sex);
printf("请你输入新的c语言成绩(原来是%d分):",p->data.cgrade);
scanf("%d",&p->data.cgrade);
getchar();
printf("请你输入新的数学成绩(原来是%d分):",p->data.mgrade);
scanf("%d",&p->data.mgrade);
getchar();
printf("请你输入新的英语成绩(原来是%d分):",p->data.egrade);
scanf("%d",&p->data.egrade);
p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=p->data.totle/3;
printf("\n=====>提示:资料修改成功!\n");
shoudsave=1;
}
else
Nofind();
}
void Disp(Link l)
{
int count=0;
Node *p;
p=l->next;
if(!p)
{
printf("\n=====>提示:没有资料可以显示!\n");
return;
}
printf("\t\t\t\t显示结果\n");
printstart();
printc();
printf("\n");
while(p)
{
printe(p);
p=p->next;
}
printstart();
printf("\n");
}
void Tongji(Link l)
{
Node *pm,*pe,*pc,*pt,*pa; /* 用于指向分数最高的接点 */
Node *r=l->next;
if(!r)
{
printf("\n=====>提示:没有资料可以统计!\n");
return ;
}
pm=pe=pc=pt=pa=r;
while(r!=NULL)
{
if(r->data.cgrade>=pc->data.cgrade)
pc=r;
if(r->data.mgrade>=pm->data.mgrade)
pm=r;
if(r->data.egrade>=pe->data.egrade)
pe=r;
if(r->data.totle>=pt->data.totle)
pt=r;
if(r->data.ave>=pa->data.ave)
pa=r;
r=r->next;
}
printf("------------------------------统计结果--------------------------------\n");
printf("总分最高者:\t%s %d分\n",pt->data.name,pt->data.totle);
printf("平均分最高者:\t%s %d分\n",pa->data.name,pa->data.ave);
printf("英语最高者:\t%s %d分\n",pe->data.name,pe->data.egrade);
printf("数学最高者:\t%s %d分\n",pm->data.name,pm->data.mgrade);
printf("c语言最高者:\t%s %d分\n",pc->data.name,pc->data.cgrade);
printstart();
}
void Sort(Link l)
{
Link ll;
Node *p,*rr,*s;
ll=(Link)malloc(sizeof(Node)); /* 用于做新的连表 */
ll->next=NULL;
if(l->next==NULL)
{
printf("\n=====>提示:没有资料可以排序!\n");
return ;
}
p=l->next;
while(p)
{
s=(Node*)malloc(sizeof(Node)); /* 新建接点用于保存信息 */
s->data=p->data;
s->next=NULL;
rr=ll;
while(rr->next!=NULL && rr->next->data.totle>=p->data.totle)
rr=rr->next;
if(rr->next==NULL)
rr->next=s;
else
{
s->next=rr->next;
rr->next=s;
}
p=p->next;
}
free(l);
l->next=ll->next;
printf("\n=====>提示:排序已经完成!\n");
}
void Save(Link l)
{
FILE* fp;
Node *p;
int flag=1,count=0;
fp=fopen("c:\\student","wb");
if(fp==NULL)
{
printf("\n=====>提示:重新打开文件时发生错误!\n");
exit(1);
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1)
{
p=p->next;
count++;
}
else
{
flag=0;
break;
}
}
if(flag)
{
printf("\n=====>提示:文件保存成功.(有%d条记录已经保存.)\n",count);
shoudsave=0;
}
fclose(fp);
}
void main()
{
Link l;/* 连表 */
FILE *fp; /* 文件指针 */
int sel;
char ch;
char jian;
int count=0;
Node *p,*r;
printf("\t\t\t\t学生成绩管理系统\n\t\t\t\t----------------------实验项目\n");
l=(Node*)malloc(sizeof(Node));
l->next=NULL;
r=l;
fp=fopen("C:\\student","rb");
if(fp==NULL)
{
printf("\n=====>提示:文件还不存在,是否创建?(y/n)\n");
scanf("%c",&jian);
if(jian=='y'||jian=='Y')
fp=fopen("C:\\student","wb");
else
exit(0);
}
printf("\n=====>提示:文件已经打开,正在导入记录......\n");
while(!feof(fp))
{
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node),1,fp)) /* 将文件的内容放入接点中 */
{
p->next=NULL;
r->next=p;
r=p; /* 将该接点挂入连中 */
count++;
}
}
fclose(fp); /* 关闭文件 */
printf("\n=====>提示:记录导入完毕,共导入%d条记录.\n",count);
while(1)
{
menu();
printf("请你选择操作:");
scanf("%d",&sel);
if(sel==0)
{
if(shoudsave==1)
{ getchar();
printf("\n=====>提示:资料已经改动,是否将改动保存到文件中(y/n)?\n");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(l);
}
printf("\n=====>提示:你已经退出系统,再见!\n");
break;
}
switch(sel)
{
case 1:Add(l);break; /* 增加学生 */
case 2:Del(l);break;/* 删除学生 */
case 3:Qur(l);break;/* 查询学生 */
case 4:Modify(l);break;/* 修改学生 */
case 5:Save(l);break;/* 保存学生 */
case 9:printf("\t\t\t==========帮助信息==========\n");break;
default: Wrong();getchar();break;
}
}
Sleep(100);
}
/* */
#include
#include
#include
#include
# define LEN sizeof(struct Student)
struct Student {
char num[10];
char name[20];
char sex[10];
int age;
char phone[12];
char qq[12];
float cscore;
float escore;
struct Student *next;
};
char filename[30];
struct Student *Creat(int n) {
void menu_print_in(void);
struct Student *head;
struct Student *p1, *p2;
system("cls");
for(int i=1;ip1 = (struct Student*)malloc(LEN);
menu_print_in();
scanf("%s%s%s%d%s%s%f%f",p1->num,p1->name,p1->sex,
&p1->age,p1->phone,p1->qq,&p1->cscore,&p1->escore);
p1->next = NULL;
if(i==1) {
head = p2 = p1;
}
else {
p2->next = p1;
p2 = p1;
}
}
return(head);
}
void WriteData_wb(struct Student *head) {
FILE *fp;
struct Student *p;
if((fp = fopen(filename, "wb"))==NULL)
printf("\a error! Can not open the file!");
p = head;
while(p!=NULL) {
if(fwrite(p,LEN,1,fp)!=1) {
printf("写入数据出错\n");
fclose(fp);
return;
}
p=p->next;
}
fclose(fp);
}
void WriteData_ab(struct Student *head) {
FILE *fp;
struct Student *p;
if((fp = fopen(filename, "ab"))==NULL)
printf("\a error! Can not open the file!");
p = head;
while(p!=NULL) {
if(fwrite(p,LEN,1,fp)!=1) {
printf("写入数据出错\n");
fclose(fp);
return;
}
p=p->next;
}
fclose(fp);
}
struct Student *ReadData(void) {
struct Student *head = NULL;
struct Student *p1, *p2;//s = p1;p = p2;
FILE *fp;
if((fp=fopen(filename,"rb+"))==NULL)
{
printf("打开文件出错\n");
exit(0);
}
while(!feof(fp)) {
if((p1=(struct Student*)malloc(LEN))==NULL){
printf("内存申请出错\n");
fclose(fp);
exit(0);
}
if(fread(p1,LEN,1,fp)!=1){
free(p1);
break;
}
if(head==NULL)
head=p2=p1;
else{
p2->next=p1;
p2=p1;
}
}
fclose(fp);
return (head);
}
void Print_inquire_all(void) {
void menu_print_out(void);
struct Student *pt;
pt = ReadData();
menu_print_out();
do {
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
pt = pt->next;
}while(pt!=NULL);
printf("\n\n");
}
int Print_inquire_num() {
void menu_print_out(void);
struct Student *pt;
char str_num[10];
printf("◎请输入您要查询的学号:");
scanf("%s", str_num);
pt = ReadData();
menu_print_out();
do {
if(strcmp(pt->num,str_num)==0) {
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
printf("\n\n");
return 0;
}
pt = pt->next;
}while(pt!=NULL);
printf("数据库中没有存储您要查询的数据!\n");
printf("\n\n");
return 0;
}
int Print_inquire_name() {
void menu_print_out(void);
struct Student *pt;
char str_name[20];
printf("◎请输入您要查询的姓名:");
scanf("%s", str_name);
pt = ReadData();
menu_print_out();
do {
if(strcmp(pt->name,str_name)==0) {
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
printf("\n\n");
return 0;
}
pt = pt->next;
}while(pt!=NULL);
printf("数据库中没有存储您要查询的数据!\n");
printf("\n\n");
return 0;
}
int Print_inquire_fuzzy(void) {
void menu_print_out(void);
struct Student *pt;
char str_find[20];
int m = 0;
printf("◎请输入您要查询的关键词:");
scanf("%s", str_find);
pt = ReadData();
menu_print_out();
do {
if(strstr(pt->num,str_find)!=0||strstr(pt->name,str_find)!=0
||strstr(pt->sex,str_find)!=0||strstr(pt->phone,str_find)!=0
||strstr(pt->qq,str_find)!=0) {
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
m = 1;
}
pt = pt->next;
}while(pt!=NULL);
if(!m)
printf("数据库中没有存储您要查询的数据!\n");
printf("\n\n");
return 0;
}
int Print_inquire_stats(void) {
struct Student *head, *p1;
float CMIN, CMAX, EMIN, EMAX, SMIN, SMAX;
head = ReadData();
p1 = head;
CMIN = CMAX = p1->cscore;
EMIN = EMAX = p1->escore;
SMIN = SMAX = p1->cscore + p1->escore;
while(p1->next!=NULL) {
if(p1->cscore > CMAX) {
CMAX = p1->cscore;
}
if(p1->cscore < CMIN) {
CMIN = p1->cscore;
}
if(p1->escore > EMAX) {
EMAX = p1->escore;
}
if(p1->escore < EMIN) {
EMIN = p1->escore;
}
if((p1->cscore + p1->escore) > SMAX) {
SMAX = (p1->cscore + p1->escore);
}
if((p1->cscore + p1->escore) < SMIN) {
SMIN = (p1->cscore + p1->escore);
}
p1 = p1->next;
}
printf("\n");
printf("◎数据库中C语言最高分为:%4.1f \n", CMAX);
printf("◎数据库中C语言最低分为:%4.1f \n\n", CMIN);
printf("◎数据库中英语最高分为:%4.1f \n", EMAX);
printf("◎数据库中英语最低分为:%4.1f \n\n", EMIN);
printf("◎数据库中总成绩最高分为:%4.1f \n", SMAX);
printf("◎数据库中总成绩最低分为:%4.1f \n\n", SMIN);
printf("\n");
printf("【PS:更多统计功能更在开发中,敬请期待O(∩_∩)O】\n\n");
return 0;
}
/*【1】修改数据之删除记录*/
int Delete() {
struct Student *pt1, *pt2, *head;
char str_num[20];
printf("\n◎请输入您要删除的学号信息:");
scanf("%s", str_num);
pt1 = ReadData();
pt2 = pt1->next;
head = pt1;
while(pt2!=NULL) {
if(strcmp(pt1->num,str_num)==0) {
WriteData_wb(pt2);
}
else if(strcmp(pt2->num,str_num)==0) {
pt1->next = pt2->next;
WriteData_wb(head);
}
pt2 = pt2->next;
pt1 = pt1->next;
}
if(pt2!=NULL)
printf("数据库中没有存储您要删除的数据!\n");
printf("\n\n");
return 0;
}
/*【2】修改数据之修改记录*/
int Amend() {
void menu_print_in(void);
struct Student *pt1, *pt2, *head;
char str_num[20];
printf("◎请输入您要修改的学号信息:");
scanf("%s", str_num);
pt1 = ReadData();
pt2 = pt1->next;
head = pt1;
while(pt2!=NULL) {
if(strcmp(pt1->num,str_num)==0) {
menu_print_in();
scanf("%s%s%s%d%s%s%f%f",pt1->num,pt1->name,pt1->sex,
&pt1->age,pt1->phone,pt1->qq,&pt1->cscore,&pt1->escore);
WriteData_wb(head);
}
else if(strcmp(pt2->num,str_num)==0) {
menu_print_in();
scanf("%s%s%s%d%s%s%f%f",pt2->num,pt2->name,pt2->sex,
&pt2->age,pt2->phone,pt2->qq,&pt2->cscore,&pt2->escore);
WriteData_wb(head);
}
pt2 = pt2->next;
pt1 = pt1->next;
}
if(pt2!=NULL)
printf("数据库中没有存储您要删除的数据!\n");
return 0;
}
int Neaten() {
struct Student *first;
struct Student *tail;
struct Student *p_min;
struct Student *min;
struct Student *p;
struct Student *head;
head = ReadData();
first = NULL;
while(head!=NULL) {
for(p=head,min=head; p->next!=NULL; p=p->next) {
if(strcmp(p->next->num,min->num)<0) {
p_min = p;
min = p->next;
}
}
if(first==NULL) {
first = min;
tail = min;
}
else {
tail->next = min;
tail = min;
}
if(min==head) {
head = head->next;
}
else {
p_min->next = min->next;
}
}
if(first!=NULL) {
tail->next = NULL;
}
head = first;
WriteData_wb(head);
return 0;
}
int Creat_num(void) {
printf("\n◎请输入您此次要添加的数据个数:");
int n;
if(scanf("%d", &n)!=1) {
printf("\a error!");
}
return n;
}
int File_name() {
printf("\n◎请输入您想要打开的文件:");
if(scanf("%s", filename)!=1)
printf("\a error!");
return 0;
}
void menu(void) {
void menu_add(void);
void menu_inquire(void);
void menu_amend(void);
printf(" ╭════════╮ \n");
printf("╭══════╣学生管理系统V1.0╠══════╮\n");
printf("║ ╰════════╯ ║\n");
printf("║ 【1】添加数据 【3】修改数据 ║\n");
printf("║ ║\n");
printf("║ 【2】查询数据 【4】退出系统 ║\n");
printf("║ ║\n");
printf("╰══════════════════════╯\n");
printf("◎请输入功能前的序号进入相应的工具:【 】\b\b");
int a = 0;
a = getchar();
while(a!='1'&&a!='2'&&a!='3'&&a!='4') {
printf("error! please input the right number!\n");
putchar('\a');
getchar();
printf("◎请重新输入功能前的序号进入相应的工具:【 】\b\b");
a = getchar();
}
switch(a) {
case '1': File_name();menu_add();
break;
case '2': File_name();menu_inquire();
break;
case '3': File_name();menu_amend();
break;
case '4': exit(0);
break;
}
getchar();
}
/*二级菜单之添加数据*/
void menu_add(void) {
system("cls");
getchar();
printf(" ╭════════╮ \n");
printf("╭══════╣ 添加数据方式 ╠══════╮\n");
printf("║ ╰════════╯ ║\n");
printf("║ 【1】新建文件 【2】增添数据 【3】返回菜单 ║\n");
printf("║ ║\n");
printf("╰══════════════════════╯\n");
printf("◎请输入功能前的序号进入相应的工具:【 】\b\b");
int a = 0;
a = getchar();
while(a!='1'&&a!='2'&&a!='3') {
printf("error! please input the right number!\n");
putchar('\a');
getchar();
printf("◎请重新输入功能前的序号进入相应的工具:【 】\b\b");
a = getchar();
}
switch(a) {
case '1': WriteData_wb(Creat(Creat_num()));
printf("\n◎新建文件成功且数据已成功保存◎\n");
system("pause");
system("cls");
menu_add();
break;
case '2': WriteData_ab(Creat(Creat_num()));
printf("\n◎数据已成功添加◎\n");
system("pause");
system("cls");
menu_add();
break;
case '3': system("cls");
getchar();
menu();
break;
}
}
/*二级菜单之查询数据*/
void menu_inquire(void) {
system("cls");
getchar();
while(1) {
system("cls");
printf(" ╭════════╮ \n");
printf("╭══════╣ 查询数据方式 ╠══════╮\n");
printf("║ ╰════════╯ ║\n");
printf("║ 【1】全量查询 【4】模糊查询 ║\n");
printf("║ ║\n");
printf("║ 【2】学号查询 【5】成绩统计 ║\n");
printf("║ ║\n");
printf("║ 【3】姓名查询 【6】返回菜单 ║\n");
printf("╰══════════════════════╯\n");
printf("◎请输入功能前的序号进入相应的工具:【 】\b\b");
int a = 0;
a = getchar();
while(a!='1'&&a!='2'&&a!='3'&&a!='3'&&a!='4'&&a!='5'&&a!='6') {
printf("error! please input the right number!\n");
putchar('\a');
getchar();
printf("◎请重新输入功能前的序号进入相应的工具:【 】\b\b");
a = getchar();
}
switch(a) {
case '1': Print_inquire_all();system("pause");getchar();
break;
case '2': Print_inquire_num();system("pause");getchar();
break;
case '3': Print_inquire_name();system("pause");getchar();
break;
case '4': Print_inquire_fuzzy();system("pause");getchar();;
break;
case '5': Print_inquire_stats();system("pause");getchar();
break;
case '6': system("cls");getchar();menu();
break;
}
}
}
这个现象一般是内存泄漏或者读取或者删除链表有误造成的!没有添加删除源代码,不好说呀