1.對於學生管理系統,能夠實現的方法有許多,但是今天我們用鏈表的方法來實現。雖然初學者很可能看不懂,但是不要緊,這是要在整體的系統的學習完C語言之後,我才編寫出的程序。所以大家不必要擔心。在這裡與大家分享我的學生管理系統的鏈表的實現過程。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct student
{
char bianhao[16];
char name[16];
struct student *next;
};
struct student *creat()//創建鏈表
{
struct student *head,*p,*end;
head=p=end=(struct student*)malloc(sizeof(struct student));
printf("請輸入學生的編號:\n");
scanf("%s",p->bianhao);
while(strcmp(p->bianhao,"0")!=0)
{
end=p;
printf("請輸入學生的姓名\n");
scanf("%s",p->name);
p=(struct student *)malloc(sizeof(struct student));
end->next=p;
printf("請輸入學生的編號\n");
scanf("%s",p->bianhao);
}
end->next=NULL;
}
void save(struct student *head)//將鏈表保存為文件形式
{
FILE *fp;
struct student *p;
char filename[20];
int ch;
printf("請輸入要保存的文件名\n");
scanf("%s",filename);
if(fp=fopen(filename,"r")!=NULL)
{
printf("該文件已經存在,是否覆蓋?\n");
printf("1-覆蓋,2-不覆蓋\n");
scanf("%d",&ch);
if(ch!=1)
{
printf("請重新輸入要保存的文件名\n");
scanf("%s",filename);
}
}
if(fp=fopen(filename,"w")==NULL)
{
printf("創建文件失敗\n");
}
p=head;
while(p!=NULL)
{
fprintf(fp,"%s\n",p->bianhao);
fprintf(fp,"%s\n",p->name);
p=p->next;
}
fputs("over",fp);
printf("文件保存成功\n");
fclose(fp);
}
void output(struct student *head)//輸出鏈表
{
struct student *p;
p=head;
if(p==NULL)
{
printf("沒有創建任何記錄\n");
}
while(p!=NULL)
{
printf("%s",p->bianhao);
printf("%s",p->name);
p=p->next;
}
}
struct student *openfile()//打開文件,即新創建連表讀取鏈表文件
{
struct student *head,*p,*f;
FILE *fp;
char filename[20];
int ch;
printf("請輸入想要打開的文件名\n");
scanf("%s",filename);
if(fp=fopen(filename,"r")==NULL)
{
printf("打開文件失敗\n");
printf("1-請重新輸入文件名\n2-退出");
scanf("%d",&ch);
if(ch==1)
scanf("%s",filename);
else if(ch==2)
return NULL;
}
head=p=f=(struct student *)malloc(sizeof(struct student));
fscanf(fp,"%s%s",p->bianhao,p->name);
while(!feof(fp))
{
p=(struct student *)malloc(sizeof(struct student));
f->next=p;//用以節點的鏈接,是f節點的下一個是p節點,從而就將p,與f聯系起來了
fscanf(fp,"%s%s",p->bianhao,p->name);
if(strcmp(p->bianhao,"over")==0)
{
f->next=NULL;
printf("文件打開成功\n");
}
f=p;
}
}
void sort_hao(struct student *head)//按照標號排序
{
struct student *p,*f,*t;
char ch[100];
int i;
p=f=t=head;
if(head==NULL)
{
printf("沒有打開任何文件\n");
}
for(p=head;p->next!=NULL;p=p->next)
{
for(t=head;f=t->next;t=t->next,f=f->next)
{
if(strcmp(t->bianhao,f->bianhao)>0)
{
strcpy(ch,t->bianhao);
strcpy(t->bianhao,f->bianhao);
strcpy(f->bianhao,ch);
strcpy(ch,t->name);
strcpy(t->name,f->name);
strcpy(f->name,ch);
}
}
}
printf("排序完成\n");
}
void sort_name(struct student *head)//按照姓名排序
{
struct student *p,*f,*t;
char ch[100];
int i;
p=f=head=t;
if(head=NULL)
{
printf("文件未能打開\n");
}
for(p=head;p->next!=NULL;p=p->next)
{
for(t=head;f=t->next!=NULL;t=t->next,f=f->next)
{
if(strcmp(t->name,f->name)>0)
{
strcpy(ch,t->bianhao);
strcpy(t->bianhao,f->bianhao);
strcpy(f->bianhao,ch);
strcpy(ch,t->name);
strcpy(t->name,f->name);
strcpy(f->name,ch);
}
}
}
printf("完成排序\n");
}
void search(struct student *head)//查詢
{
struct student *p;
char str[20];
int i,j=0;
p=head;
if(head==NULL)
{
printf("沒有打開任何文件\n");
}
printf("1-按照編號查詢\n2-按照姓名查詢\n");
scanf("%d",&i);
if(i=1)
{
printf("請輸入學生的編號\n");
}
else
{
printf("請輸入學生的姓名\n");
}
scanf("%s",str);
while(p!=NULL)
{
if(i==1)
{
if(strcmp(p->bianhao,str)==0)
{
printf("編號:%s\n姓名:%s\n",p->bianhao,p->name);
j=1;
break;
}
}
if(i==2)
{
if(strcmp(p->name,str)==0)
{
printf("編號:%s\n姓名:%s\n",p->name,p->name);
j=1;
}
}
p=p->next;
}
if(j==0)
{
printf("查找完畢,沒有找到任何結果\n");
}
}
struct student *add(struct student *head)//添加學生的信息
{
struct student *p,*e,*f,*h;
if(head==NULL)
{
printf("打開文件失敗\n");
}
h=e=f=head;
p=(struct student *)malloc(sizeof(struct student));//新添加節點,即新添加記錄
printf("編號:\n");
scanf("%s",p->bianhao);
printf("姓名:\n");
scanf("%s",p->name);
if(strcmp(f->bianhao,p->bianhao)>0)
{
p->next=f;
h=p;
printf("添加成功\n");
}
if(f->next=NULL)
{
f->next=p;
p->next=NULL;
printf("添加成功\n");
}
while(f->next!=NULL)
{
if(f->next==NULL)
{
f->next=p;
p->next=NULL;
printf("添加成功\n");
}
}
}
struct student *delete_mem(struct student *head)//刪除個人信息
{
struct student *p,*e;
char str[20];
if(head==NULL)
{
printf("未能打開任何文件\n");
}
p=e=head;
printf("請輸入要刪除的編號\n");
scanf("%s",str);
if(strcmp(p->bianhao,str)==0)
{
head=head->next;
printf("刪除成功\n");
}
p=p->next;
while(p!=NULL)
{
if(strcmp(p->bianhao,str)==0)
{
if(p->next!=NULL)
e->next=p->next;
if(p->next==NULL)
e->next=NULL;
printf("刪除成功\n");
}
p=p->next;
e=e->next;
}
printf("搜索完畢,未能找到結果\n");
}
struct student *change(struct student *head)//修改學生的信息
{
struct student *p;
char str[20];
if(head==NULL)
{
printf("未能打開任何文件\n");
}
p=head;
printf("請輸入要修改的學生編號\n");
scanf("%s",p->bianhao);
while(p!=NULL)
{
if(strcmp(p->bianhao,str)==0)
{
printf("編號:%s\n姓名:%s\n",p->bianhao,p->name);
printf("請按照提示操作\n");
printf("編號\n");
scanf("%s",p->bianhao);
printf("姓名\n");
scanf("%s",p->name);
}
p=p->next;
}
printf("未能找到記錄\n");
}
void mima()//密碼的加密
{
FILE *fp;
char mima1[20],mima2[20];
int i=0;
if(fp=fopen("mima","r")==NULL)
{
printf("密碼尚未創建\n");
do
{
printf("請輸入密碼\n");
scanf("%s",mima1);
printf("請在此輸入密碼\n");
scanf("%s",mima2);
if(strcmp(mima1,mima2)!=0)
{
printf("兩次輸入的密碼不同,請重新輸入\n");
i=1;
}
else
break;
}while(i);
fp=fopen("mima","w");
fprintf(fp,"%s",mima1);
printf("密碼試制成功\n");
fclose(fp);
}
else
printf("密碼以創建\n");
}
void delete_doc()//文件的刪除操作
{
FILE *fp;
char mima1[20],mima2[20],filename[20];
printf("請輸入初始密碼\n");
scanf("%s",mima1);
fp=fopen("mima1","r");
fscanf(fp,"%s",mima2);
if(strcmp(mima1,mima2)==0)
{
printf("請輸入要刪除的文件名\n");
scanf("%s",filename);
if(remove(filename)==0)//remove函數的應用?
{
printf("刪除成功\n");
}
else
{
printf("刪除失敗");
}
}
else
printf("密碼錯誤\n");
}
void output_use()
{
printf("使用方法如下\n");
printf("1-編輯個人信息之後需要保存,否則再次啟動時會覆蓋\n");
printf("2-保存信息後,若要在原文件中添加,則需要先打開文件功能,然後再添加記錄,然後再保存\n");
printf("3-除了新建文件之外,進行其他功能,先打開文件才能繼續\n");
printf("4-編輯個人信息時,以學號0作為結束,故學生學號是沒有為0的\n");
printf("5-由於本系統的缺陷,故在保存前需要保存一個學生的信息\n");
}
void output_view()//功能表
{
printf("==========================\n");
printf("0-使用說明\n");
printf("1-編輯學生的信息\n");
printf("2-保存學生的信息\n");
printf("3-顯示學生的信息\n");
printf("4-打開記錄\n");
printf("5-將記錄排序\n");
printf("6-查詢記錄\n");
printf("7-添加記錄\n");
printf("8-刪除記錄\n");
printf("9-修改記錄\n");
printf("a-設置密碼\n");
printf("b-刪除文件\n");
printf("==========================\n");
}
void main()//主函數
{
struct student *head=NULL;
char ch;
int i,j=0;
printf("歡迎使用本系統,按回車鍵進入系統\n");
getchar();
system("cls");
do
{
output_view();
printf("請選擇相應的操作\n");
scanf("%c",&ch);
switch(ch)
{
case '0':output_use();break;
case '1':head=creat();break;
case '2':save(head);break;
case '3':output(head);break;
case '4':head=openfile();break;
case '5':system("cls");
printf("1-按照編號排序\n");
printf("2-按照姓名排序\n");
scanf("%d",&i);
switch(i)
{
case 1:sort_hao(head);break;
case 2:sort_name(head);break;
}
break;
case '6':search(head);break;
case '7':head=add(head);break;
case '8':head=delete_mem(head);break;
case '9':head=change(head);break;
case 'a':mima();break;
case 'b':delete_doc();break;
}
printf("按回車鍵返回\n");
getchar();
system("cls");
j=1;
}while(j);
}
2.希望與大家共同努力,共同上進。
長風破浪會有時,直掛雲帆濟滄海!