#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define len sizeof(struct student)
#define file_data_path "student.txt"
struct student
{
long int num;
char name[20];
int age;
char sex[10];
char birthday[10];
char address[30];
long int tele_num;
struct student*next;
};
int total_num=0;
struct student*head=NULL;
void welcome();
void mainmenu();
void record();
void insertlink(struct student*stu);
void insertlink1();
void display(struct student*stu);
void displayall();
void query();
void locate_num();
void locate_name();
void readdata();
void writedate();
void dellink();
void change();
void devise(struct student*p);
int main()
{
char username[9];
char userpwd[7];
int i;
welcome();
for(i=0;i<3;i++)
{
printf("\n管理员初始用户名与密码为123\n");
printf("请输入您的用户名");
scanf("%s",username);
printf("\n请输入您的密码\n") ;
scanf("%s",userpwd);
if((strcmp(username,"123")==0)&&(strcmp(userpwd,"123")==0))
{
mainmenu();
break;
}
else
{
if(i<3)
{
printf("用户名或密码错误,请重新输入!");
}
else
{
printf("您已经连续三次输入错误,请明天再次输入");
}
}
}
return 0;
}
void welcome()
{
printf("\t\t\t+--------------------------+\n");
printf("\t\t\t||\n");
printf("\t\t\t|欢迎使用学生信息管理系统|\n");
printf("\t\t\t||\n");
printf("\t\t\t+--------------------------+\n");
printf("\n\n\n\n\n\n\n\n");
printf("\t\t\t本程序为线性表");
}
void mainmenu()
{
int choice;
choice=-1;
readdata();
printf("\n温馨提示:为保证您的操作得到保存,请按照正常顺序退出程序^_^\n");
do{
printf("\n\n\n");
printf("\t\t\t-------------------------- —\n");
printf("\t\t\t|学生信息管理系统|\n");
printf("\t\t\t-------------------------- —\n");
printf("\t\t\t|[1]---录入学生信息|\n");
printf("\t\t\t|[2]---浏览学生信息|\n");
printf("\t\t\t|[3]---查询学生信息|\n");
printf("\t\t\t|[4]---删除学生信息|\n");
printf("\t\t\t|[5]---修改学生信息|\n");
printf("\t\t\t|[6]---插入学生信息|\n");
printf("\t\t\t|[0]---退出系统|\n");
printf("\t\t\t*·*·*·*·*·*·*·*·*·*·*·*·*·*·*·*·") ;
printf("\t\t\t-------------------------- —\n");
printf("请输入你的选择");
scanf("%d",&choice);
switch(choice)
{
case 0:
writedate();
exit(0);
case 1:
record();
break;
case 2:
displayall();
break;
case 3:
query();
break;
case 4:
dellink();
break;
case 5:
change();
break;
case 6:
insertlink1();
break;
default:
printf("\n无效选项!");
break;
}
}
while(choice!=0);
}
void record()
{
struct student*p0;
p0=(struct student* )malloc(len) ;
printf("请输入学生学号:");
scanf("%ld",&p0->num);
printf("请输入学生姓名");
scanf("%ld",&p0->name);
printf("请输入学生年龄:");
scanf("%ld",&p0->sex);
printf("请输入学生性别:");
scanf("%ld",&p0->birthday);
printf("请输入学生出生日期:");
scanf("%ld",&p0->address);
printf("请输入学生地址:");
scanf("%ld",&p0->num);
printf("请输入学生电话:");
scanf("%ld",&p0->tele_num);
insertlink(p0);
printf("该学生的信息为:\n");
printf("--------------------------------------------------");
printf("学号\t姓名\t年龄\t性别\t出身日期\t地址\t电话\n");
display(p0);
}
void inserlink(struct student*stu)
{
struct student*p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
total_num++;
}
void inserlink1()
{
record();
}
void display(struct student*p)
{
printf("%ld\t%s\t%d\t%s\t%s\t%s\t%ld\n",p->num,p->name,p->age,p->sex,p->birthday,p->address,p->tele_num);
//查看学生信息
}
void displayall()
{
struct student*p;
p=head;
if(head!=NULL)
{
printf("\n学号\t姓名\t年龄\t性别\t出身日期\t地址\t电话\n");
printf("--------------------------------------------------");
do
{
display(p);
p=p->next;
}
while(p!=NULL);
}
printf("\n");
}
void query()
{
int choice;
choice=-1;
do
{
printf("\n");
printf("+---------------—+\n");
printf("|[1]---按学号查询|\n");
printf("|[2]---按姓名查询|\n");
printf("|[0]---取消|\n");
printf("+---------------—+\n");
printf("请输入您的选择:");
scanf("%d",&choice);
switch(choice)
{
case 0:
return;
case 1:
locate_num();
break;
case 2:
locate_name() ;
break;
default:
printf("\n无效选项");
break;
}
}
while(choice!=0);
}
void locate_num()
{
int num;
struct student*p1;
printf("请输入学生学号:");
scanf("%ld",&num);
if(head==NULL)
{
printf("无学生记录!");
return;
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
p1=p1->next;
if(num==p1->num){
printf("\n学号\t姓名\t年龄\t性别\t出身日期\t地址\t电话\n");
printf("--------------------------------------------------");
display(p1);
}
else
printf("没有该学生的记录,请核对");
}
void locate_name()
{
char name[20];
struct student*p1;
printf("请输入学生姓名:");
scanf("&s",name);
if(head==NULL)
{
printf("无学生记录\n");
return;
}
p1=head;
while(strcmp(name,p1->name)&&p1->next!=NULL)
p1=p1->next;
if(!strcmp(name,p1->name))
{
printf("\n学号\t姓名\t年龄\t性别\t出身日期\t地址\t电话\n");
printf("--------------------------------------------------");
display(p1);
}
else
printf("没有该学生记录,请核对!");
}
void writedate()
{
FILE*fp;
struct student*p;
fp=fopen(file_data_path,"w");
if(! fp)
{
printf("文件打开错误");
return;
}
fprintf(fp,"%d\n",total_num);
for(p=head;p!=NULL;p=p->next)
{
fprintf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\n",p->num,p->name,p->age,p->sex,p->birthday,p->address,p->tele_num);
}
fclose(fp);
}
void readdate()
{
FILE*fp;
struct student*p1,*p2;
fp=fopen(file_data_path,"r");
if(!fp)
{
printf("文件打开错");
return;
}
fscanf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\n",&p1->num,p1->name,p1->age,p1->sex,p1->birthday,p1->address,&p1->tele_num);
while(!feof(fp))
{
p1=(struct student*)malloc(len);
fscanf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\n",&p1->num,p1->name,p1->age,p1->sex,p1->birthday,p1->address,&p1->tele_num);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
fclose(fp);
}
void dellink()
{
struct student*p1,*p2;
long int num;
if(head==NULL)
{
printf("无学生记录!\n");
return;
}
printf("请输入您要删除的学生学号:");
scanf("%ld",&num);
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
free(p1);
total_num--;
}
else
printf("没有该学生的记录,请核对\n");
}
void change()
{
struct student*p1,*p2;
long int num;
if(head==NULL)
{
printf("无学生记录!\n");
return;
}
printf("请输入您要修改的学生学号:\n");
scanf("%ld",&num);
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
devise(p1);
else
printf("没有该学生记录,请核对!\n");
}
void devise(struct student*p)
{
int choice;
choice=-1;
do
{
printf("请输入您要修改的学生的信息内容:\n");
printf("+----------------------+");
printf("|姓名 请按1|\n");
printf("|年龄 请按2|\n");
printf("|性别 请按3|\n");
printf("|生日 请按4|\n");
printf("|地址 请按5|\n");
printf("|电话 请按6|\n");
printf("|取消 请按0|\n");
printf("+----------------------+");
printf("请输入您的选择:");
scanf("%d",&choice);
switch(choice)
{
case 0:
return;
case 1:
printf("请输入要修改的姓名:");
scanf("%s",p->name);
break;
case 2:
printf("请输入要修改的年龄:");
scanf("%d",&p->age);
break;
case 3:
printf("请输入要修改的性别:");
scanf("%s",p->sex);
break;
case 4:
printf("请输入要修改的生日:");
scanf("%s",p->birthday);
break;
case 5:
printf("请输入要修改的地址:");
scanf("%s",p->address);
break;
case 6:
printf("请输入要修改的电话:");
scanf("%ld",&p->tele_num);
break;
default:
printf("\n无效选项!");
break;
}
}
while(choice!=0);
}