📄 zhide.cpp
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int i;/*定义全局变量i,主要控制学生的数量*/
struct student
{
long int num;/*学号*/
char name[20];/*名字*/
char sex;/*性别*/
char xibie[40];/*系别*/
char zhuanye[30];/*专业*/
int day;/*生日的day*/
int month;/*生日的月*/
int year;/*生日的年*/
int il;/*文件中学生的个数*/
}stu[200];/*定义一个结构数组最大为200*/
void shuru(int r)/*定义个一个函数,用此函数从键盘读入学生的信息*/
{
char j;
i=r;
for(;i<200;++i)/*应用for循环从键盘读入个个学生的信息*/
{
if(i==0||i==r)/*从别的函数中得到变量r,看r是否为第一个*/
printf("please input the first one:\n");
else
{
printf("do you want to input another?y/n\n");
scanf("%c",&j);
if(j!='y'&&j!='Y')
break;
}
stu[i].il=i;
printf("please input the student's number:\n");
scanf("%ld",&stu[i].num);
printf("please input the student's name:\n");
scanf("%s",stu[i].name);
getchar();
printf("please input the student's sex:f/m?\n");
scanf("%c",&stu[i].sex);
printf("please input the student's department:\n");
scanf("%s",stu[i].xibie);
printf("please input the student's profession:\n");
scanf("%s",stu[i].zhuanye);
printf("the year of the student's birthday:\n");
scanf("%d",&stu[i].year);
printf("the month:\n");
scanf("%d",&stu[i].month);
printf("the day:\n");
scanf("%d",&stu[i].day);
getchar();
}
}
void menu()/*菜但函数*/
{
printf("\n0.new\n");
printf("1.open\n");
printf("2.input new students' information\n");
printf("3.search one student\n");
printf("4.modify one student's information\n");
printf("5.delete one student's information\n");
printf("6.save\n");
printf("7.quit\n");
}
void if_break()
{
printf("the number you input is error!");
}
void zprintfzhd()/*打开目标文件后,把文件中的内容读入数组结构,并把il的值赋给全局变量i*/
{
int k=0;
int f;
FILE*fp;
getchar();
char dir[40];
printf("please input the directories:\n");/*输入目标文件的路径*/
gets(dir);
if((fp=fopen(dir,"rb"))==NULL)
{
printf("don't have old file!");
}
do
{ fread(&stu[k],sizeof(struct student),1,fp);
k++;
} while(!feof(fp));
i=stu[k-2].il+1;
fclose(fp);
}
void printfzhd()/*利用for循环打印出所有学生的信息*/
{
int k=0;
struct student *p;
p=&stu[0];
printf("number:\t\t");
printf("name:\t");
printf("sex:\t");
printf("department:\t");
printf("profession:\t");
printf("birthday:\n");
for(;k<i;k++)
{
printf("%ld\t",(p+k)->num);
printf("%s\t",(p+k)->name);
printf("%c\t",(p+k)->sex);
printf("%s\t",(p+k)->xibie);
printf("%s\t",(p+k)->zhuanye);
printf("%d",(p+k)->year);
printf(".");
printf("%d",(p+k)->month);
printf(".");
printf("%d\n",(p+k)->day);
}
}
void snumberzhd()/*根据从键盘输入的学生的名字,查找其对应学生的信息*/
{
long int a;
int b,q;
struct student *p;
p=&stu[0];
q=1;
printf("\nplease input the student's number:\n");
scanf("%ld",&a);
for(b=0;b<i;b++,p++)
{
if(a==p->num)
{
printf("number:%ld\n",p->num);
printf("name:%s\n",p->name);
printf("sex:%c\n",p->sex);
printf("department:%s\n",p->xibie);
printf("profession:%s\n",p->zhuanye);
printf("birthday:%d.%d.%d\n",p->year,p->month,p->day);
q=0;
break;
}
}
if(q)
{
printf("the number you input can't find in the file!\n");
}
}
void snamezhd()/*得到从键盘上输入的名字,并查找其对应的学生信息*/
{
char name1[20];
int q,k;
struct student *p;
p=&stu[0];
q=1;
printf("please input the student's name:\n");
gets(name1);
for(k=0;k<i;k++,p++)
{
if(strcmp(name1,p->name)==0)
{
printf("number:%ld\n",p->num);
printf("name:%s\n",p->name);
printf("sex:%c\n",p->sex);
printf("department:%s\n",p->xibie);
printf("profession:%s\n",p->zhuanye);
printf("birthday:%d.%d.%d\n",p->year,p->month,p->day);
q=0;
break;
}
}
if(q)
{
printf("the name you input can't find in the file!\n");
}
}
void newzhd()/*想要建立一个新的文件,从新开始的输入学生的信息*/
{
printf("please input with tishi!\n and the number of all the students must less than 200!\n");
shuru(0);
printfzhd();
}
void openzhd()
{
zprintfzhd();
printfzhd();
}
void inputzhd()/*输入新学生信息的函数*/
{
shuru(i);
}
void searchzhd()/*查找函数*/
{
int j;
printf("please choose the way which you want to use to search the student.\n name is 1,number is 2\n 1 or 2?");
scanf("%d",&j);/*根据选择确定要查找的方式*/
getchar();
if(j!=1&&j!=2)
{
printf("the number you choose is error!\n");
exit(0);
}
else if(j==1)
snamezhd();
else
snumberzhd();
}
void modifyzhd()/*修改函数*/
{
char name1[20];
int q,k;
struct student *p;
p=&stu[0];
q=1;
getchar();
printf("please input the student's name:\n");
gets(name1);
for(k=0;k<i;k++,p++)/*根据从键盘上读入的名字,找到其对应数组变量*/
{
if(strcmp(name1,p->name)==0)
{
printf("number:%ld\n",p->num);
printf("name:%s\n",p->name);
printf("sex:%c\n",p->sex);
printf("department:%s\n",p->xibie);
printf("profession:%s\n",p->zhuanye);
printf("birthday:%d.%d.%d\n",p->year,p->month,p->day);
q=0;
stu[i].il=k;/*输入新的信息代替原来的信息,执行完这步,最好储存,因为这里只是数组结构中变了,文件中并没有改变*/
printf("new number:\n");
scanf("%ld",&stu[k].num);
printf("new name:\n");
scanf("%s",stu[k].name);
getchar();
printf("new sex:\n");
scanf("%c",&stu[k].sex);
printf("new department:\n");
scanf("%s",stu[k].xibie);
printf("new profession:\n");
scanf("%s",stu[k].zhuanye);
printf("new year of the birthday:\n");
scanf("%d",&stu[k].year);
printf("new month:\n");
scanf("%d",&stu[k].month);
printf("new day:\n");
scanf("%d",&stu[k].day);
break;
}
}
if(q)
{
printf("the name you input can't find in the file!\n");
}
}
void deletezhd()/*删除函数*/
{
char name1[20];
int q,k,l;
struct student *p;
p=&stu[0];
q=1;
getchar();
printf("please input the student's name:\n");
gets(name1);
for(k=0;k<i;k++,p++)/*根据从键盘上读入的名字找到其对应的结构数组变量*/
{
if(strcmp(name1,p->name)==0)
{
printf("number:%ld\n",p->num);
printf("name:%s\n",p->name);
printf("sex:%c\n",p->sex);
printf("department:%s\n",p->xibie);
printf("profession:%s\n",p->zhuanye);
printf("birthday:%d.%d.%d\n",p->year,p->month,p->day);
q=0;
for(l=k;l<i-1;l++)/*把其后面的数组结构变量一组一组的向前覆盖,从而得到删除的效果*/
{
stu[l].num=stu[l+1].num;
strcpy(stu[l].name,stu[l+1].name);
stu[l].sex=stu[l+1].sex;
strcpy(stu[l].xibie,stu[l+1].xibie);
strcpy(stu[l].zhuanye,stu[l+1].zhuanye);
stu[l].year=stu[l+1].year;
stu[l].month=stu[l+1].month;
stu[l].day=stu[l+1].day;
}
break;
}
}
printf("the order is over!");
i=i-1;
if(q)
{
printf("the name you input can't find in the file!\n");
}
}
void savezhd()/*储存函数,把结构数组中的变量一每个数组为单位,储存在指定文件中*/
{
int k;
FILE *fp;
char dir[40];
getchar();
printf("please input the directories:\n");
gets(dir);/*得到储存路径*/
if((fp=fopen(dir,"w+b"))==NULL)
{
printf("cannot open the file");
exit(1);
}
for(k=0;k<i;k++)
{
fwrite(&stu[k],sizeof(struct student),1,fp);
}
fclose(fp);
printf("save over!");
}
void quitzhd()/*退出函数,不执行任何命令*/
{}
void sswitch(int o)/*从main函数中得到想要的值,并执行对应的函数*/
{
if(o<0||o>8)
{
if_break();
}
switch(o)
{
case 0:newzhd(); break;
case 1:openzhd(); break;
case 2:inputzhd(); break;
case 3:searchzhd(); break;
case 4:modifyzhd(); break;
case 5:deletezhd(); break;
case 6:savezhd(); break;
case 7:quitzhd();
}
}
main()
{
int o;
do{
menu();
scanf("%d",&o);/*得到菜但选项,传递值给其他函数*/
sswitch(o);}while(o>=0&&o<7);
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -