|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
struct student
|
|
|
{
|
|
|
char num[20];char name[20];char id[10];char sex[4];int age;char tele[15];
|
|
|
}student[500],student1;
|
|
|
|
|
|
void menu();
|
|
|
void type_in();
|
|
|
void baocun(int);
|
|
|
void scan();
|
|
|
void del();
|
|
|
void search();
|
|
|
void search_name();
|
|
|
void change();
|
|
|
void add();
|
|
|
|
|
|
void menu()
|
|
|
{
|
|
|
printf("\n");
|
|
|
printf(" 1.录入学生信息\n");
|
|
|
printf(" 2.浏览学生信息\n");
|
|
|
printf(" 3.查询学生信息\n");
|
|
|
printf(" 4.删除学生信息\n");
|
|
|
printf(" 5.修改学生信息\n");
|
|
|
printf(" 6.添加学生信息\n");
|
|
|
printf(" 7.退出\n");
|
|
|
printf("\n");
|
|
|
|
|
|
}
|
|
|
/********************************************************************************************/
|
|
|
int main()
|
|
|
{
|
|
|
int n,f;
|
|
|
while(1)
|
|
|
{
|
|
|
do
|
|
|
{
|
|
|
menu();
|
|
|
printf("请输入你需要操作的序号(1-7):\n");
|
|
|
scanf("%d",&n);
|
|
|
if(n>=1&&n<=7)
|
|
|
{
|
|
|
f=1;
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
f=0;
|
|
|
printf("无效操作!");
|
|
|
}
|
|
|
}while(f==0);
|
|
|
switch(n)
|
|
|
{
|
|
|
case 1:printf(" 录入学生信息\n\n"); type_in(); break;
|
|
|
case 2:printf(" 浏览学生信息\n\n"); system("cls"); scan(); break;
|
|
|
case 3:printf(" 查询学生信息\n\n"); system("cls"); search(); break;
|
|
|
case 4:printf(" 删除学生信息\n\n"); system("cls"); del(); break;
|
|
|
case 5:printf(" 修改学生信息\n\n"); system("cls"); change(); break;
|
|
|
case 6:printf(" 添加学生信息\n\n"); system("sls"); add(); break;
|
|
|
case 7:goto loop;
|
|
|
}
|
|
|
}
|
|
|
loop:
|
|
|
system("cls");
|
|
|
return 0;
|
|
|
}
|
|
|
/***********************************************************************************/
|
|
|
void type_in()
|
|
|
{
|
|
|
int i,m;
|
|
|
printf("请输入需要创建信息的学生人数(1--500):\n");
|
|
|
scanf("%d",&m);
|
|
|
for (i=0;i<m;i++)
|
|
|
{
|
|
|
printf("请输入学号: ");
|
|
|
scanf("%s",student[i].num);
|
|
|
printf("请输入姓名: ");
|
|
|
scanf("%s",student[i].name);
|
|
|
printf("请输入年龄: ");
|
|
|
scanf("%d",&student[i].age);
|
|
|
printf("请输入电话: ");
|
|
|
scanf("%s",student[i].tele);
|
|
|
printf("请输入身份证号码: ");
|
|
|
scanf("%s",&student[i].id);
|
|
|
printf("请输入性别: ");
|
|
|
scanf("%s",&student[i].sex);
|
|
|
getchar();system("cls");
|
|
|
printf("\n");
|
|
|
}
|
|
|
baocun(m);
|
|
|
}
|
|
|
|
|
|
void baocun(int m)
|
|
|
{
|
|
|
int i;
|
|
|
|
|
|
FILE *fp;
|
|
|
if ((fp=fopen("D:\\student_list.txt","wb"))==NULL)
|
|
|
{
|
|
|
printf ("此文件不能打开\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
for (i=0;i<m;i++)
|
|
|
if (fwrite(&student[i],sizeof(struct student),1,fp)!=1)
|
|
|
printf("file write error\n");
|
|
|
fclose(fp);
|
|
|
}
|
|
|
|
|
|
int fun()
|
|
|
{
|
|
|
FILE*fp;
|
|
|
int i=0;
|
|
|
if((fp=fopen("D:\\student_list.txt","rb"))==NULL)
|
|
|
{
|
|
|
printf ("cannot open file\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
do
|
|
|
{
|
|
|
fread(&student[i],sizeof(struct student),1,fp);
|
|
|
i++;
|
|
|
}
|
|
|
while(feof(fp)==0);
|
|
|
}
|
|
|
fclose(fp);
|
|
|
return(i-1);
|
|
|
}
|
|
|
void scan()
|
|
|
{
|
|
|
int i;
|
|
|
int m=fun();
|
|
|
printf("\n学号 \t姓名 \t年龄 \t电话 \t身份证 \t性别 \n");
|
|
|
for(i=0;i<m;i++)
|
|
|
printf("%s \t%s \t%d \t%s \t%s \t%s \n",student[i].num,student[i].name,student[i].age,student[i].tele,student[i].id,student[i].sex);
|
|
|
printf("按任意键继续\n");
|
|
|
getchar();
|
|
|
getchar();system("cls");
|
|
|
}
|
|
|
|
|
|
void del()
|
|
|
{
|
|
|
|
|
|
int m=fun();
|
|
|
int i,j,n,t,f;
|
|
|
char name1[10];
|
|
|
printf("\n 原来的学生信息:\n");
|
|
|
scan();
|
|
|
printf("\n");
|
|
|
printf("请输入要删除的学生的姓名:\n");
|
|
|
scanf("%s",name1);
|
|
|
for(f=1,i=0;f&&i<m;i++)
|
|
|
{
|
|
|
if(strcmp(student[i].name,name1)==0)
|
|
|
{
|
|
|
printf("\n已找到此人,原始记录为:\n");
|
|
|
printf("\n学号 \t姓名 \t年龄 \t电话 \t身份证 \t性别 \n");
|
|
|
printf("%s \t%s \t%d \t%s \t%s \t%s \n",student[i].num,student[i].name,student[i].age,student[i].tele,student[i].id,student[i].sex);
|
|
|
printf("\n确实要删除此人信息请按1,不删除请按0\n");
|
|
|
scanf("%d",&n);
|
|
|
if(n==1)
|
|
|
{
|
|
|
for(j=i;j<m-1;j++)
|
|
|
{
|
|
|
student[j]=student[j+1];
|
|
|
}
|
|
|
f=0;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if(!f)
|
|
|
m=m-1;
|
|
|
else
|
|
|
printf("\n查无此人!\n");
|
|
|
printf("\n 浏览删除后的所有学生信息:\n");
|
|
|
baocun(m);
|
|
|
scan();
|
|
|
printf("\n继续删除请按1,不再删除请按0\n");
|
|
|
scanf("%d",&t);
|
|
|
switch(t)
|
|
|
{
|
|
|
case 1:del();break;
|
|
|
case 0:break;
|
|
|
default :break;
|
|
|
}system("cls");
|
|
|
}
|
|
|
void search()
|
|
|
{
|
|
|
int t,f;
|
|
|
do
|
|
|
{
|
|
|
printf("\n按姓名查询请按1; 进入主函数按2\n");
|
|
|
scanf("%d",&t);
|
|
|
if(t>=1&&t<=2)
|
|
|
{
|
|
|
f=1;
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
f=0;
|
|
|
printf("您输入有误,请重新选择!");
|
|
|
}
|
|
|
}while(f==0);system("cls");
|
|
|
while(f==1)
|
|
|
{
|
|
|
switch(t)
|
|
|
{
|
|
|
case 1:printf("按姓名查询\n");search_name();break;
|
|
|
case 2:main();break;
|
|
|
default:break;
|
|
|
}
|
|
|
system("cls");
|
|
|
}
|
|
|
}
|
|
|
/***********************************************************************************/
|
|
|
void search_name()/*按姓名查找函数*/
|
|
|
{
|
|
|
char name1[10];
|
|
|
int i,t;
|
|
|
int m=fun();
|
|
|
printf("请输入要查找的姓名:\n");
|
|
|
scanf("%s",name1);
|
|
|
for(i=0;i<m;i++)
|
|
|
if(strcmp(name1,student[i].name)==0)
|
|
|
{
|
|
|
printf("\n已找到此人,其记录为:\n");
|
|
|
printf("\n学号 \t姓名 \t年龄 \t电话 \t身份证 \t性别 \n");
|
|
|
printf("%s \t%s \t%d \t%s \t%s \t%s \n",student[i].num,student[i].name,student[i].age,student[i].tele,student[i].id,student[i].sex);
|
|
|
break;
|
|
|
}
|
|
|
if(i==m)
|
|
|
printf("\n未找到\n");
|
|
|
printf("\n");
|
|
|
printf("返回查询函数请按1,继续查询姓名请按2\n");
|
|
|
scanf("%d",&t);
|
|
|
switch(t)
|
|
|
{
|
|
|
case 1:search();break;
|
|
|
case 2: break;
|
|
|
default:break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*************************************************************************************/
|
|
|
void search_num()
|
|
|
{
|
|
|
char num1[10];
|
|
|
int i,t;
|
|
|
int m=fun();
|
|
|
printf("请输入要查找的学号:\n");
|
|
|
scanf("%s",num1);
|
|
|
for(i=0;i<m;i++)
|
|
|
if(student[i].num==0)
|
|
|
{
|
|
|
printf("\n已找到此人,其记录为:\n");
|
|
|
printf("\n学号 \t姓名 \t年龄 \t电话 \t身份证 \t性别 \n");
|
|
|
printf("%s \t%s \t%d \t%s \t%s \t%s \n",student[i].num,student[i].name,student[i].age,student[i].tele,student[i].id,student[i].sex);
|
|
|
break;
|
|
|
}
|
|
|
if(i==m)
|
|
|
printf("\n未找到\n");
|
|
|
printf("\n");
|
|
|
printf("返回查询函数请按1,继续查询学号请按2\n");
|
|
|
scanf("%d",&t);
|
|
|
switch(t)
|
|
|
{
|
|
|
case 1:search();break;
|
|
|
case 2: break;
|
|
|
default:break;
|
|
|
}
|
|
|
}
|
|
|
/***************************************************************************************/
|
|
|
void change()
|
|
|
{
|
|
|
char num[20];
|
|
|
char name[20];
|
|
|
char id[10];
|
|
|
char sex[4];
|
|
|
long long int age;
|
|
|
char tele[15];
|
|
|
int b,c,i,n,t,f;
|
|
|
int m=fun();
|
|
|
printf("\n 原来的学生信息:\n");
|
|
|
scan();
|
|
|
printf("\n");
|
|
|
printf("请输入要修改的学生的姓名:\n");
|
|
|
scanf("%s",name);
|
|
|
for(f=1,i=0;f&&i<m;i++)
|
|
|
{
|
|
|
if(strcmp(student[i].name,name)==0)
|
|
|
{
|
|
|
printf("\n已找到此人,原始记录为:\n");
|
|
|
printf("\n学号 \t姓名 \t年龄 \t电话 \t身份证 \t性别 \n");
|
|
|
printf("%s \t%s \t%d \t%s \t%s \t%s \n",student[i].num,student[i].name,student[i].age,student[i].tele,student[i].id,student[i].sex);
|
|
|
printf("\n确实要修改此学生信息请按1 ; 不修改请按0\n");
|
|
|
scanf("%d",&n);
|
|
|
if(n==1)
|
|
|
{
|
|
|
printf("\n需要进行修改的选项\n 1.学号 2.姓名 3.年龄 4.电话 5.身份证 6.性别\n");
|
|
|
printf("请输入你想修改的那一项序号:\n");
|
|
|
scanf("%d",&c);
|
|
|
if(c>6||c<1)
|
|
|
printf("\n选择错误,请重新选择!\n");
|
|
|
}
|
|
|
f=0;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
if(f==1)
|
|
|
printf("\n未找到\n");
|
|
|
do
|
|
|
{
|
|
|
switch(c)
|
|
|
{
|
|
|
case 1:printf("学号改为: ");
|
|
|
scanf("%s",num);
|
|
|
strcpy(student[i-1].num,num);
|
|
|
break;
|
|
|
case 2:printf("姓名改为: ");
|
|
|
scanf("%s",name);
|
|
|
strcpy(student[i-1].name,name);
|
|
|
break;
|
|
|
case 3:printf("年龄改为: ");
|
|
|
scanf("%lld",&age);
|
|
|
student[i-1].age=age;
|
|
|
break;
|
|
|
case 4:printf("电话改为: ");
|
|
|
scanf("%s",tele);
|
|
|
strcpy(student[i-1].tele,tele);
|
|
|
break;
|
|
|
case 5:printf("身份证号改为: ");
|
|
|
scanf("%s",id);
|
|
|
strcpy(student[i-1].id,id);
|
|
|
break;
|
|
|
case 6:printf("性别改为: ");
|
|
|
scanf("%s",sex);
|
|
|
strcpy(student[i-1].sex,sex);
|
|
|
break;
|
|
|
}
|
|
|
printf("\n");
|
|
|
printf("\n是否确定所修改的信息?\n 是 请按1 ; \n不,重新修改 请按2: \n");
|
|
|
scanf("%d",&b);
|
|
|
}
|
|
|
while(b==2);
|
|
|
printf("\n浏览修改后的所有学生信息:\n");
|
|
|
printf("\n");
|
|
|
baocun(m);
|
|
|
scan();
|
|
|
printf("\n继续修改请按1,不再修改请按0\n");
|
|
|
scanf("%d",&t);
|
|
|
switch(t)
|
|
|
{
|
|
|
case 1:change();break;
|
|
|
case 0:break;
|
|
|
default :break;
|
|
|
}system("cls");
|
|
|
}
|
|
|
void add()
|
|
|
{
|
|
|
int n=fun();
|
|
|
int i,m;
|
|
|
printf("请输入需要创建信息的学生人数(1--500):\n");
|
|
|
scanf("%d",&m);
|
|
|
for (i=n;i<m+n;i++)
|
|
|
{
|
|
|
printf("请输入学号: ");
|
|
|
scanf("%s",student[i].num);
|
|
|
printf("\n");
|
|
|
printf("请输入姓名: ");
|
|
|
scanf("%s",student[i].name);
|
|
|
getchar();
|
|
|
printf("请输入年龄: ");
|
|
|
scanf("%lld",&student[i].age);
|
|
|
printf("请输入电话: ");
|
|
|
scanf("%s",student[i].tele);
|
|
|
printf("请输入身份证: ");
|
|
|
scanf("%s",student[i].id);
|
|
|
printf("请输入性别: ");
|
|
|
scanf("%s",student[i].sex);
|
|
|
getchar();system("cls");
|
|
|
printf("\n");
|
|
|
}
|
|
|
baocun(m+n);system("cls");
|
|
|
printf("添加学生档案完成。\n这时库存信息为:\n");
|
|
|
scan();
|
|
|
}
|
|
|
|