***************欢迎进入学生管理系统********************
**1.添加 2.删除 3.修改 4.查询 5.排序 6.退出************
*****************************************************
功能:实现学生信息的增、删、查、改、排序
主函数#include "stu.h"
#include
#include
int main(int argc, const char* argv[])
{
int chose;
// 1.定义出来班级
class_t cls;
// 2.初始化班级内学生的个数
cls.n = -1;
int ret = 0;
while (1) {
puts("***************欢迎进入学生管理系统********************");
puts("**1.添加 2.删除 3.修改 4.查询 5.排序 6.退出************");
puts("*****************************************************");
printf("input chose > ");
scanf("%d", &chose);
while ((getchar()) != 'n')
;
switch (chose) {
case ADDSTU:
MyAddStu(&cls);
break;
case DELSTU:
ret = MyDelStu(&cls);
printf("删除学生个数%dn", ret);
break;
case MODSTU:
MyModStu(&cls);
break;
case CHKSTU:
MyChkStu(&cls);
break;
case SOTSTU:
MySotStu(&cls);
break;
case QITSTU:
printf("退出学生管理系统....n");
return -1;
default:
printf("选择错误,请重新输入:");
continue;
}
}
return 0;
}
#include "stu.h"
void Input(stu_t* stu)
{
int ret = 0;
repoint1:
printf("请输入学生信息,name,sex,score:");
ret = scanf("%s %c %d", stu->name, &stu->sex, &stu->score);
if (3 != ret) {
printf("输入错误,请重新输入n");
while ((getchar() != 'n'))
;
goto repoint1;
}
while ((getchar() != 'n'))
;
if (('m' != stu->sex) && ('w' != stu->sex)) {
printf("性别输入错误,请重新输入n");
goto repoint1;
}
if ((stu->score < 0) && (stu->score > 100)) {
printf("分数输入错误,请重新输入n");
goto repoint1;
}
}
int MyAddStu(class_t* cls)
{
if (cls->n >= 29) {
printf("班级人数已满!n");
return -1;
}
Input(&cls->stu[++(cls->n)]);
return 0;
}
int MyDelStu(class_t* cls)
{
int i = 0;
int j = 0;
char name[20];
int num;
int k = cls->n;
repoint3:
printf("输入要删除学生信息的姓名:");
scanf("%s", name);
while ((getchar() != 'n'))
;
for (i = 0; i <= cls->n; i++) {
if (!strcmp(name, cls->stu[i].name)) {
printf("%-2d %-20s %-2c %-2dn", i, cls->stu[i].name, cls->stu[i].sex, cls->stu[i].score);
} else {
printf("没有该学生,请重新输入:");
goto repoint3;
}
}
printf("输入要删除的学生信息对应的编号,如果输入-1则全部删除:");
scanf("%d", &num);
while ((getchar() != 'n'))
;
if (-1 == num) {
for (i = 0; i <= cls->n; i++) {
if (strcmp(name, cls->stu[i].name)) {
cls->stu[j] = cls->stu[i];
j++;
}
}
cls->n = cls->n - i - j;
} else {
if (num == cls->n)
cls->n--;
else {
for (i = num; i < cls->n; i++) {
cls->stu[i] = cls->stu[i + 1];
}
cls->n--;
}
}
return (k - cls->n);
}
void MyModStu(class_t* cls)
{
char name[20];
int n = -1;
repoint2:
printf("请输入要修改学生的姓名:");
scanf("%s", name);
int ret;
for (int i = 0; i <= cls->n; i++) {
if ((ret = strcmp(name, cls->stu[i].name))) {
printf("没有该学生,请重新输入n");
goto repoint2;
} else {
n = i;
}
}
Input(&cls->stu[n]);
}
void MyChkStu(class_t* cls)
{
for (int i = 0; i <= cls->n; i++) {
printf("%-20s%-2c%-2dn", cls->stu[i].name, cls->stu[i].sex, cls->stu[i].score);
}
}
int MySotStu(class_t* cls)
{
int i, j;
stu_t stu1;
for (i = 0; i <= cls->n - 1; i++) {
for (j = 0; j <= cls->n - i - 1; j++) {
if (cls->stu[j].score > cls->stu[j + 1].score) {
stu1 = cls->stu[j];
cls->stu[j] = cls->stu[j + 1];
cls->stu[j + 1] = stu1;
}
}
}
return 0;
}
#ifndef __STU_H__
#define __STU_H__
#include
#include
enum choose {
ADDSTU = 1,
DELSTU,
MODSTU,
CHKSTU,
SOTSTU,
QITSTU,
};
typedef struct {
char name[20]; //姓名
char sex; //性别
int score; //分数
} stu_t;
typedef struct {
stu_t stu[30]; //班级内能容纳的学生的个数
int n; //班级内当前学生的个数
} class_t;
int MyAddStu(class_t* cls);
int MyDelStu();
void MyModStu();
void MyChkStu();
int MySotStu();
#endif



