当前位置:网站首页>C language: student management system based on sequence table, super detailed, all with notes, if you don't understand it after reading, please slap me.
C language: student management system based on sequence table, super detailed, all with notes, if you don't understand it after reading, please slap me.
2022-07-24 01:00:00 【lhb2998658795】
1. Basic function
1. add to 2. Delete 3. modify 4. lookup 5. Sort 6. Exhibition 7. sign out
2. Code implementation
2.1 The header file
#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; // Number of students in the current class
}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 Function block
#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 The return value of is the number of successful entries
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. Judge whether the students in the class are empty
if(cls->n < 0){
printf(" There are no students in the class \n");
return 0;
}
//2. Enter the name of the deleted student
printf("input delete stu name > ");
scanf("%s",name);
getchar();
//3. To delete
for(i=j=0;i<=cls->n;i++){
if(strcmp(name,cls->stu[i].name)){
cls->stu[j] = cls->stu[i];
j++;
}
}
//4. Reset n Value
cls->n = j-1;
printf(" Deleted %d A student \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. Find students and get the number of students found
i = find_stu(cls);
if(i==0){
printf(" Failed to find students , Please check and enter \n");
return ;
}
printf(" We found it %d A student \n",i);
retry:
printf(" Please output the student information to be modified > ");
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. Modify student information
printf(" Modifying %s Student information \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. Create a class
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("----------------- Welcome to student management system -----------------------");
puts("-----1. add to 2. Delete 3. modify 4. lookup 5. Sort 6. Exhibition 7. sign out -------");
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(" Exit the student management system \n");
loop=0;
break;
default:
printf(" Input error , Please reselect \n");
break;
}
}
//4. Free memory
if(cls != NULL){
free(cls);
cls=NULL;
}
return 0;
}边栏推荐
- Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
- freemarker
- 【数据挖掘工程师-笔试】2022年海尔 公司
- Database connection pool & dbutils
- 制作 .Img 镜像文件
- [the 83rd fortnight of leetcode]
- Use of crawler request library 2
- 【Flyway 介绍】
- JS related knowledge
- How to use SAP intelligent robotic process automation to automate Excel
猜你喜欢

Pbootcms database conversion tutorial (SQLite to MySQL detailed tutorial)

How to use mitmproxy to get data return in automated testing?

Image processing: Generation 3 × Window of 3

docker redis

Redis common commands

VLAN division, automatic allocation of IP to all hosts through DHCP, and communication accessible throughout the network

Basic exercises of C language for beginners

工作3年的测试员跳槽后工资是原来的2倍,秘诀原来是......

Starfish OS: create a new paradigm of the meta universe with reality as the link

Establishment of static route
随机推荐
URL query parameter encoding problem (golang)
【LeetCode第 83 场双周赛】
Programmeur de cheval noir - test d'interface - test d'interface d'apprentissage de quatre jours - jour 4 - Postman lit des fichiers de données externes, lit des données de fichiers de données, IHRM P
[STM32] basic knowledge of serial communication
QT入门篇(2.1初入QT的开始第一个程序)
【复盘】关于我在错误的时间选错了技术这件事
Redis | very important Middleware
Graphic pipeline (I) post-processing stage alpha test template test depth test mix
MySQL exercise: all employees reporting to the CEO
Dark horse programmer - interface test - four day learning interface test - day 4 - postman reads external data files, reads data files, IHRM project practice, employee management module, adds employe
freemarker
[QNX hypervisor 2.2 user manual]9.1 configuration variables
Understand the locks that can't
Implementation of singleton mode and prevention of reflection and serialization
Three usages of synchronized keywords in vernacular
[the 83rd fortnight of leetcode]
Create database table db.create in flask project_ all()
Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
T-seda code
Fpga:ov7725 camera displays images in rgb565 format through vga/hdmi