#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char num[20];//学号
char name[20];//姓名
int age;//年龄
char sex[10];//性别
char time[20];//出生年月
char addr[20];//住址
char tel[20];//电话
char mail[20];//邮箱
}STU;
STU p[100];//定义结构体数组
int N=0;//记录人数
/*选择菜单 将选择返回给主函数*/
int menu()
{
int n;
system("cls");//清屏
printf("1:学生信息录入\n");
printf("2:学生信息浏览\n");
printf("3:按学号查询\n");
printf("4:按姓名查询\n");
printf("0:退出\n");
scanf("%d",&n);
while(n<0||n>4)//循环获取 确保范围正确
{
printf("范围不正确,重新输入\n");
scanf("%d",&n);
}
return n;
}
void Input_Stu()
{
system("cls");
printf("请输入学生学号\n");
scanf("%s",&p[N].num);
printf("请输入学生姓名\n");
scanf("%s",&p[N].name);
printf("请输入学生年龄\n");
scanf("%d",&p[N].age);
printf("请输入学生性别\n");
scanf("%s",&p[N].sex);
printf("请输入学生出生年月\n");
scanf("%s",&p[N].time);
printf("请输入学生住址\n");
scanf("%s",&p[N].addr);
printf("请输入学生电话\n");
scanf("%s",&p[N].tel);
printf("请输入学生邮箱\n");
scanf("%s",&p[N].mail);
N++;//人数+1
printf("添加成功!\n");
system("pause");
}
void PRINT()
{
int i;
system("cls");
if(0==N)//空结构体数组
{
printf("无学生信息!\n");
system("pause");
return ;
}
printf("%-10s%-10s%-5s%-5s%-10s%-10s%-11s%-11s\n","学号","姓名","年龄","性别","出生年月","住址","电话","邮箱");
for(i=0;i<N;i++)//遍历结构体数组输出
{
printf("%-10s%-10s%-5d%-5s%-10s%-10s%-11s%-11s\n",p[i].num,p[i].name,p[i].age,p[i].sex,p[i].time,p[i].addr,p[i].tel,p[i].mail);
}
system("pause");
}
/*查找学号*/
void serach_num()
{
int i;
char temp[20];
system("cls");
printf("请输入要查找的学号:\n");
scanf("%s",temp);
for(i=0;i<N;i++)
{
if(!strcmp(temp,p[i].num))
{
printf("%-10s%-10s%-5s%-5s%-10s%-10s%-11s%-11s\n","学号","姓名","年龄","性别","出生年月","住址","电话","邮箱");
printf("%-10s%-10s%-5d%-5s%-10s%-10s%-11s%-11s\n",p[i].num,p[i].name,p[i].age,p[i].sex,p[i].time,p[i].addr,p[i].tel,p[i].mail);
break;//找到则跳出 不考虑学号相同的存在
}
}
if(i==N)//如果遍历结束说明没有找到
{
printf("无该学号信息!\n");
}
system("pause");
}
/*按名字查找 考虑可能有重复名字 所以flag用来记录是否查找到 遍历整个结构体数组*/
void serach_name()
{
int i,flag=1;
char temp[20];
system("cls");
printf("请输入要查找的姓名:\n");
scanf("%s",temp);
for(i=0;i<N;i++)
{
if(!strcmp(temp,p[i].name))
{
if(flag==1)
{
flag=0;
printf("%-10s%-10s%-5s%-5s%-10s%-10s%-11s%-11s\n","学号","姓名","年龄","性别","出生年月","住址","电话","邮箱");
}
printf("%-10s%-10s%-5d%-5s%-10s%-10s%-11s%-11s\n",p[i].num,p[i].name,p[i].age,p[i].sex,p[i].time,p[i].addr,p[i].tel,p[i].mail);
}
}
if(flag)
{
printf("无该姓名信息!\n");
}
system("pause");
}
/*主函数*/
int main()
{
int n;
do
{
n=menu();
switch(n)//根据选择进入相应界面
{
case 1:
Input_Stu();
break;
case 2:
PRINT();
break;
case 3:
serach_num();
break;
case 4:
serach_name();
break;
}
}while(n!=0);//选项为0退出系统
return 0;
}
学生信息管理系统
最新推荐文章于 2022-04-10 12:19:50 发布