#include <stdio.h>
#include <string.h>
#include <assert.h>
//声明结构体类型
typedef struct student{
int id;
char name[20];
double score;
}Stu;
int count = 0; //记录学生的有效个数
/*添加一个学生,成功返回1 失败返回0*/
int addStudent(Stu* stus, int len);
/*检查学生ID是否重复,已经存在返回1 反之返回0*/
int checkID(Stu* stus, int id);
/*打印所有学生的信息*/
void printAllStudent(Stu* stus);
/*按姓名查找学生信息*/
void findStudentByName(Stu* stus);
/*通过学生的ID修改学生的分数,成功返回1 失败返回0*/
int modifyScoreById(Stu* stus);
/*通过ID删除指定的学生信息,成功返回1 失败返回0*/
int deleteStudentById(Stu* stus);
/*通过分数对学生生进升序排序*/
void sortStudentByScore(Stu* stus);
/*指定排序规则*/
int compar(const void * x , const void * y)
{
return ((Stu*)x)->score > ((Stu*)y)->score;
}
/*屏幕暂停,直到接收下一个字符时*/
void contine(void)
{
while(getchar() != '\n');
getchar();
}
int main(int argc, char ** argv)
{
//结构体数组的定义和初始化
Stu stus[20];
memset(stus, 0, sizeof(stus));
int choice = 0;
//界面交互设置
while(1){
system("clear"); //每次运行时,清屏
//菜单打印
printf("1、添加学生\n");
printf("2、打印所有学生\n");
printf("3、按姓名查找学生\n");
printf("4、按ID修改学生信息\n");
printf("5、按ID删除学生信息\n");
printf("6、对学生进行排序\n");
printf("0、退出\n");
printf("请选择你要做的操作: ");
//switch-case控制菜单交互
if(!scanf("%d", &choice)){
//缓存清理,继续下一次
while(getchar() != '\n');
continue;
}
switch(choice){
case 1: //添加
if(addStudent(stus, 20)){
printf("添加学生成功!\n");
}else{
printf("添加学生失败!\n");
}
//暂停
contine();
break;
case 2: //打印
printAllStudent(stus);
break;
case 3: //查找
findStudentByName(stus);
break;
case 4: //修改
if(modifyScoreById(stus)){
printf("修改成功!\n");
}else{
printf("修改失败!\n");
}
break;
case 5: //删除
if(deleteStudentById(stus)){
printf("删除成功!\n");
}else{
printf("删除失败!\n");
}
break;
case 6: //排序
qsort(stus, count, sizeof(Stu), compar);
printAllStudent(stus);
//sortStudentByScore(stus);
break;
case 0:
return 1; //结束主函数,程序结束
default:
break;
}
}
return 0;
}
/*添加一个学生,成功返回1 失败返回0*/
int addStudent(Stu* stus, int len)
{
assert(stus);
if(count >= len){
return 0; //数组容量满
}
//输入学生信息
Stu stu;
memset(&stu, 0, sizeof(stu));
printf("请输入学生ID、姓名、分数: ");
scanf("%d %s %lf", &(stu.id), stu.name, &(stu.score));
//检查ID是否重复
if(!checkID(stus, stu.id)){ //检查是否重复,如果已经存在,返回1,反之返回0
stus[count] = stu; //添加到数组中
count++; //学生计数+1
return 1;
}
return 0; //ID重复,添加失败
}
/*检查学生ID是否重复,已经存在返回1 反之返回0*/
int checkID(Stu*stus, int id)
{
if(count == 0){
return 0;
}
//遍历数组查找是否存在相同的ID
int i = 0;
for(i = 0; i < count; i++){
if(id == stus[i].id){
return 1;
}
}
return 0;
}
/*打印所有学生的信息*/
void printAllStudent(Stu* stus)
{
assert(stus);
if(0 == count){
return ;
}
printf("学号\t\t姓名\t\t分数\n");
int i = 0;
for(i = 0; i < count; i++){
printf("%d\t\t%s\t\t%.2lf\n", stus[i].id, stus[i].name, stus[i].score);
}
}
/*按姓名查找学生信息*/
void findStudentByName(Stu* stus)
{
assert(stus);
if(0 == count){
return ;
}
//遍历数组匹配要查找的学生姓名
char name[20];
memset(name, 0, sizeof(name));
printf("请输入要查找的学生姓名: ");
scanf("%s", name);
int i = 0;
printf("学号\t\t姓名\t\t分数\n");
int flag = 0; //是否至少有一个匹配
for(i = 0; i < count; i++){
//找到该学生信息
if(!strcmp(name, stus[i].name)){
printf("%d\t\t%s\t\t%.2lf\n", stus[i].id, stus[i].name, stus[i].score);
flag++;
}
}
if(!flag){
printf("你查找的学生不存在!\n");
}
}
/*通过学生的ID修改学生的分数,成功返回1 失败返回0*/
int modifyScoreById(Stu* stus)
{
assert(stus);
if(0 == count){
return 0;
}
int id = 0;
printf("请输入需要修改的学生ID: ");
scanf("%d", &id);
//没有该学生
if(!checkID(stus, id)){
return 0;
}
double score = 0.0;
printf("请输入要修改的分数:");
scanf("%lf", &score);
int i = 0;
for(i = 0; i < count; i++){
if(stus[i].id == id){
stus[i].score = score;
break;
}
}
return 1;
}
/*通过ID删除指定的学生信息,成功返回1 失败返回0*/
int deleteStudentById(Stu *stus)
{
assert(stus);
if(0 == count){
return 0;
}
int id = 0;
printf("请输入学生的ID:");
scanf("%d", &id);
//没有当前学生存在
if(!checkID(stus, id)){
return 0;
}
int i = 0;
int j = 0;
for(i = 0; i < count; i++){
//找到学生并删除
if(stus[i].id == id){
//删除
for(j = i; j < count - 1; j++){
stus[j] = stus[j + 1];
}
count--; //学生有效个数-1
break;
}
}
return 1;
}
/*通过分数对学生生进升序排序*/
void sortStudentByScore(Stu* stus)
{
assert(stus);
if(0 == count || 1 == count){
return ;
}
int i = 0;
int j = 0;
for(i = 0; i < count - 1; i++){
for(j = 0; j < count - i- 1; j++){
if(stus[j].score > stus[j + 1].score){
//交换两个结构体变量
Stu tmp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = tmp;
}
}
}
//打印排序后的结果
printAllStudent(stus);
}
简化版学生管理系统,主要思路
最新推荐文章于 2022-07-05 14:02:24 发布