first commit

master
30298574152@qq.com 7 years ago
commit 3e12e364a2

@ -0,0 +1,993 @@
#include<stdio.h>
#include<Windows.h>
struct Student
{
char num[20];
char name[20];
char age[20];
char sex[20];
char birthday[20];
char address[20];
char telephone[20];
char email[20];
struct Student *next;
};
void meun() //主菜单,调用分函数
{
void new_student();
void output_student();
void search_meun();
void modify_meun();
void delete_meun();
while(1)
{
system("color f0");
system("cls");;
printf("--------------------------\n");
printf("***--- 1.录入学生信息 ---***\n");
printf("***--- 2.显示学生信息 ---***\n");
printf("***--- 3.查询学生信息 ---***\n");
printf("***--- 4.修改学生信息 ---***\n");
printf("***--- 5.删除学生信息 ---***\n");
printf("***--- 0.退出系统 ---***\n");
printf("***---请输入想执行功能:");
; int n;
scanf("%d",&n);
switch(n)
{
case 1:new_student();break;
case 2:output_student();break;
case 3:search_meun();break;
case 4:modify_meun();break;
case 5:delete_meun();break;
case 0:printf("***---感谢您的使用---***");
Sleep(1);
exit(0);
system("cls");
break;
default:if(n<0||n>6)
{
printf("***---您输入有误,请重试!---***");
Sleep(500);
continue;
}
}
}
}
void new_student()
{
system("color 12");
int i, n, bools;
struct Student *head=NULL, *p=NULL, *p1=NULL, *p2=NULL;
system("cls");
printf("------------------------\n");
printf("***--- 0.返回主菜单 ---***\n");
printf("***--- 1.重录 ---***\n");
printf("***--- 2.补录 ---***\n");
printf("------------------------\n");
printf("***---请选择录入方式: ");
scanf("%d", &bools);
if (((bools!=1) && (bools!=2)) || (bools==0))
{
system("cls");
if(bools!=0)
{
printf("***---您输错了,请重试!---***\n");
system("pause");
}
return;
}
system("cls");
printf("***---请输入学生录入人数: ");
scanf("%d",&n);
if (n <= 0)
{
system("cls");
printf("***---您输错了,请重试!---***\n");;
system("pause");
return;
}
FILE *fp1;
p2 = NULL;
if (bools == 2)
{
if ((fp1 = fopen("Student.txt","r")) == NULL)
{
system("cls");
printf("***---文件打开失败!请重试!---***\n");;
system("pause");
return;
}
int ch = fgetc(fp1);
rewind(fp1);
if(ch!=EOF)
{
while (!feof(fp1))
{
if ((p1 = (struct Student*)malloc(sizeof(struct Student)))== NULL)
{
printf("***---获取数据失败,请重试!---***\n");
return;
}
fscanf(fp1,"%s %s %s %s %s %s %s %s", p1->num, p1->name, p1->age, p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
if (p2 == NULL)
{
head = p2 = p1;
p2->next = NULL;
}
else
{
p2->next = p1;
p2 = p1;
p2->next = NULL;
}
}
}
if (fclose(fp1))
{
printf("***---文件关闭失败!请重试!---***\n");
return;
}
}
p = p1 = p2 = NULL;
for(i=0; i<n; i++)
{
if ((p = (struct Student*)malloc(sizeof(struct Student))) == NULL)
{
printf("***---获取内存失败,请重试!---***\n");
return;
}
printf("***---请输入第%d个学生的信息---***\n",i+1);
while (1)
{
printf("***---学号为11位请输入学生学号");
scanf("%s",p->num);
if (strlen(p->num) !=11)
{
printf("***---您输入有误,请重新输入!---***");;
system("pause");
continue;
}
else
{
break;
}
}
printf("***---请输入学生姓名:");
scanf("%s",p->name);
printf("***---请输入学生年龄:");
scanf("%s",p->age);
printf("***---请输入学生性别:");
scanf("%s",p->sex);
printf("***---请输入出生年月:");
scanf("%s",p->birthday);
printf("***---请输入学生地址:");
scanf("%s",p->address);
printf("***---请输入学生电话:");
scanf("%s",p->telephone);
printf("***---请输入email");
scanf("%s",p->email);
if (head == NULL)
{
head = p;
p->next = NULL;
}
else if (head->next == NULL)
{
p2 = p1 = head;
}
else
{
p1 = head;
p2 = head->next;
}
while (p1 != NULL)
{
if (strcmp(p->num, p2->num) < 0)
{
if (strcmp(p->num, p1->num) < 0)
{
p->next = p1;
head = p;
}
else
{
p->next = p2;
p1->next = p;
}
break;
}
else if (p2->next == NULL)
{
p2->next = p;
p->next = NULL;
break;
}
else
{
p1 = p1->next;
p2 = p2->next;
}
}
}
FILE *fp2;
if ((fp2 = fopen("Student.txt","w")) == NULL)
{
printf("***---文件打开失败!请重试!---***\n");
return;
}
p = head;
while (p != NULL)
{
fprintf(fp2,"%s %s %s %s %s %s %s %s\n", p->num,p->name,p->age,
p->sex,p->birthday,p->address,p->telephone,p->email);
p = p->next;
}
if (fclose(fp2))
{
printf("***---文件关闭失败!请重试!---***\n");
return;
}
else
{
for (int i=1; i<=10; i++)
{
printf("***---正在保存*****%d0%%,请稍后\n", i);
Sleep(1);
system("cls");
}
printf("***---保存成功!---***\n");
Sleep(500);
}
}
void output_student()
{
system("color 12");
getchar();
struct Student *head;
struct Student *p1=NULL, *p2=NULL;
FILE *fp1;
if ((fp1 = fopen("Student.txt","r")) == NULL)
{
printf("***---文件打开失败!请重试!---***\n");
return;
}
if ((head = (struct Student*)malloc(sizeof(struct Student))) == NULL)
{
printf("***---获取内存失败,请重试!---***\n");
return;
}
int ch = fgetc(fp1);
if(ch==EOF)
{
system("cls");
printf("***---没有学生信息,请录入!---***\n");;
system("pause");
return;
}
rewind(fp1);
while (!feof(fp1))
{
if ((p1 = (struct Student*)malloc(sizeof(struct Student))) == NULL)
{
printf("***---获取数据失败,请重试!---***\n");
return;
}
fscanf(fp1,"%s %s %s %s %s %s %s %s", p1->num,p1->name,p1->age,
p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
if (p2 == NULL)
{
head = p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
p2->next = NULL;
}
}
if (fclose(fp1))
{
printf("***---文件关闭失败!请重试!---***\n");
return;
}
p2 = head;
system("cls");
printf("学号: 姓名: 年龄: 性别: 出生年月: 地址: 电话号码: 邮箱:\n\n");
while (p2->next!=NULL)
{
printf("%-15s%-8s%-8s%-8s%-15s%-20s%-14s%-30s\n", p2->num,p2->name,p2->age,
p2->sex,p2->birthday,p2->address,p2->telephone,p2->email);
p2 = p2->next;
};
system("pause"); //系统暂停函数,任意键继续
}
void search_meun() //打印查询信息菜单,调用查询函数
{
system("color 25");
char bools;
char info[20];
while (1)
{
system("cls");
printf("--------------------------------\n");
printf("***--- 0.返回主菜单 ---***\n");
printf("***--- 1.按学号查询 ---***\n");
printf("***--- 2.按姓名查询 ---***\n");
printf("--------------------------------\n");
printf("***---请输入你想执行的功能:");
getchar();
scanf("\n%c",&bools);
getchar();
system("cls");
if ((bools != '1') && (bools != '2'))
{
return ;
}
else if (bools == '1')
{
printf("***---请输入要查询学生的学号:");
scanf("\n%s",info);
}
else
{
printf("***---请输入要查询学生的姓名:");
scanf("\n%s",info);
}
char search_student(char bools, char *info);
char ch = search_student(bools,info);
if ((ch!='y') && (ch!='Y'))
{
break;
}
}
getchar();
}
char search_student(char bools, char *info) //查询函数
{
struct Student *head=NULL;
struct Student *p1=NULL, *p2=NULL;
FILE *fp1;
if ((fp1 = fopen("Student.txt","r")) == NULL)
{
printf("***---文件打开失败,请重试!\n");
return 'n';
}
char ch = fgetc(fp1);
if(ch==EOF)
{
system("cls");
printf("***---没有学生信息,请录入!---***\n");
printf("\n");
system("pause");
return 'n';
}
rewind(fp1);
while (!feof(fp1))
{
if ((p1 = (struct Student*)malloc(sizeof(struct Student))) == NULL)
{
printf("***---获取数据失败,请重试!---***\n");
return 'n';
}
fscanf(fp1,"%s %s %s %s %s %s %s %s", p1->num,p1->name,p1->age,
p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
if (p2 == NULL)
{
head = p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
p2->next = NULL;
}
}
if (fclose(fp1))
{
printf("***---文件关闭失败,请重试!---***\n");
return 'n';
}
p1 = head;
int flag=0;
do {
if (bools == '1')
{
if (!strcmp(p1->num, info))
{
system("cls");
printf("***---您查询的学生的信息为:\n\n");
printf("学号: 姓名:年龄: 性别: 出生年月: 地址: 电话号码: 邮箱:\n");
printf("%-15s%-8s%-8s%-8s%-15s%-20s%-14s%-30s\n", p1->num,p1->name,p1->age,
p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
flag = 1;
printf("\n");
system("pause");
system("cls");
printf("-------------------------\n");
printf("***--- y.继续查询 ---***\n");
printf("***--- n.退出查询 ---***\n");
printf("-------------------------\n");
printf("***---是否继续查询:\n");
getchar();
char ch = getchar();
return ch;
}
else
{
p1 = p1->next;
}
}
else
{
if (!strcmp(p1->name, info))
{
system("cls");
printf("***---您查询的学生的信息为:\n\n");
printf("学号: 姓名: 年龄: 性别: 出生年月: 地址: 电话号码: 邮箱:\n");
printf("%-15s%-8s%-8s%-8s%-15s%-20s%-14s%-30s\n", p1->num,p1->name,p1->age,
p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
flag = 1;
printf("\n");
system("pause");
system("cls");
printf("-------------------------\n");
printf("***--- y.继续查询 ---***\n");
printf("***--- n.退出查询 ---***\n");
printf("-------------------------\n");
printf("***---是否继续查询:\n");
getchar();
char ch = getchar();
return ch;
}
else
{
p1 = p1->next;
}
}
} while (p1->next != NULL);
if (!flag)
{
printf("***---您查询的学生不存在,请重试!---***\n");
system("pause");
return 'y';
}
}
void modify_meun() //打印修改信息菜单,调用修改函数
{
void modify(char bools,char *info);
system("color 5f");
char bools;
char info[20];
system("cls");
printf("--------------------------------\n");
printf("***--- 0.返回主菜单 ---***\n");
printf("***--- 1.按学号修改 ---***\n");
printf("***--- 2.按姓名修改 ---***\n");
printf("--------------------------------\n");
printf("***---请输入你想执行的功能:");
scanf("\n%c",&bools);
system("cls");
if ((bools != '1') && (bools != '2'))
{
getchar();
return ;
}
else if (bools == '1')
{
printf("***---请输入要修改学生的学号:");
scanf("\n%s",info);
}
else
{
printf("***---请输入要修改学生的姓名:");
scanf("\n%s",info);
}
modify(bools,info);
}
void modify_menu_2() //打印需修改属性菜单
{
printf("\n");
printf("-----------------------------------------------\n");
printf("***--- 1.学号:\n");
printf("***--- 2.姓名:\n");
printf("***--- 3.年龄:\n");
printf("***--- 4.性别:\n");
printf("***--- 5.出生年月:\n");
printf("***--- 6.地址:\n");
printf("***--- 7.电话号码:\n");
printf("***--- 8.邮箱:\n");
printf("***--- 0.不修改:\n");
printf("------------------------------------------------\n");
printf("***---请输入修改哪些信息,可多选,以空格隔开:");
}
void modify(char bools,char *info) //修改函数
{
struct Student *head=NULL;
struct Student *p1=NULL,*p2=NULL;
FILE *fp1;
if ((fp1 = fopen("Student.txt","r")) == NULL)
{
printf("***---文件打开失败,请重试---***\n");
return;
}
int ch1 = fgetc(fp1);
if(ch1==EOF)
{
system("cls");
printf("***---没有学生信息,请录入!---***\n");
system("pause");
return;
}
rewind(fp1);
while (!feof(fp1))
{
if ((p1 = (struct Student*)malloc(sizeof(struct Student))) == NULL)
{
printf("***---获取数据失败,请重试!---***\n");
return;
}
fscanf(fp1,"%s %s %s %s %s %s %s %s", p1->num,p1->name,p1->age,
p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
if (p2 == NULL)
{
head = p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
p2->next = NULL;
}
}
if (fclose(fp1))
{
printf("***---文件关闭失败,请重试!---***\n");
return;
}
int i,flag=0; //判断是否找到该学生找到为1
int a[9] = {-1};
char ch = ' ';
p1 = head;
p2 = head->next;
//输入学号修改
if (bools == '1')
{
if (!strcmp(head->num, info))
{
system("cls");
printf("***---您要修改的学生的信息为:\n\n");
printf("学号: 姓名: 年龄: 性别: 出生年月: 地址: 电话号码: 邮箱:\n");
printf("%-15s%-8s%-8s%-8s%-15s%-20s%-14s%-30s\n", p1->num,p1->name,p1->age,
p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
modify_menu_2();
for (i=0; ch == ' '; i++)
{
scanf("%d", &a[i]);
ch = getchar();
}
for (i=0; a[i]; i++)
{
switch (a[i])
{
case 1:
printf("***---请输入修改后的学号:\n");
gets(p1->num);
break;
case 2:
printf("***---请输入修改后的姓名:\n");
gets(p1->name);
break;
case 3:
printf("***---请输入修改后的年龄:\n");
gets(p1->age);
break;
case 4:
printf("***---请输入修改后的性别:\n");
gets(p1->sex);
break;
case 5:
printf("***---请输入修改后的出生年月:\n");
gets(p1->birthday);
break;
case 6:
printf("***---请输入修改后的地址:\n");
gets(p1->address);
break;
case 7:
printf("***---请输入修改后的电话号码:\n");
gets(p1->telephone);
break;
case 8:
printf("***---请输入修改后的邮箱:\n");
gets(p1->email);
break;
default:
printf("***---您输入有误,请重试!\n");
system("pause");
return;
}
}
flag = 1;
}
else
{
while (p2->next != NULL)
{
if (!strcmp(p2->num, info))
{
system("cls");
printf("***---您要修改的学生的信息为:\n\n");
printf("学号: 姓名: 年龄: 性别: 出生年月: 地址: 电话号码: 邮箱:\n");
printf("%-15s%-8s%-8s%-8s%-15s%-20s%-14s%-30s\n", p2->num, p2->name, p2->age,
p2->sex, p2->birthday, p2->address, p2->telephone, p2->email);
modify_menu_2();
for (i=0; ch == ' '; i++)
{
scanf("%d", &a[i]);
ch = getchar();
}
for (i=0; a[i]; i++)
{
switch (a[i])
{
case 1:
printf("***---请输入修改后的学号:\n");
gets(p2->num);
break;
case 2:
printf("***---请输入修改后的姓名:\n");
gets(p2->name);
break;
case 3:
printf("***---请输入修改后的年龄:\n");
gets(p2->age);
break;
case 4:
printf("***---请输入修改后的性别:\n");
gets(p2->sex);
break;
case 5:
printf("***---请输入修改后的出生年月:\n");
gets(p2->birthday);
break;
case 6:
printf("***---请输入修改后的地址:\n");
gets(p2->address);
break;
case 7:
printf("***---请输入修改后的电话号码:\n");
gets(p2->telephone);
break;
case 8:
printf("***---请输入修改后的邮箱:\n");
gets(p2->email);
break;
default:
printf("***---您输入有误,请重试!\n");;
system("pause");
return;
}
}
flag = 1;
break;
}
else
{
p1 = p1->next;
p2 = p2->next;
}
}
}
}
//输入姓名修改
else
{
if (!strcmp(head->name, info))
{
system("cls");
printf("***---您要修改的学生的信息为:\n\n");
printf("学号: 姓名: 年龄: 性别: 出生年月: 地址: 电话号码: 邮箱:\n");
printf("%-15s%-8s%-8s%-8s%-15s%-20s%-14s%-30s\n", p1->num,p1->name,p1->age,
p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
modify_menu_2();
for (i=0; ch == ' '; i++)
{
scanf("%d", &a[i]);
ch = getchar();
}
for (i=0; a[i]; i++)
{
switch (a[i])
{
case 1:
printf("***---请输入修改后的学号:\n");
gets(p1->num);
break;
case 2:
printf("***---请输入修改后的姓名:\n");
gets(p1->name);
break;
case 3:
printf("***---请输入修改后的年龄:\n");
gets(p1->age);
break;
case 4:
printf("***---请输入修改后的性别:\n");
gets(p1->sex);
break;
case 5:
printf("***---请输入修改后的出生年月:\n");
gets(p1->birthday);
break;
case 6:
printf("***---请输入修改后的地址:\n");
gets(p1->address);
break;
case 7:
printf("\t\t\t\t请输入修改后的电话号码:\n");
gets(p1->telephone);
break;
case 8:
printf("***---请输入修改后的邮箱:\n");
gets(p1->email);
break;
default:
printf("***---您输入有误,请重试!\n");;
system("pause");
return;
}
}
flag = 1;
}
else
{
while (p2->next != NULL)
{
if (!strcmp(p2->name, info))
{
system("cls");
printf("***---您要修改的学生的信息为:\n\n");
printf("学号: 姓名: 年龄: 性别: 出生年月: 地址: 电话号码: 邮箱:\n");
printf("%-15s%-8s%-8s%-8s%-15s%-20s%-14s%-30s\n", p2->num,p2->name,p2->age,
p2->sex,p2->birthday,p2->address,p2->telephone,p2->email);
modify_menu_2();
for (i=0; ch == ' '; i++)
{
scanf("%d", &a[i]);
ch = getchar();
}
for (i=0; a[i]; i++)
{
switch (a[i])
{
case 1:
printf("***---请输入修改后的学号:\n");
gets(p2->num);
break;
case 2:
printf("***---请输入修改后的姓名:\n");
gets(p2->name);
break;
case 3:
printf("***---请输入修改后的年龄:\n");
gets(p2->age);
break;
case 4:
printf("***---请输入修改后的性别:\n");
gets(p2->sex);
break;
case 5:
printf("***---请输入修改后的出生年月:\n");
gets(p2->birthday);
break;
case 6:
printf("***---请输入修改后的地址:\n");
gets(p2->address);
break;
case 7:
printf("***---请输入修改后的电话号码:\n");
gets(p2->telephone);
break;
case 8: printf("***---请输入修改后的邮箱:\n");
gets(p2->email);
break;
default:
printf("***---您输入有误,请重试!\n");;
system("pause");
return;
}
}
flag = 1;
break;
}
else
{
p1 = p1->next;
p2 = p2->next;
}
}
}
}
if (a[0] == 0)
{
system("cls");
printf("***---您未进行修改---***\n");
Sleep(500);
return;
}
if (!flag)
{
printf("***---您修改的学生不存在,请重试!---***\n");
system("pause");
return;
}
else
{
for (int i=1; i<=10; i++)
{
printf("***---正在修改*****%d0%%,请稍后---***\n", i);
Sleep(1);
system("cls");
}
printf("***---修改成功!---***\n");
Sleep(500);
}
char menu_modefy_save();
char temp = menu_modefy_save(); //保存修改后的数据
if ((temp == 'y') || (temp == 'Y'))
{
FILE *fp2;
p1 = head;
if ((fp2 = fopen("Student.txt","w")) == NULL)
{
printf("***---文件打开失败,请重试!---***\n");
return;
}
while (p1->next != NULL)
{
fprintf(fp2,"%s %s %s %s %s %s %s %s\n", p1->num,p1->name,p1->age,
p1->sex,p1->birthday,p1->address,p1->telephone,p1->email);
p1 = p1->next;
}
if (fclose(fp2))
{
printf("***---文件关闭失败,请重试!---***\n");
return;
}
else
{
for (int i=1; i<=10; i++)
{
printf("***---正在保存*****%d0%%,请稍后---***\n", i);
Sleep(1);
system("cls");
}
printf("***---保存成功!---***\n");
Sleep(500);
system("cls");
}
}
else
{
system("cls");
printf("***---您的修改操作未保存---***\n");
Sleep(500);
}
}
char menu_modefy_save() //打印修改是否保存的菜单
{
system("cls");
printf("--------------------------\n");
printf("***--- y.保存 ---***\n");
printf("***--- n.不保存 ---***\n");
printf("--------------------------\n");
printf("***---请选择是否保存:");
return getchar();
}
Loading…
Cancel
Save