You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
5.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define N 2
struct student
{
char name[10];
char no[100];
int sex;
int age;
char bnote[100];
};
void print(bool menu)//打印基本信息
{
if (!menu)
{
printf(" \t \t \t (欢迎使用本班级档案管理系统)\t\t\t\t\n");
printf("★*★*★*★*★*★*★*★*★*★ 欢迎进入我们的系统 ★*★*★*★*★*★*★*★*★*★\n\n");
printf("男:1,女:2\n");
printf("学生基本信息录入 ,press 1\n");
}
else
{
printf("学生基本信息显示 ,press 2\n");
printf("学生基本信息保存 ,press 3\n");
printf("学生基本信息删除 ,press 4\n");
printf("学生基本信息修改 ,press 5\n");
printf("学生基本信息查询 ,press 6\n");
}
printf("退出学生信息系统 ,press 7\n");
}
bool Check(char pas[6])//密码验证
{
FILE *fp;
char ch;
char a[7]={" "};
int i = 0;
if ((fp=fopen("D:\\1.txt","r"))==NULL)
{
printf("打开文件失败!\n");
exit(0);
}
ch=fgetc(fp);
while (ch!=EOF&&i<6)
{
a[i] = ch;
ch=fgetc(fp);
i++;
}
a[6] = '\0';
if (!strcmp(pas,a))
{
return true;
}
else
{
printf("密码错误\n");
return false;
}
fclose(fp);
}
void GetInfo(struct student *arr,int n)//学生基本信息录入
{
FILE *fp;
fp=fopen("D:\\student.txt","w");
for (int i = 0;i<n;i++)
{
printf("请输入学生的姓名:");
scanf("%s",arr[i].name);
printf("请输入学生的学号:");
scanf("%s",&arr[i].no);
printf("请输入学生的性别:");
scanf("%d",&arr[i].sex);
printf("请输入学生的年龄:");
scanf("%d",&arr[i].age);
printf("备注:");
scanf("%s",&arr[i].bnote);
}
for(int i=0;i<2;i++)
{
fprintf(fp,"%12s%12s%12d%12d%12s\n",arr[i].name,arr[i].no,arr[i].sex,arr[i].age,arr[i].bnote);
}
fclose(fp);
}
void Display(struct student* arr,int n)//学生基本信息显示
{
FILE *fp;
fp=fopen("D:\\student.txt","r");
printf("学生信息: 名字 学号 性别(1男/2女) 年龄 备注\n");
for (int i = 0;i<n;i++)
{
fscanf(fp,"%12s%12s%12d%12d%12s\n",&arr[i].name,&arr[i].no,&arr[i].sex,&arr[i].age,&arr[i].bnote);
printf("%12s%12s%12d%12d%12s\n",arr[i].name,arr[i].no,arr[i].sex,arr[i].age,arr[i].bnote);
}
fclose(fp);
}
void Save(struct student* arr,int n)//学生基本信息保存保存在c:\\student.txt
{
FILE *fp;
fp= fopen("D:\\student.txt","w+");
if (fp == NULL)
{
printf("文件不存在!\n");
exit(0);
}
for(int i=0;i<2;i++)
{
fprintf(fp,"%12s%12s%12d%12d%12s\n",arr[i].name,arr[i].no,arr[i].sex,arr[i].age,arr[i].bnote);
}
fclose(fp);
}
void Modfiy(struct student* arr,int n)//学生基本信息修改
{
char number[100];
bool flag=false;
char pass[6];
printf("请输入密码:\n");
scanf("%s",pass);
if (Check(pass))
{
printf("修改学生信息:\n");
printf("请输入学号:\n");
scanf("%s",number);
for (int i =0;i<n;i++)
{
if (!strcmp(number,arr[i].no))
{
printf("infor: name no sex age note\n");
printf("please input information:\n");
printf("请输入学生的姓名:");
scanf("%s",&arr[i].name);
printf("请输入学生的学号:");
scanf("%s",&arr[i].no);
printf("请输入学生的性别:");
scanf("%d",&arr[i].sex);
printf("请输入学生的年龄:");
scanf("%d",&arr[i].age);
printf("备注:");
scanf("%s",&arr[i].bnote);
Save(arr,n);
flag = true;
}
}
if (flag == false)
{
printf("该学生不存在!:\n");
}
}
}
void Delete(struct student* arr,int n)//学生基本信息删除
{
char number[100];
bool flag=false;
printf("Delete information:\n");
printf("请输入学号:\n");
scanf("%s",number);
for (int i =0;i<n;i++)
{
if (!strcmp(number,arr[i].no))
{
strcpy(arr[i].name," ");
strcpy(arr[i].no," ");
strcpy(arr[i].bnote," ");
arr[i].sex = ' ';
arr[i].age =0;
Save(arr,n);
flag = true;
}
}
if (flag == false)
{
printf("该学生不存在!:\n");
}
}
void Search(struct student* arr,int n)//学生基本信息查询
{
int temp,i;
bool flag=false;
printf("--------------通过学号查询,press 1-------------\n");
printf("---------------通过姓名查询,press 2-----------\n");
printf("---------------通过性别查询,press 3------------\n");
printf("---------------通过年龄查询,press 4------------\n");
printf("---------------退出,press 5 ------------\n");
scanf("%d",&temp);
switch (temp)
{
case 1://按照学号查询
{
char num[100];
printf("请输入学号:\n");
scanf("%s",num);
for ( i =0;i<n;i++)
{
if (!strcmp(num,arr[i].no))
{
printf("%12s%12s%12c%12d%12s\n",arr[i].name,arr[i].no,
arr[i].sex,arr[i].age,arr[i].bnote);
flag = true;
}
}
if (!flag)
{
printf("该学生不存在.\n");
}
}
break;
case 2://按照名字查询
{
char num[10];
printf("please input name:\n");
scanf("%s",num);
for ( i =0;i<n;i++)
{
if (!strcmp(num,arr[i].name))
{
printf("%-12s%-12s%-12c%-12d%-12s",arr[i].name,arr[i].no,arr[i].sex,arr[i].age,arr[i].bnote);
flag = true;
}
}
if (!flag)
{
printf("该学生不存在.\n");
}
}
break;
case 3://按照性别查询
{
char ch;
printf("请输入性别:\n");
//scanf("%c",&ch);
ch = getch();
for ( i =0;i<n;i++)
{
if (ch == arr[i].sex)
{
printf("%12s%12s%12c%12d%12s\n",arr[i].name,arr[i].no,
arr[i].sex,arr[i].age,arr[i].bnote);
flag = true;
}
}
if (!flag)
{
printf("t该学生不存在.\n");
}
}
break;
case 4://按照年龄查询
{
char num_age;
printf("请输入年龄:\n");
scanf("%d",&num_age);
for ( i =0;i<n;i++)
{
if (num_age == arr[i].age)
{
printf("%12s%12s%12c%12d%12s\n",arr[i].name,arr[i].no,
arr[i].sex,arr[i].age,arr[i].bnote);
flag = true;
}
}
if (!flag)
{
printf("该学生不存在.\n");
}
}
break;
default:
break;
}
}
int main()
{
struct student arr[N];
char ps[6];
bool flag =false;
printf("请输入密码:\n");
scanf("%s",ps);
flag = Check(ps);
if (flag)
{
int select;
print(false);
scanf("%d",&select);
while (0<select && select <7 )
{
switch (select)
{
case 1:
GetInfo(arr,N);
print(true);
break;
case 2:
Display(arr,N);
print(true);
break;
case 3:
Save(arr,N);
print(true);
break;
case 4:
Delete(arr,N);
print(true);
break;
case 5:
Modfiy(arr,N);
print(true);
break;
case 6:
Search(arr,N);
print(true);
break;
default:
break;
}
printf("请选择:\n");
scanf("%d",&select);
}
}
}