1.基本功能
1.添加 2.删除 3.修改 4.查找 5.排序 6.展示 7.退出
2.代码实现
2.1头文件
#ifndef __STU_H__
#define __STU_H__
#define MAX_STU 30
typedef struct{
char name[20];
char sex;
int score;
}stu_t;
typedef struct{
stu_t stu[MAX_STU];
int n; //当前班级内学生的个数
}class_t;
#define PRINT_ERR(msg) do{\
printf("%s",msg);\
while(getchar()!='\n');\
goto retry;\
}while(0)
#define ADD_STU 1
#define DEL_STU 2
#define MOD_STU 3
#define FIND_STU 4
#define SORT_STU 5
#define SHOW_STU 6
#define QUIT_STU 7
int add_stu(class_t *cls);
int delete_stu(class_t *cls);
void show_stu(class_t *cls);
int find_stu(class_t *cls);
void modify_stu(class_t *cls);
void sort_stu(class_t *cls);
#endif
2.2函数块
#include <stdio.h>
#include <string.h>
#include "stu.h"
static int num;
void input_stu(stu_t *stu)
{
int ret;
retry:
printf("input (name sex score) > ");
//scanf的返回值是输入成功的项目的个数
ret = scanf("%s %c %d",
stu->name,
&stu->sex,
&stu->score);
if(ret != 3)
PRINT_ERR("input iterm error,try again\n");
if((stu->sex != 'm') &&
(stu->sex != 'w'))
PRINT_ERR("input sex error,try again\n");
if((stu->score < 0) ||
(stu->score > 100))
PRINT_ERR("input score error,try again\n");
}
int add_stu(class_t *cls)
{
if((++cls->n) >= MAX_STU){
printf("class full,try again\n");
return -1;
}
input_stu(&cls->stu[cls->n]);
return 0;
}
int delete_stu(class_t *cls)
{
int i,j;
char name[20] = {0};
//1.判断班级内的学生是否是空
if(cls->n < 0){
printf("班级内没有学生\n");
return 0;
}
//2.输入删除学生的姓名
printf("input delete stu name > ");
scanf("%s",name);
getchar();
//3.开始删除
for(i=j=0;i<=cls->n;i++){
if(strcmp(name,cls->stu[i].name)){
cls->stu[j] = cls->stu[i];
j++;
}
}
//4.重置n的值
cls->n = j-1;
printf("删除了%d个学生\n",i-j);
return i-j;
}
void print_info(stu_t *stu)
{
printf("name = %-10s,sex = %c,score = %d\n",
stu->name,
stu->sex,
stu->score);
}
void show_stu(class_t *cls)
{
int i;
for(i=0;i<=cls->n;i++){
print_info(&cls->stu[i]);
}
}
int find_stu(class_t *cls)
{
int i,ret=0;
char name[20] = {0};
num=0;
printf("input stu name > ");
scanf("%s",name);
getchar();
for(i=0;i<=cls->n;i++){
if(strcmp(name,cls->stu[i].name)==0){
print_info(&cls->stu[i]);
num = num | 1<<i;
ret++;
}
}
return ret;
}
void modify_stu(class_t *cls)
{
int i,which,tmp=0,j;
//1.查找学并获取查找到的学生的个数
i = find_stu(cls);
if(i==0){
printf("查找学生失败,请查证后输入\n");
return ;
}
printf("共查找到了%d个学生\n",i);
retry:
printf("请输出要修改第几个学生信息 > ");
scanf("%d",&which);
getchar();
if(which <=0 || which >i){
printf("input error,try again\n");
goto retry;
}
for(j=0;j<=29;j++){
if(num>>j & 0x1){
tmp++;
if(tmp == which) break;
}
}
//2.修改学生信息
printf("正在修改%s学生的信息\n",cls->stu[j].name);
input_stu(&cls->stu[j]);
}
void sort_stu(class_t *cls)
{
stu_t tmp;
int i,j;
int flags=0;
for(i=0;i<cls->n;i++){
for(flags=j=0;j<cls->n-i;j++){
if(cls->stu[j].score>cls->stu[j+1].score){
tmp=cls->stu[j];
cls->stu[j] = cls->stu[j+1];
cls->stu[j+1] = tmp;
flags=1;
}
}
if(flags == 0) break;
}
}
3main.c
#include <stdio.h>
#include <stdlib.h>
#include "stu.h"
int main(int argc, const char *argv[])
{
int chose,loop=1;
//1.创建班级
class_t *cls =(class_t *) malloc(sizeof(class_t));
if(cls == NULL){
printf("malloc memory error\n");
return -1;
}
cls->n=-1;
while(loop){
puts("-----------------欢迎使用学生管理系统-----------------------");
puts("-----1.添加 2.删除 3.修改 4.查找 5.排序 6.展示 7.退出-------");
puts("------------------------------------------------------------");
printf("input chose > ");
scanf("%d",&chose);
getchar();
switch(chose){
case ADD_STU:
add_stu(cls);
break;
case DEL_STU:
delete_stu(cls);
break;
case MOD_STU:
modify_stu(cls);
break;
case FIND_STU:
find_stu(cls);
break;
case SORT_STU:
sort_stu(cls);
break;
case SHOW_STU:
show_stu(cls);
break;
case QUIT_STU:
printf("退出学生管理系统\n");
loop=0;
break;
default:
printf("输入错误,请重新选择\n");
break;
}
}
//4.释放内存
if(cls != NULL){
free(cls);
cls=NULL;
}
return 0;
}