应用结构体数组或链表完成对学生成绩的管理
要求实现的功能包括:
1.学生信息录入:从键盘按学号顺序输入N名学生信息(姓名,学号或成绩)
2.学生信息输出:输出所存储学生的信息
3.学生信息排序:按照成绩从低到高的顺序对录入的学生信息排序
4.学生信息查询:按照输入的学号查找并输出对应学生的信息
5.学生信息插入:按给定位置将学生信息插入到指定位置
6.学生信息删除:删除给定学生的信息
7.学生成绩统计:按分数段统计学生信息
一、结构体数组
1.代码
#include<stdio.h>
#include<windows.h>
//美化输出颜色改变(因为想让输出好看点,就在网上学习了一下)
void color(short x)
{
if (x >= 0 && x <= 15)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
struct information
{
int number;
char name[200];
int score;
char grade;
}stu[1000],chuan;
//学生信息输出
void look(int pd,int *m)
{
if (pd)
{
for (int i = 0; i < *m; i++)
{
printf("number:%d name:%s score:%d\n",stu[i].number, stu[i].name, stu[i].score);
}
}
}
//学生信息排序
void sort(int *m)
{
int pd;
for (int i = 0; i < *m; i++)
{
for (int j = i + 1; j < *m; j++)
{
if (stu[i].score > stu[j].score)
{
chuan = stu[i]; stu[i] = stu[j]; stu[j] = chuan;
}
}
}
printf("If you want to see the sorted information?(1(Yes)/0(No))\n");
color(7);
scanf("%d", &pd);
color(3);
look(pd,m);
}
//学生信息查询
void quest(int *m)
{
int num,pd=1;
printf("Please input number:\n");
color(7);
scanf("%d", &num);
color(3);
for (int i = 0; i < *m; i++)
{
if (stu[i].number == num)
{
pd = 0;
printf("number:%d name:%s score:%d\n", stu[i].number, stu[i].name, stu[i].score);
break;
}
}
if (pd) printf("Sorry,there is no information about this student!\n");
}
//学生信息插入
void insert(int* m)
{
int pd = 1,num;
printf("Please input the position number you want to insert:\n");
color(7);
scanf("%d", &num);
color(3);
printf("Please input the student information:\n");
color(7);
scanf("%d %s %d", &stu[*m].number, stu[*m].name, &stu[*m].score);
color(3);
for (int i = *m; i >0; i--)
{
if (i != num)
{
chuan = stu[i]; stu[i] = stu[i - 1]; stu[i - 1] = chuan;
}
}
*m+=1;
printf("If you want to see the information after inserting?(1(Yes)/0(No))\n");
color(7);
scanf("%d", &pd);
color(3);
look(pd, m);
}
//学生信息删除
void delet(int *m)
{
int num,pd;
printf("Please input the student number:\n");
color(7);
scanf("%d", &num);
color(3);
for (int i = 0; i < *m; i++)
{
if (stu[i].number == num)
{
for (int j = i; j < *m; j++)
{
stu[j] = stu[j + 1];
}
}
}
*m -= 1;
printf("If you want to see the information after inserting?(1(Yes)/0(No))\n");
color(7);
scanf("%d", &pd);
color(3);
look(pd, m);
}
//学生成绩统计
void statistics(int *m)
{
int pd=0; char ch;
for (int i = 0; i < *m; i++)
{
switch (stu[i].score/10)
{
case(10):
case(9):stu[i].grade = 'A'; break;
case(8):stu[i].grade = 'B'; break;
case(7):stu[i].grade = 'C'; break;
case(6):stu[i].grade = 'D'; break;
dafault:stu[i].grade = 'E';
}
}
printf("If you want to see the score grade?(1(Yes)/0(No))\n");
color(7);
scanf("%d", &pd);
color(3);
if (pd)
{
printf("Please input the grade that you want:(A/B/C/D/E)\n");
getchar();
scanf("%c",&ch);
for (int i = 0; i < *m; i++)
{
if (stu[i].grade == ch)
{
printf("%d %s %d %c\n", stu[i].number, stu[i].name, stu[i].score, stu[i].grade);
}
}
}
}
//主函数
int main(void)
{
int n;
char choice;
color(3);
printf("Welcome to the student grade management!\n");
printf("How many students do you want to record?\n");
color(7);
scanf("%d", &n);
color(3);
printf("Please input the student information:\n");
color(7);
for (int i = 0; i < n; i++)
{
scanf("%d %s %d", &stu[i].number, stu[i].name, &stu[i].score);
}
color(14);
printf(">>>>>>>>>*<<<<<<<<\n");
printf("> 学生信息输出:P <\n> 学生信息排序:S <\n> 学生信息查询:Q <\n> 学生信息插入:I <\n> 学生信息删除:D <\n> 学生成绩统计:T <\n> 需要退出系统:E <\n");
printf(">>>>>>>>>*<<<<<<<<\n");
color(3);
while (1)
{
printf("Please input your choice:\n");
getchar();
color(7);
scanf("%c", &choice);
color(3);
if (choice == 'P')
{
look(1,&n);
}
if (choice == 'S')
{
sort(&n);
}
if (choice == 'Q')
{
quest(&n);
}
if (choice == 'I')
{
insert(&n);
}
if (choice == 'D')
{
delet(&n);
}
if (choice == 'T')
{
statistics(&n);
}
if (choice == 'E')
{
break;
}
}
}
2.设计思路
主要用到了函数、指针和结构体的知识
首先定义一个结构体数组(用于记录学生的信息)和结构体变量(在删除和插入学生信息时会用到)
将每一个选择的功能都写一个函数(将每个功能分成一小块,我觉得这样思路会更清晰一点)
因为在插入和删除时学生个数n会发生改变,用指针可以直接获取函数中改变的值,不需要返回值。
二、链表
(链表的代码还有点问题,可能晚点才会更)
如果你觉得以上对你有帮助,阔以点点左下角的👍吗?
♥就酱紫啦~byebye 大家一起努力,加油!!!