#include <stdio.h>
#include <malloc.h>
#define len sizeof(struct student)
#define len sizeof(struct student)
#define null 0
struct student
{char name[20];
char num[8];
int score;
struct student *next;};
void creat_txt()
{FILE *fp;
if((fp=fopen("f:\\student.txt","a"))==null)
{printf("open file error\n");exit(0);
}
fclose(fp);
}
void
inpu()
{
struct student *p1,*p2,*head;
char ch;int falg;
void save(struct student *head);
creat_txt();
p1=p2=(struct student *)malloc(len);
printf("name=");scanf("%s",&p1->name);
printf("number=");scanf("%s",&p1->num);
printf("score=");scanf("%d",&p1->score);
head=p1;head->next=null;
while(1)
{printf("press 0 to end,any key to continue...\n");
getchar();
if(ch=getchar()=='0')break;
printf("input student name num score:\n");
p1=(struct student *)malloc(len);
printf("name=");scanf("%s",&p1->name);
printf("number=");scanf("%s",&p1->num);
printf("score=");scanf("%d",&p1->score);
p2->next=p1;
p1->next=null;
p2=p1;
}
printf("save,please wait......\n");
save(head);
}
void list()
{FILE *list;
struct student temp;
struct student out;
temp.name[0]='0';
if((list=fopen("f:\\student.txt","r"))==null)
{printf("open file error \n");exit(0);
}
printf("name
number
score\n");
while(!feof(list))
{
if(temp.name!=0);
{out.name=temp.name;
out.num=temp.num;
out.score=temp.score;}
fread(&temp,len,1,list);
if(out.name[0]!='0')printf("%-10s %s %5d\n",out.name,out.num,out.score);
}
printf("\n");
fclose(list);
}
void save(struct student *head)
{FILE *open;
struct student *p1;
p1=head;
if((open=fopen("f:\\student.txt","a"))==null)
{printf("open file error\n");exit(0);
}
while(p1)
{fwrite(p1,len,1,open);
//fprintf(open,"\n");
p1=p1->next;
printf("1 successful.....\n");
}
fclose(open);
printf("save successful\n");
getchar();
}
struct student *look_for(struct student *head,struct student key)
{struct student *p1;
p1=head;
while(1)
{if(p1->next==null){p1=null;break;}
if(strcmp(p1->name,key.name)==0)break;
else
p1=p1->next;
}
return(p1);
}
void look()
{struct student *read_in_link();
void list();
struct student *look_for(struct student *head,struct student key);
struct student key;
struct student *answer;
struct student *head;
list();
printf("please input name.\n");
scanf("%s",&key.name);
getchar();
head=read_in_link();
answer=look_for(head,key);
if(answer!=null)
printf("%s,
%s,
%d\n",answer->name,answer->num,answer->score);
else
printf("this person is not in!\n");
}
struct student *read_in_link()
{FILE *fp;
struct student *p1,*p2,*head;
struct student stu;
void print_link(struct student *head);
if((fp=fopen("f:\\student.txt","r+"))==null)
{printf("open file error \n");exit(0);
}
p1=p2=(struct student *)malloc(len);
head=p1;
while(!feof(fp))
{fread(&stu,len,1,fp);
p1->name=stu.name;p1->num=stu.num;
p1->score=stu.score;
p2->next=p1;
p2=p1;
p1->next=null;
p1=(struct student *)malloc(len);
}
p2->next=null;
free(p1);
fclose(fp);
//printf("name=%s
num=%s
score=%d \n",head->name,head->num,head->score);
//print_link(head);
return(head);
}
void print_link(struct student *head)
{struct student *p1;
p1=head;
printf("list of your database\n");
while(p1->next!=null)
{printf("%s
%s
%d\n",p1->name,p1->num,p1->score);
p1=p1->next;}
printf("===========================\n");
}
void delet()
{
void del(struct student *head,struct student *answer);
struct student *read_in_link();
void list();
struct student *look_for(struct student *head,struct student key);
struct student key,*head,*answer;
printf("please input the person'name that you want't to del\n");
scanf("%s",key.name);
head=read_in_link();
answer=look_for(head,key);
//printf("%s
%s
%d",answer->name,answer->num,answer->score);
if(answer!=null)del(head,answer);
else printf("this person is not in!\n");
}
void del(struct student *head,struct student *answer)
{struct student *p1;
struct student *p2;
void rewrite(struct student *head);
p1=p2=head;
if(answer==head)
{head=head->next;
rewrite(head);
}
else
{
while(strcmp(p1->name,answer->name)==0);
{
p2=p1;
p1=p1->next;
}
p2->next=p1->next;
free(p1);
print_link(head);
rewrite(head);
list();
}
}
void rewrite(struct student *head)
{FILE *open;
void print_link(struct student *head);
struct student *p1;
p1=head;
if((open=fopen("f:\\student.txt","w+"))==null)
{printf("open file error\n");exit(0);
}
while(p1->next!=null)
{fwrite(p1,len,1,open);
p1=p1->next;
printf("1 successful.....\n");
}
fclose(open);
printf("del successful\n");
print_link(head);
getchar();
}
void print()
{printf("
=======================================\n");
printf("
1. input student messige.\n");
printf("
2.look for student messige.\n");
printf("
3.delet student message.\n");
printf("
4.list the database:\n");
printf("
5.exit\n");
printf("
======================================\n");
}
void menu()
{int key;
void inpu(void);
void look(void);
void delet(void);
while(1)
{print();
scanf("%d",&key);
if(key<0||key>9)printf("error\n");
switch(key)
{case 1:
inpu();break;
case 2:
look();break;
case 3:
delet();break;
case 4:
list();break;
case 5:
exit(1);break;
}
}
}
main()
{
menu();
}
以前写的,你把不要的功能删掉吧.
代码没优化过,也没做过什么注释,你将就参考参考吧~