📄 考务管理.cpp
字号:
/* Note:Your choice is C IDE */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
#define LEN sizeof(struct addbook)
#define MAX 100
void InitDialBook(void); /*创建通讯录*/
void UserInterface(void); /*主菜单*/
void Load(void); /*读文件*/
void Add(void); /*添加成员信息*/
int Find(void); /*查找成员信息*/
void Modify(void); /*修改成员信息*/
void Delete(void); /*删除成员信息*/
void ShowAll(void); /*显示成员信息*/
void Save(void); /*保存成员信息*/
struct addbook{
int record;
char name[15];
char phone[15];
char addr[20];
char units[10];
char age[3];
char remark[15];
struct addbook *link;
};
FILE *fp;
struct addbook *p,*p1,*head,*s,*t;
void main()
{struct addbook *k,*w,*f,*j,*m;
int i,flag;
int choice;
if((fp = fopen("zzxx.txt","rb"))==NULL)
{ system("cls");
printf("\n\n\t\t\tcannot find the file!\n");
if((fp = fopen("zzxx.txt","wb"))==NULL)
{
printf("open file error!\n");
getch();
exit(0);
}
printf("\n\t\taddress list have not any message.please add\n");
InitDialBook();
printf("\n\t\t\tadd success!\n");
}
Load();
/* printf("\nthis is the %d menber of the file.\n",p->record); */
flag = 1;
while(flag)
{
UserInterface();
printf(" please enter your choice:(0-5):");
scanf("%d",&choice);
getchar(); /*getchar()用来吃回车符 !*/
if(choice>=0&&choice<6)
{
switch(choice)
{
case 1:
Add();break;
case 2:
Find();break;
case 3:
ShowAll();break;
case 4:
Delete();break;
case 5:
Modify();break;
case 0:
flag = 0;
system("cls");
break;
}
}
else{
printf("error!please enter again!\n");
}
}
fp = fopen("zzxx.txt","wb");
Save();
fclose(fp); system("cls");
printf("\n\n\n\t\t\tany key to exit!\n");
getch();
}
void InitDialBook(void)/*初始化文件函数*/
{ if(!(p=(struct addbook *)malloc(LEN))){printf("malloc error!\n");getchar();}
p->record = 1;
printf("\n\t\t\tname:");
gets(p->name);
printf("\n\t\t\ttelephone:");
gets(p->phone);
printf("\n\t\t\taddress:");
gets(p->addr);
printf("\n\t\t\tunits:");
gets(p->units);
printf("\n\t\t\tage:");
gets(p->age);
printf("\n\t\t\tremark:");
gets(p->remark);
p->link = NULL;
fwrite(p,LEN,1,fp);
fclose(fp);
}
void UserInterface(void)/*主菜单*/
{ system("cls");
printf("\n\n ************please select************\n");
printf(" +-----------------------------------+\n");
printf(" | 1 add information |\n");
printf(" | 2 find |\n");
printf(" | 3 show all the information |\n");
printf(" | 4 delete information |\n");
printf(" | 5 mend information |\n");
printf(" | 0 exit and save |\n");
printf(" +-----------------------------------+\n");
printf("\n\n notice!!enter number must under the inglish statement !!\n\n");
}
void Load(void)/*载入文件*/
{ if(!(head=(struct addbook *)malloc(LEN))) {printf("malloc error!\n");getchar();}
if((fp = fopen("zzxx.txt","rb"))==NULL){printf("load falure !\n");getchar(); exit(1);}
fread(head,LEN,1,fp);
p = head;
while(p->link!=NULL){
if(!(p1 = (struct addbook *)malloc(LEN))){printf("malloc error!\n");getchar();}
p->link=p1;
fread(p1,LEN,1,fp);
p = p1;
}
fclose(fp);
}
void Add(void)/*输入函数*/
{
if(!(p1 = (struct addbook *)malloc(LEN))){printf("malloc error!\n");getchar();}
p1->record = p->record + 1;system("cls");
printf("\n\n\t -------------------------------------------\n");
printf("\t\tplease enter information\n");
printf("\t\ -------------------------------------------\n");
printf("\n\tthis is %d menber",p->record+1);getchar(); /*getchar()用来吃回车符,不加的话直接让输telephone!*/
printf("\n\tname:");
gets(p1->name);
printf("\n\ttelephone:");
gets(p1->phone);
printf("\n\taddress:");
gets(p1->addr);
printf("\n\tunits:");
gets(p1->units);
printf("\n\tage:");
gets(p1->age);
printf("\n\tremark:");
gets(p1->remark);
p1->link = NULL;
p->link = p1;
p = p1;
printf("\ndo you want to continue to add?(y/n):");
if(getchar()=='y')Add();
}
void ShowAll(void)/*显示所有函数*/
{struct addbook *k,*w,*f,*j,*m;int i,q,t=0;
printf("head=%o\n",head);getchar();
system("cls");printf("\n\nwhat sequence to show the information?\n\naccording to record(0) or address(1)?\n\n");
printf("\n ******************** \n"); /*显示方式:按顺序显示(0)或按城市(地址)(1)显示*/
printf("\n 0. record\n");
printf("\n 1. address\n");
printf("\n ******************** \n");
printf("\nenter your choice (0-1):");
scanf("%d",&q); getchar();
switch(q)
{
case 0:system("cls");printf("t=%d\n",t);getchar();
for(i=0,f=head;f!=NULL;j=w,f=w->link)/*用选择发给每个成员按xuhao从小到大排序!*/
{for(k=f;k->link!=NULL;k=k->link)
if(f->record<=k->link->record){f=k->link;j=k;t++;}
if(f==head)i++;
else {m=f;j->link=m->link;m->link=head;head=m; i++; }
if(i==1)w=f;
}
s=head;/*起初我把这一句放在排序前头,总是出错,原因就是我排序后,head值已经变了,在按以前的输出肯定不行*/
printf("\n\n**************************************************************************\n");
printf("xuhao name telephone address units age remark");
printf("\n**************************************************************************\n");
do{
printf("%-5d %-13s%-13s %-13s%-10s%-3s %11s\n",s->record,s->name,s->phone,
s->addr,s->units,s->age,s->remark);
s = s->link;
}while(s!=NULL);
printf("\n\n\n\t\t\tany key to exit!");
getch();
break;
case 1: system("cls"); printf("t=%d\n",t);getchar();
for(i=0,f=head;f!=NULL;j=w,f=w->link)/*用选择发给每个成员按城市首字母的先后顺 序排序!*/
{for(k=f;k->link!=NULL;k=k->link)
if(strcmp(f->addr,k->link->addr)<=0){f=k->link;j=k;t++;}
if(f==head)i++;
else {m=f;j->link=m->link;m->link=head;head=m; i++; }
if(i==1)w=f;
} s=head;
printf("\n\n**************************************************************************\n");
printf("xuhao name telephone address units age remark");
printf("\n**************************************************************************\n");
do{
printf("%-5d %-13s%-13s %-13s%-10s%-3s %11s\n",s->record,s->name,s->phone,
s->addr,s->units,s->age,s->remark);s=s->link;
}while(s!=NULL);
printf("\n\n\n\t\t\tany key to exit!");
getchar();
break;
}
}
int Find(void)/*查找函数*/
{
char c;int d;
struct addbook who; system("cls");
printf("\n\n with which manners to find ?name(0),address(1).\n");
printf("\n ******************** \n"); /*查找方式:按姓名(0)查找或按城市即地址(1)查找*/
printf("\n 0. name\n");
printf("\n 1. address\n");
printf("\n ******************** \n");
printf("\n enter your choice!(0-1):");
s = head; scanf("%d",&d);
getchar();/*getchar()真重要!还是用来吃回车符!没他真不行!*/
switch(d)
{
case 0:
system("cls");
printf("\n\t\tname:");
gets(who.name);
while((s->link!=NULL)&&(strcmp(s->name,who.name)))
{
s = s->link;
}if((s->link==NULL)&&(strcmp(s->name,who.name)))
{
printf("\n%s not exist!\n",who.name);
printf("please check the name before finding.\n");
getch();
return 0;/* 如果找不到就返回0 */
} break;
case 1:
system("cls");
printf("\n\t\taddress:");
gets(who.addr);
while((s->link!=NULL)&&(strcmp(s->addr,who.addr)))
{
s = s->link;
}if((s->link==NULL)&&(strcmp(s->addr,who.addr)))
{
printf("\n%s not exist!\n",who.addr);
printf("please check the name before finding.\n");
getch();
return 0;/* 如果找不到就返回0 */
}break;
}
system("cls");
printf("\ninformation:\n");
printf("\n\n**************************************************************************\n");
printf("xuhao name telephone address units age remark");
printf("\n**************************************************************************\n");
printf("%-5d %-13s%-13s %-13s%-10s%-3s %11s\n",s->record,s->name,s->phone,
s->addr,s->units,s->age,s->remark);
printf("\n\nany key to continue!\n\n");
getchar();return 1;/*如果找到了就返回1*/
}
void Modify(void) /*修改信息*/
{ char ch,c; system("cls");
printf("\n who do you want to mend?please find it firstly!\n\n");
if(Find())
{ bf: printf("\n\n which item do you want to mend?\n name(n),telephone(t),address(a),units(u),age(A),remark(r).");
printf("\n\n enter your choice(n/t/a/u/A/r):"); /*选择所需修改的项目:名字(n),电话(t),
地址(a),单位(u),年龄(A),备注(r)*/
ch=getchar();getchar();
switch(ch)
{ case 'n':printf("\nname :");gets(s->name);break;
case 't':printf("\ntelephone :");gets(s->phone);break;
case 'a': printf("\naddress :");gets(s->addr); break;
case 'u':printf("\nunits :");gets(s->units);break;
case 'A': printf("\nage :"); gets(s->age);break;
case 'r': printf("\nremark :");gets(s->remark);break;
default: printf("\n\t\tcheck your choice! enter again!\n");break;
}
printf("\ndo you want to continue to mend?(y/n)");
printf("\nenter your choice (y/n):");
c=getchar();getchar();if(c=='y'){ system("cls");goto bf;}
}
}
void Delete(void)/*删除函数*/
{
int find_prev;
int find_record;
char del;
char del_member_name[15];
struct addbook *p_del_prev;
printf("\n please find the menber you want to delete firstly!\n\n");/*先找到要删的对象,然后再删!*/
if(Find())/*如果Find()的值为假就回到主菜单!*/
{
strcpy(del_member_name,s->name);
printf("make sure to delete %s ?(Y/N)?:",del_member_name);
scanf("%c",&del);
if(del=='n'||del=='N') return;
find_prev = s->record - 1;
find_record = 1;
p_del_prev = head;
while((p_del_prev->link!=NULL)&&(find_record<find_prev))
{
p_del_prev = p_del_prev->link;
find_record++;
}
/* 如果删除第一位成员 */
if(s->record==1)
{
if(s->link!=NULL)
{
head = head->link;
s = head;
while(s!=NULL)
{
s->record = s->record - 1;
s = s->link;
}
printf("%d,%s",p->record,p->name);
}
else if(s->link==NULL){ /* 如果第一位成员是最后一位 */
p = NULL;
head = p;
if(remove("zzxx.txt")==0){
printf("have emptyed!\n");
printf("create again!\n");
printf("open process to create again .\n");
}
printf("\n\n\t\tanykey to exit.\n");
getch();
exit(0);
}
}
/* 如果删除最后一位成员 */
else if(s->record==p->record){
p_del_prev->link = NULL;
s->link = NULL;
p = p_del_prev;
}
else{
p_del_prev->link = s->link;
s->link = NULL;
while(p_del_prev->link!=NULL)
{
p_del_prev = p_del_prev->link;
p_del_prev->record = p_del_prev->record -1;
}
printf("%d\n%d\n",p->record,p->link);
}
printf("menber \"%s\" have been deleted.\n ",del_member_name);
getch();printf("Do you want to contine to delete record ?(y/n)");
if(getchar()=='y'||getchar()=='Y')Delete();}
}
void Save(void)/*保存文件*/
{
s = head;
do{
fwrite(s,LEN,1,fp);
s = s->link;
}while(s!=NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -