用结构体数组实现学生信息管理系统
student.h
#ifndef __STIDENT_H__
#define __STIDENT_H__
#define N 20
//学生信息结构体
typedef struct stu{
int id;
char name[32];
int score;
}stu_t;
//班级整体信息结构体
typedef struct Class{
stu_t person[N]; //班级人数结构体
int count; //添加的班级人数
}cla_t;
//函数的声明
void print_menu();
void insert_student(cla_t *my_class);
void modify_student(cla_t *my_class);
void search_student(cla_t *my_class);
void delete_student(cla_t *my_class);
void sort_student(cla_t *my_class);
void print_student(cla_t *my_class);
#endif
student.c
#include "student.h"
#include <stdio.h>
#include <string.h>
void print_menu()
{
printf("-----------------------\n");
printf("| 1.添加 2.修改 3.查询 |\n");
printf("| 4.删除 5.排序 6.退出 |\n");
printf("-----------------------\n");
printf("(请输入你的选择)\n");
}
void insert_student(cla_t *my_class)
{
if (N == my_class->count)
{
printf("班级人数已满,添加失败.\n");
return;
}
printf("请输入学生的(id name score)--以空格间隔\n");
int input_id = 0;
char input_name[32];
int input_score = 0;
scanf("%d%s%d", &input_id, input_name, &input_score);
// 执行添加的操作
my_class->person[my_class->count].id = input_id;
strcpy(my_class->person[my_class->count].name, input_name);
my_class->person[my_class->count].score = input_score;
my_class->count++;
printf("学生信息添加成功\n");
}
void modify_student(cla_t *my_class)
{
printf("请输入要修改的学员的id:\n");
int input_id = 0;
scanf("%d", &input_id);
// 遍历结构体数组 确定是否有要修改的学员
int i = 0;
for (i = 0; i < my_class->count; i++)
{
if (input_id == my_class->person[i].id)
{
// 找到了 执行修改
printf("请输入新的name和score:--以空格间隔\n");
scanf("%s%d", my_class->person[i].name,
&(my_class->person[i].score));
break;
}
}
if (i == my_class->count)
{
printf("查无此人...\n");
}
}
//根据id查找
void search_student(cla_t *my_class)
{
printf("请输入要查询的学生信息的id:\n");
int input_id = 0;
scanf("%d", &input_id);
// 遍历结构体数组 确定是否有查找的学生.
int i = 0;
for (i = 0; i < my_class->count; i++)
{
if(input_id == my_class->person[i].id)
{
//找到了
printf("%-6d%-10s%-4d\n",my_class->person[i].id,\
my_class->person[i].name,my_class->person[i].score);
break;
}
}
if (i == my_class->count)
{
printf("查无此人...\n");
}
}
//根据id删除
void delete_student(cla_t *my_class)
{
printf("请输入要删除的学员的id:\n");
int input_id = 0;
scanf("%d", &input_id);
//遍历结构体数组 找到待删除的元素
int i = 0;
int j = 0;
for(i = 0; i < my_class->count; i++){
if(input_id == my_class->person[i].id){
#if 1
for(j = i+1; j < my_class->count; j++){
//结构体变量可以直接相互赋值
my_class->person[j-1] = my_class->person[j];
}
#else
for(j = i; j < my_class->count-1; j++){
//结构体变量可以直接相互赋值
my_class->person[j] = my_class->person[j+1];
}
#endif
my_class->count--;
printf("删除成功\n");
return;
}
}
if (i == my_class->count)
{
printf("查无此人...\n");
}
}
//对学生的成绩排序 从大到小
void sort_student(cla_t *my_class){
int i = 0;
int j = 0;
stu_t temp;
for(i = 0; i < my_class->count-1; i++){
for(j = 0; j < my_class->count-1-i; j++){
if(my_class->person[j].score < my_class->person[j+1].score){
temp = my_class->person[j];
my_class->person[j] = my_class->person[j+1];
my_class->person[j+1] = temp;
}
}
}
printf("排序完成\n");
print_student(my_class);
}
void print_student(cla_t *my_class)
{
printf("\n");
int i = 0;
for(i = 0; i < my_class->count; i++){
printf("%-6d%-10s%-4d\n", my_class->person[i].id, \
my_class->person[i].name, my_class->person[i].score);
}
printf("\n");
}
main.c
#include <stdio.h>
#include "student.h"
#include <string.h>
int main(int argc, char const *argv[])
{
printf("欢迎使用学生信息管理系统\n");
//定义班级信息结构体
cla_t my_class;
memset(&my_class,0,sizeof(my_class));
int choose = 0;
while(1){
print_menu();//打印菜单
scanf("%d", &choose);
switch(choose){
case 1:
insert_student(&my_class);
break;
case 2:
modify_student(&my_class);
break;
case 3:
search_student(&my_class);
break;
case 4:
delete_student(&my_class);
break;
case 5:
sort_student(&my_class);
break;
}
if(6 == choose)
break;
//print_student(&my_class);
}
printf("欢迎下次使用\n");
return 0;
}