





#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct _Stu
{
char num[20];//学号
char name[20];//姓名
char sex[20];//性别
char birth[20];//出生日期
char In_School[20];//入学日期
char Out_School[20];//毕业日期
}STU,*PSTU;
typedef struct _List
{
STU stu;
struct _List *next;
}List,*PList;
int checkDay(char * day);//不合法返回1
void Save_Info(PList stu);
void Read_Info(PList stu);
void Print_List(List node);
void Del_Info(PList node);
void sort(PList list);
void Printf_Stu_Info(STU stu);
int Main_Menu();
void Add_New_Info(PList node);
void Modify_Info(PList node);
void Search_Num(PList node);
void Search_Name(PList node);
void Search_Sex(PList node);
void Search_Birth(PList node);
void Search_Intime(PList node);
void Search_Outtime(PList node);
void Search_Menu(PList node)
{
int choice;
// system("cls");
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************************************\n");
printf("\t\t\t请输入你要进行的操作:");
scanf("%d",&choice);
while(choice<0||choice>6)
{
printf("\t\t\t*请重新选择:");
scanf("%d",&choice);
}
switch(choice)
{
case 1:
Search_Num(node);
break;
case 2:
Search_Name(node);
break;
case 3:
Search_Sex(node);
break;
case 4:
Search_Birth(node);
break;
case 5:
Search_Intime(node);
break;
case 6:
Search_Outtime(node);
break;
}
}
int main()
{
int choice;
List node;
node.next=NULL;
Read_Info(&node);
system("mode con: cols=100 lines=30");
do
{
choice=Main_Menu();
switch(choice)
{
case 1:
Add_New_Info(&node);
Save_Info(&node);
break;
case 2:
Del_Info(&node);
Save_Info(&node);
break;
case 3:
Modify_Info(&node);
break;
case 4:
Search_Menu(&node);
break;
case 5:
sort(node.next);
Print_List(node);
break;
}
}while(choice!=0);
return 0;
}
int Main_Menu()
{
int choice;
// system("cls");
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* 0:退出 *\n");
printf("\t\t\t************************************\n");
printf("\t\t\t请输入你要进行的操作:");
scanf("%d",&choice);
while(choice<0||choice>5)
{
printf("\t\t\t*请重新选择:");
scanf("%d",&choice);
}
return choice;
}
//检查日期
//不合法返回1
int checkDay(char * day)
{
int i = 0;
int year = 0;
int mouth = 0;
int d = 0;
//长度不合法
if (strlen(day) < 8||strlen(day) > 10)
{
return 1;
}//我们在这里设置日期的输入格式为xxxx-xx-xx
while (day[i] != '-'&&i < strlen(day))
{
if (day[i] < '0'||day[i] > '9')
{
return 1;
}
year *= 10;
year += day[i]-'0';
i++;
}
i++;
while (day[i] != '-' && i < strlen(day))
{
if (day[i] < '0'||day[i] > '9')
{
return 1;
}
mouth *= 10;
mouth += day[i]-'0';
i++;
}
i++;
while(day[i] != '\0' && i < strlen(day))
{
//是否含有除日母以外别的东西
if(day[i] < '0'||day[i] > '9')
{
return 1;
}
d *= 10;
d += day[i] - '0';
i++;
}
//printf("%d %d %d\n",year,mouth,d);
//月份合不合法
if (mouth <= 0||mouth > 12)
{
return 1;
}
//日期合不合法
if (d <= 0||d > 31)
{
return 1;
}
if (mouth == 2||mouth == 4||mouth == 6||mouth == 9||mouth == 11)
{
if (d>30)
{
return 1;
}
}
//闰年的 二月判断
if (year % 400 == 0||(year % 100 != 0&&year % 4 == 0))
{
if (d > 29&&mouth == 2)//闰年的二月有29天
{
return 1;
}
}
else
{
if (d > 28&&mouth == 2)
{
return 1;
}
}
return 0;
}
//保存信息
void Save_Info(PList stu)
{
FILE* pFile;
pFile= fopen("stu.txt","w+");
if(pFile==NULL)
{
return;
}
stu=stu->next;
while(stu!=NULL)
{
if(!strcmp(stu->stu.num,"101")||!strcmp(stu->stu.num,"102")||!strcmp(stu->stu.num,"103")||!strcmp(stu->stu.num,"104")||!strcmp(stu->stu.num,"105"))
{
stu=stu->next;
continue;
}
fwrite(&stu->stu,1,sizeof(STU),pFile);
stu=stu->next;
}
fclose(pFile);
}
//读取信息
void Read_Info(PList stu)
{
int i=0;
FILE* pFile;
STU temp;
pFile= fopen("stu.txt","r+");
if(pFile==NULL)
{
return ;
}
while(stu->next!=NULL)
{
stu=stu->next;
}
while(fread(&temp,1,sizeof(STU),pFile))
{
stu->next=(PList)malloc(sizeof(List));
stu=stu->next;
stu->stu=temp;
stu->next=NULL;
}
fclose(pFile);
}
void Print_List(List node)
{
PList p=node.next;
// system("cls");
printf("%-15s%-20s%-10s%-12s%-12s%-12s\n","学号","姓名","性别","出生日期","入学日期","毕业日期");
while(p)
{
printf("%-15s",p->stu.num);
printf("%-20s",p->stu.name);
printf("%-10s",p->stu.sex);
printf("%-12s",p->stu.birth);
printf("%-12s",p->stu.In_School);
printf("%-12s\n",p->stu.Out_School);
p=p->next;
}
// system("pause");
}
void Del_Info(PList node)
{
PList p = node->next;
PList q=node;
char temp[20];
// system("cls");
printf("请输入要删除的学生学号:");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.num))
{
q->next=p->next;
free(p);
printf("删除成功!\n");
system("pause");
return ;
}
q=p;
p=p->next;
}
printf("无该学号!\n");
// system("pause");
}
void sort(PList list)//链表排序,冒泡排序
{
PList p,q;
STU t;
for(p=list;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(strcmp(p->stu.num,q->stu.num)>0)//升序
{
t=q->stu;
q->stu=p->stu;
p->stu=t;
}
}
}
}
void Printf_Stu_Info(STU stu)
{
printf("%-15s",stu.num);
printf("%-20s",stu.name);
printf("%-10s",stu.sex);
printf("%-12s",stu.birth);
printf("%-12s",stu.In_School);
printf("%-12s\n",stu.Out_School);
}
void Add_New_Info(PList node)
{
int len;
char str[100];
PList p = node;
// system("cls");
while(p->next!=NULL)
{
p=p->next;
}
p->next=(PList)malloc(sizeof(List));
p=p->next;
p->next=NULL;
do
{
printf("\t\t\t请输入学号:");
scanf("%s",str);
len=strlen(str);
if(len>10)
{
printf("\t\t\t长度错误,重新输入\n");
}
else
{
strcpy(p->stu.num,str);
}
}while(len>10);
do
{
printf("\t\t\t请输入姓名:");
fflush(stdin);
gets(str);
len=strlen(str);
if(len>20)
{
printf("\t\t\t长度错误,重新输入\n");
}
else
{
strcpy(p->stu.name,str);
}
}while(len>20);
do
{
printf("\t\t\t请输入性别:");
scanf("%s",str);
if(!strcmp(str,"男")||!strcmp(str,"女"))
{
strcpy(p->stu.sex,str);
break;
}
else
{
printf("\t\t\t性别只能是(男/女)\n");
}
}while(1);
do
{
printf("\t\t\t请输入出生日期:");
scanf("%s",p->stu.birth);
if(!checkDay(p->stu.birth))
{
break;
}
printf("\t\t\t格式不合法(xxxx-xx-xx)\n");
}while(1);
do
{
printf("\t\t\t请输入入学日期:");
scanf("%s",p->stu.In_School);
if(!checkDay(p->stu.In_School))
{
break;
}
printf("\t\t\t格式不合法(xxxx-xx-xx)\n");
}while(1);
do
{
printf("\t\t\t请输入毕业日期(0代表未毕业):");
scanf("%s",p->stu.Out_School);
if(!strcmp(p->stu.Out_School,"0"))
{
strcpy(p->stu.Out_School,"");
break;
}
if(!checkDay(p->stu.Out_School))
{
break;
}
printf("\t\t\t格式不合法(xxxx-xx-xx)\n");
}while(1);
printf("录入成功!\n");
// system("pause");
}
void Modify_Info(PList node)
{
PList p = node->next;
PList q=node;
int len;
char temp[20];
char str[100];
// system("cls");
printf("\t\t\t请输入要修改的学生学号:");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.num))
{
Printf_Stu_Info(p->stu);
printf("\n\n\n");
do
{
do
{
printf("\t\t\t请输入学号:");
scanf("%s",str);
len=strlen(str);
if(len>10)
{
printf("\t\t\t长度错误,重新输入\n");
}
else
{
strcpy(p->stu.num,str);
}
}while(len>10);
printf("\t\t\t请输入姓名:");
fflush(stdin);
gets(str);
len=strlen(str);
if(len>20)
{
printf("\t\t\t长度错误,重新输入\n");
}
else
{
strcpy(p->stu.name,str);
}
}while(len>20);
do
{
printf("\t\t\t请输入性别:");
scanf("%s",str);
if(!strcmp(str,"男")||!strcmp(str,"女"))
{
strcpy(p->stu.sex,str);
break;
}
else
{
printf("\t\t\t性别只能是(男/女)\n");
}
}while(1);
do
{
printf("\t\t\t请输入出生日期:");
scanf("%s",p->stu.birth);
if(!checkDay(p->stu.birth))
{
break;
}
printf("\t\t\t格式不合法(xxxx-xx-xx)\n");
}while(1);
do
{
printf("\t\t\t请输入入学日期:");
scanf("%s",p->stu.In_School);
if(!checkDay(p->stu.In_School))
{
break;
}
printf("\t\t\t格式不合法(xxxx-xx-xx)\n");
}while(1);
do
{
printf("\t\t\t请输入毕业日期(0代表未毕业):");
scanf("%s",p->stu.Out_School);
if(!strcmp(p->stu.Out_School,"0"))
{
strcpy(p->stu.Out_School,"");
break;
}
if(!checkDay(p->stu.Out_School))
{
break;
}
printf("\t\t\t格式不合法(xxxx-xx-xx)\n");
}while(1);
printf("\t\t\t修改成功!\n");
// system("pause");
return ;
}
q=p;
p=p->next;
}
printf("无该学号!\n");
// system("pause");
}
void Search_Num(PList node)
{
PList p = node->next;
PList q=node;
int flag=0;
char temp[20];
// system("cls");
printf("请输入要查询的学生学号:");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.num))
{
if(flag==0)
{
printf("%-15s%-20s%-10s%-12s%-12s%-12s\n","学号","姓名","性别","出生日期","入学日期","毕业日期");
}
Printf_Stu_Info(p->stu);
flag=1;
}
q=p;
p=p->next;
}
if(flag==0)
{
printf("无该学号信息!\n");
}
// system("pause");
}
void Search_Name(PList node)
{
PList p = node->next;
PList q=node;
int flag=0;
char temp[20];
// system("cls");
printf("请输入要查询的学生姓名:");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.name))
{
if(flag==0)
{
printf("%-15s%-20s%-10s%-12s%-12s%-12s\n","学号","姓名","性别","出生日期","入学日期","毕业日期");
}
Printf_Stu_Info(p->stu);
flag=1;
}
q=p;
p=p->next;
}
if(flag==0)
{
printf("无该姓名信息!\n");
}
// system("pause");
}
void Search_Sex(PList node)
{
PList p = node->next;
PList q=node;
int flag=0;
char temp[20];
//system("cls");
printf("请输入要查询的学生性别:");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.sex))
{
if(flag==0)
{
printf("%-15s%-20s%-10s%-12s%-12s%-12s\n","学号","姓名","性别","出生日期","入学日期","毕业日期");
}
Printf_Stu_Info(p->stu);
flag=1;
}
q=p;
p=p->next;
}
if(flag==0)
{
printf("无该性别信息!\n");
}
// system("pause");
}
void Search_Birth(PList node)
{
PList p = node->next;
PList q=node;
int flag=0;
char temp[20];
// system("cls");
printf("请输入要查询的出生日期:");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.birth))
{
if(flag==0)
{
printf("%-15s%-20s%-10s%-12s%-12s%-12s\n","学号","姓名","性别","出生日期","入学日期","毕业日期");
}
Printf_Stu_Info(p->stu);
flag=1;
}
q=p;
p=p->next;
}
if(flag==0)
{
printf("无该出生日期信息!\n");
}
// system("pause");
}
void Search_Intime(PList node)
{
PList p = node->next;
PList q=node;
int flag=0;
char temp[20];
// system("cls");
printf("请输入要查询的入学日期:");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.In_School))
{
if(flag==0)
{
printf("%-15s%-20s%-10s%-12s%-12s%-12s\n","学号","姓名","性别","出生日期","入学日期","毕业日期");
}
Printf_Stu_Info(p->stu);
flag=1;
}
q=p;
p=p->next;
}
if(flag==0)
{
printf("无该入学日期信息!\n");
}
// system("pause");
}
void Search_Outtime(PList node)
{
PList p = node->next;
PList q=node;
int flag=0;
char temp[20];
//system("cls");
printf("请输入要查询的毕业日期(0表示未毕业):");
scanf("%s",temp);
if(!strcmp(temp,"0"))
{
strcpy(temp,"");
}
while(p)
{
if(!strcmp(temp,p->stu.Out_School))
{
if(flag==0)
{
printf("%-15s%-20s%-10s%-12s%-12s%-12s\n","学号","姓名","性别","出生日期","入学日期","毕业日期");
}
Printf_Stu_Info(p->stu);
flag=1;
}
q=p;
p=p->next;
}
if(flag==0)
{
printf("无该日期信息!\n");
}
//system("pause");
}