+-
学生信息管理系统(c语言简版)——章节实验作业
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define MaxSize 50
typedef struct {
char number[20];
char name[20];
int math;
int computer;
int ad_math;
int average;
}Student;
typedef struct
{
Student student[MaxSize];
int length;
}SqList;
void Show() //菜单
{
printf("**********************************\n");
printf(" WELCOME!\n");
printf("\n");
printf(" 新科院学生信息管理系统\n\n");
printf(" \t 1.查询\t 2.插入\n\n");
printf(" \t 3.删除\t 4.打印\n\n");
printf(" \t 5.排序\t 6.退出\n\n");
printf(".............7.录入.............\n");
printf("\n");
printf(" 输入相关序号实现相关功能!\n");
printf("**********************************\n");
}
void InitList(SqList*& L) //初始化
{
L = (SqList*)malloc(sizeof(SqList));
L->length = 0;
}
void CreateList(SqList*&L) //录入信息
{
int i = 0, n;
printf("请输入录入学生人数:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("姓名:");
scanf("%s", &L->student[i].name);
getchar(); //吸收换行符
printf("学号:");
scanf("%s", &L->student[i].number);
getchar();
printf("数学成绩,计算机成绩,高数成绩:\n");
scanf("%d%d%d", &L->student[i].math, &L->student[i].computer, &L->student[i].ad_math);
L->student[i].average = (L->student[i].ad_math + L->student[i].computer + L->student[i].math) / 3; //平均成绩
getchar(); //吸收换行符
if (i == n - 1) //强制跳出循环
break;
}
L->length = n;
printf("录入成功!");
Sleep(3000);
}
void DispList(SqList*&L) //打印
{
int i, flag;
if (L->length==0) //判空
{
printf("抱歉!无数据,请录入!");
Sleep(3000);
return;
}
while (1) {
for (i = 0; i < L->length; i++)
{
printf("姓名:%s\t", L->student[i].name);
printf("学号:%s\n", L->student[i].number);
printf("数学成绩:%d\t", L->student[i].math);
printf("计算机成绩:%d\t", L->student[i].computer);
printf("高数成绩:%d\t", L->student[i].ad_math);
printf("平均成绩:%d\n", L->student[i].average);
printf("............................\n");
}
printf("输入0退出打印!\n");
scanf("%d", &flag);
if (flag == 0) //跳出打印结果显示
break;
}
}
void SeekElem(SqList* L) //查询
{
int i = 0, flag;
char num[MaxSize];
printf("请输入要查找学生信息的学号:\n");
scanf("%s", num);
while (1) {
while (i < L->length && strcmp(num, L->student[i].number) != 0)
{
i++;
}
if (strcmp(num, L->student[i].number) == 0)
{
printf("姓名:%s\t", L->student[i].name);
printf("学号:%s\n", L->student[i].number);
printf("数学成绩:%d\t", L->student[i].math);
printf("计算机成绩:%d\t", L->student[i].computer);
printf("高数成绩:%d\t", L->student[i].ad_math);
printf("平均成绩:%d\n", L->student[i].average);
}
else
printf("查无此人!\n");
printf("输入0退出打印!\n");
scanf("%d", &flag);
if (flag == 0) //跳出打印结果显示
break;
}
}
void ListInsert(SqList*& L) //插入
{
int n, i, flag;
printf("请输入插入学生信息个数:");
scanf("%d", &n);
printf("请输入插入学生信息:\n");
for (i = 0; i < n; i++)
{
printf("姓名:");
scanf("%s", &L->student[L->length + i].name);
getchar(); //吸收换行符
printf("学号:");
scanf("%s", &L->student[L->length + i].number);
getchar();
printf("数学成绩,计算机成绩,高数成绩:\n");
scanf("%d%d%d", &L->student[L->length + i].math, &L->student[L->length + i].computer, &L->student[L->length + i].ad_math);
getchar(); //吸收换行符
L->student[i].average = (L->student[L->length + i].ad_math + L->student[L->length + i].computer + L->student[L->length + i].math) / 3; //平均成绩
}
L->length = L->length + n;
printf("\a");
printf("插入成功!");
Sleep(2000); //显示插入成功提示
}
void ListDelete(SqList*& L) //删除
{
int i, j;
char num[MaxSize];
printf("请输入要删除学生信息的学号:\n");
scanf("%s", num);
for (i = 0; i < L->length; i++)
{
if (strcmp(num, L->student[i].number) == 0)
{
for (j = i; j < L->length - 1; j++)
L->student[j] = L->student[j + 1];
L->length--;
printf("删除成功!");
Sleep(3000);
return; //删除成功,强制退出函数
}
}
printf("输入错误!\a");
Sleep(3000);
}
void Sort(SqList*&L) //排序
{
int i, j;
Student temp;
printf("按照平均成绩降序输出学生的信息如下:\n");
for (i=0; i<L->length-1; i++)
for (j = 0; j < L->length - i - 1; j++) //冒泡排序(降序排列)
{
if (L->student[j].average < L->student[j + 1].average)
{
temp = L->student[j];
L->student[j] = L->student[j+1];
L->student[j+1] = temp;
}
}
DispList(L);
}
int main()
{
int n;
SqList* L;
InitList(L);
while (1) {
system("cls"); //清屏函数
Show();
printf("请输入1-7的数:");
scanf("%d", &n);
switch (n) {
case 1:
SeekElem(L);
break;
case 2:
ListInsert(L);
break;
case 3:
ListDelete(L);
break;
case 4:
DispList(L);
break;
case 5:
Sort(L);
break;
case 6:
printf("........正在退出中........");
Sleep(3000);
system("cls");
printf("\a退出成功!"),
exit(0);
break;
case 7:
CreateList(L);
break;
default:
printf("\a输入错误!请重新输入(1—7)!"),
Sleep(2000);
break;
}
}
}