/*
学生信息管理系统
0.定义全局变量结构体存储学生信息
1.欢迎界面
2.提示如何操作并接收用户操作(按学号、姓名(模糊音)、性别(男/女)、分数(可嵌套))
3.按用户输入的信息去查找并输出相关学生信息
*/
#include <stdio.h>
#include <string.h>
#define length 5
struct Stu
{
int _number;
char* _name;
char* _gender;
float _score;
};
struct Stu stu1[5] =
{
{01,"jack","男",70.8},
{02,"rose","女",80.9},
{03,"Luffy","男",77.7},
{04,"Ayse","男",96.6},
{05,"selinna","女",66.7},
};
struct Stu *stu = stu1;
/**
* 显示欢迎界面
*/
void showLogo();
/**
* 输出第i位学生信息
*/
void outputMsgOfI();
/**
* 提示玩法,让用户输入,返回输入的值
*/
int letUserInput();
/**
* 按学号查询
*/
void searchByStuNumber();
/**
* 按姓名查询
*/
void searchByStuName();
/**
* 按姓名查询
*/
void searchByStuGender();
/**
* 按分数查询
*/
void searchByStuScore();
/**
* main函数在此!
*
*/
int main(int argc, const char * argv[])
{
showLogo();
unsigned char userInputNum = letUserInput();
switch (userInputNum)
{
case '1'://数字1在ASCII、码中,存储的值为 49
searchByStuNumber();
break;
case '2':
searchByStuName ();
break;
case '3':
searchByStuGender();
break;
case '4':
searchByStuScore();
break;
case '5':
printf("学号\t\t姓名\t \t性别\t\t分数\n");
for (int i = 0; i < length; i++)
{
outputMsgOfI(i);
}
break;
default:
break;
}
return 0;
}
void showLogo()
{
printf("\t\t*******************\n");
printf("\t\t**欢迎您查询学生信息**\n");
printf("\t\t*******************\n");
}
void outputMsgOfI(int i)
{
// printf("学号\t\t姓名\t \t性别\t\t分数\n");
printf(" %-6d\t%-11s\t %-8s\t%-4.1f\n",(stu + i)->_number,(stu + i)->_name,(stu + i)->_gender,(stu + i)->_score);
}
int letUserInput()
{
unsigned char num = '2';//不是 2 ,因为下面是跟 字符‘1’(ascii码为49)比较!不赋初值的话num可能为 0 ,会使下面的do...while(),()会满足条件!
printf("请选择以下其中一种查询方式:\n");
printf("输入1,按学号查询;\n");
printf("输入2,按姓名(支持模糊音)查询;\n");
printf("输入3,按性别查询;\n");
printf("输入4,按分数段查询。\n");
printf("输入5,查询全部学生的成绩。\n");
do
{
if (num <'1' || num > '5')
{
printf("您输入的数字有误,请重新输入:\n");
}
rewind(stdin);
scanf("%c",&num);
} while (num < '1' || num > '5');
return num;
}
void searchByStuNumber()
{
unsigned char stu_number;
do
{
printf("请输入学号(1-5):\n");
rewind(stdin);
scanf("%c",&stu_number);
}while (stu_number < '1' || stu_number > '5');
switch (stu_number)
{
case '1':
printf("学号\t\t姓名\t \t性别\t\t分数\n");
outputMsgOfI(0);
break;
case '2':
printf("学号\t\t姓名\t \t性别\t\t分数\n");
outputMsgOfI(1);
break;
case '3':
printf("学号\t\t姓名\t \t性别\t\t分数\n");
outputMsgOfI(2);
break;
case '4':
printf("学号\t\t姓名\t \t性别\t\t分数\n");
outputMsgOfI(3);
break;
case '5':
printf("学号\t\t姓名\t \t性别\t\t分数\n");
outputMsgOfI(4);
break;
default:
break;
}
}
void searchByStuName()
{
char stuName[10];
unsigned char flag = 0;
printf("请输入学生姓名:\n");
rewind(stdin);
fgets(stuName, 10, stdin);
int len = (int )strlen(stuName);//最好强制转换
if ('\n' == stuName[len - 1])//必须判定,不然多一个 '\n' 会导致名字比较不成功!每次用 fgets 都要注意这点!
{
stuName[len - 1] = '\0';
}
for (int i = 0; i < length; i++)
{
if ( strstr((stu + i)->_name, stuName) != NULL )//某个名字,包含用户输入的字符的话
{
flag++;//不能用i的值判定,因为不确定是哪个i符合条件,才进入循环的!
if (1 == flag)//我去!好厉害!刚把的!防止打多个表头!!!
{
printf("学号\t\t姓名\t \t性别\t\t分数\n");
}
outputMsgOfI(i);
}
}
}
void searchByStuGender()
{
unsigned char gender;//定义为此类型,节省空间
do
{
printf("请输入学生性别对应的数字(男-1,女-0):\n");
rewind(stdin);
scanf("%c",&gender);
}while (gender != '0' && '1' != gender);
if (gender == '1')
{
outputMsgOfI(0);
outputMsgOfI(2);
outputMsgOfI(3);
}else
{
outputMsgOfI(1);
outputMsgOfI(4);
}
}
void searchByStuScore()
{
unsigned char minScore = 0, maxScore = 100;
do
{
printf("请输入需要查询成绩的区域(格式:最小分数,最大分数):\n");
rewind(stdin);
scanf("%d,%d",&minScore,&maxScore);
}while (minScore < 0 || maxScore > 100);
for (int i = 0; i < length; i++)
{
if ((stu + i)->_score >= minScore && (stu + i)->_score <= maxScore)
{
outputMsgOfI(i);
}
}
}
学生信息管理系统
最新推荐文章于 2023-05-17 11:02:54 发布