- 按以下菜单界面编写学生信息管理系统;
1)录入学生信息首先输入学生人数,然后根据学生人数开辟动态数组;
2)学生信息包括学号、姓名、性别、三门课成绩、总分;其中学号、姓名、
性别、三门课成绩是需要从键盘录入的,而总分是由程序计算的,而不
是用户自己算了再输入。
3)显示结果(学生信息)要以列表的形式显示,注意美观性;
4)要有必要的容错措施,比如输入各科分数要在0-100之间,输入错误给
用户提示,让其重新输入;
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<Windows.h>
#include<assert.h>
using namespace std;
typedef struct student
{
char ID[15];
char name[15];
char sex[5];
int score[3];
int totalscore;
}student;
typedef struct Data
{
student* data;
int size;
int capacity;
}Data;
enum Option
{
EXIT,//0
ADD,
PRINT,
SEARCH_BY_ID,
SEARCH_BY_NAME,
SORT,
FIND_BY_SCORE
};
void menu()
{
cout << "\n*************************\n";
cout << "1.录入学生信息\n";
cout << "2.显示学生信息\n";
cout << "3.按学号查找\n";
cout << "4.按姓名查找\n";
cout << "5.按总分递减排序\n";
cout << "6.查找有2科目不及格的学生信息\n";
cout << "0.退出\n";
cout << "*************************\n";
cout << "请输入功能号0-6:";
}
void Init(Data* pdata)
{
assert(pdata);
cout << "输入学生人数:" << endl;
cin >> pdata->capacity;
pdata->data = (student*)malloc(sizeof(student) * (pdata->capacity));
pdata->size = 0;
for (int i = 0; i < pdata->capacity; i++)
{
pdata->data[i].totalscore = 0;
}
}
void fun1(Data*pdata)
{
int flag = 0;
if (pdata->size==pdata->capacity)
{
cout << "容量不够,无法输入" << endl;
return;
}
cout << "输入学号:";
scanf("%s",pdata->data[pdata->size].ID);
cout << "输入姓名:";
scanf("%s", pdata->data[pdata->size].name);
cout << "输入性别:";
scanf("%s", pdata->data[pdata->size].sex);
do {
cout << "输入三科成绩:";
for (int i = 0; i < 3; i++)
{
scanf("%d", &pdata->data[pdata->size].score[i]);
if (pdata->data[pdata->size].score[i] > 100 || pdata->data[pdata->size].score[i] < 0)
{
flag = 1;
}//如果输入成绩不合法
pdata->data[pdata->size].totalscore += pdata->data[pdata->size].score[i];
}
if (flag == 1)
{
cout << "成绩输入不合规,重新输入" << endl;
}
} while (flag != 0);
cout << "信息输入成功" << endl;
pdata->size++;
}
void fun2(Data* pdata)//打印信息
{
assert(pdata);
printf("%-10s%-10s%-5s%-12s%-5s\n", "学号", "姓名", "性别", "成绩", "总成绩");
for (int i = 0; i < pdata->size; i++)
{
printf("%-10s%-10s%-5s%-4d%-4d%-4d%-5d\n",
pdata->data[i].ID,
pdata->data[i].name,
pdata->data[i].sex,
pdata->data[i].score[0],
pdata->data[i].score[1],
pdata->data[i].score[2],
pdata->data[i].totalscore);
}
}
void fun3(Data*pdata)
{
cout << "\n请输入要查找的学号:\n";
char IDinput[15]="\0";
cin >> IDinput;
for (int i = 0; i < pdata->size; i++)
{
if (strcmp(pdata->data[i].ID, IDinput) == 0)
{
printf("%-10s%-10s%-5s%-12s%-5s\n", "学号", "姓名", "性别", "成绩", "总成绩");
printf("%-10s%-10s%-5s%-4d%-4d%-4d%-5d\n",
pdata->data[i].ID,
pdata->data[i].name,
pdata->data[i].sex,
pdata->data[i].score[0],
pdata->data[i].score[1],
pdata->data[i].score[2],
pdata->data[i].totalscore);
break;
}
}
}
void fun4(Data*pdata)
{
cout << "\n请输入要查找的姓名:\n";
char nameinput[15] = "\0";
cin >> nameinput;
printf("%-10s%-10s%-5s%-12s%-5s\n", "学号", "姓名", "性别", "成绩", "总成绩");
for (int i = 0; i < pdata->size; i++)
{
if (strcmp(pdata->data[i].name, nameinput) == 0)
{
printf("%-10s%-10s%-5s%-4d%-4d%-4d%-5d\n",
pdata->data[i].ID,
pdata->data[i].name,
pdata->data[i].sex,
pdata->data[i].score[0],
pdata->data[i].score[1],
pdata->data[i].score[2],
pdata->data[i].totalscore);
}
}
}
int compare(const void* a, const void* b)
{
return ((student*)b)->totalscore - ((student*)a)->totalscore;
}
void fun5(Data*pdata)//排序
{
qsort(pdata->data, pdata->size, sizeof(student), compare);
fun2(pdata);
}
void fun6(Data* pdata)
{
for (int i = 0; i < pdata->size; i++)
{
int flag = 0;
for (int j = 0; j < 3; j++)
{
if (pdata->data[i].score[j] < 60)
{
flag++;
}
}
if (flag >= 2)
{
printf("%-10s%-10s%-5s%-4d%-4d%-4d%-5d\n",
pdata->data[i].ID,
pdata->data[i].name,
pdata->data[i].sex,
pdata->data[i].score[0],
pdata->data[i].score[1],
pdata->data[i].score[2],
pdata->data[i].totalscore);
}
}
}
int main()
{
int input = 0;
Data data;
Init(&data);
do
{
menu();
printf("请输入你的选择:>");
scanf("%d", &input);
system("cls");
switch (input)
{
case ADD:
fun1(&data);
break;
case PRINT:
fun2(&data);
break;
case SEARCH_BY_ID:
fun3(&data);
break;
case SEARCH_BY_NAME:
fun4(&data);
break;
case SORT:
fun5(&data);
break;
case FIND_BY_SCORE:
fun6(&data);
break;
case EXIT:
fun2(&data);
printf("退出通讯录\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
测试用例可以自己简单测一测