当前位置:网站首页>C语言:基于顺序表的学生管理系统,超级详细,全部都有注释,看完不懂来扇我。
C语言:基于顺序表的学生管理系统,超级详细,全部都有注释,看完不懂来扇我。
2022-07-23 05:44:00 【lhb2998658795】
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);
#endif2.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;
}边栏推荐
- 【Autosar 存储Stack NVM】
- 单片机学习笔记4--GPIO(基于百问网STM32F103系列教程)
- Importance of data analysis
- [distinguish the meaning and usage of constant pointer and pointer constants const int * and int * const]
- 二叉树基础oj练习-
- 单片机学习笔记8--按键和外部中断(基于百问网STM32F103系列教程)
- 博客搭建五:图床选择
- 【AUTOSAR之FEE(非易失存储器Flash与Eeprom区别)】
- SCI审稿过程中的几种状态
- 钢结构基本原理题库
猜你喜欢

高分子物理名词解释归纳

博客搭建四:将自己的博客加入百度和谷歌收录的方法

Data analysis of time series (III): decomposition of classical time series

Question bank of basic principles of steel structure

钢结构基本原理全面详细总结

博客搭建五:图床选择

Interpretation of the paper: the interpretability of the transformer model of functional genomics

5.4 Pyinstaller库安装与使用

Data analysis (II)
![[physical layer of CAN bus] 1. Content sharing of can/canfd sampling points](/img/e4/0b709a6ed5e639a75e0506f6eac9fd.png)
[physical layer of CAN bus] 1. Content sharing of can/canfd sampling points
随机推荐
高分子物理考研概念及要点、考点总结
嵌入式从入门到精通(入土)——超详细知识点分享2
高电压技术复习资料
Blog building 4: how to add your blog to Baidu and Google
Data analysis of time series (III): decomposition of classical time series
[AUTOSAR com 1. introduction to communication protocol stack]
二叉树的实现-c
How to establish data analysis thinking
Blog building five: drawing bed selection
[introduction to AUTOSAR com 4.com service layer module]
3.2daydayup举一反三:三天打鱼两天晒网式学习
Axure实现增删改查
单片机学习笔记9--串口通信(基于百问网STM32F103系列教程)
[AUTOSAR CP general 1. how to read AUTOSAR official documents]
常见的排序方法—选择排序
AWK 程序设计语言
Interpretation of the paper: "deep-4mcw2v: sequence based predictor for identifying N4 methylcytosine (4mc) sites in E. coli"
高电压技术重点知识整理
输入三角形边长,求面积
基于对象(Object Based)-两个经典类