某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,参考第11周在线测验中“学生成绩管理系统V2.0”,用二维字符数组作函数参数编程实现如下菜单驱动的学生成绩管理系统:
(1)录入每个学生的学号、姓名和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按成绩由低到高排出名次表;
(5)按学号由小到大排出成绩表;
(6)按姓名的字典顺序排出成绩表;
(7)按学号查询学生排名及其考试成绩;
(8)按姓名查询学生排名及其考试成绩;
(9)按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比;
(10)输出每个学生的学号、姓名、考试成绩。
这里采用二维字符数组存储学生姓名,用另一个二维数组存储学生学号和成绩。
下面代码中应该有可以简化的步骤。
#include
#include
using namespace std;
void input(int xvehao_score[][2],char student[][4], int n);
void caculate(int xvehao_score[][2], int n);
void sort_in_descending_order_by_score(int xvehao_score[][2], char student[][4], int n);
void sort_in_ascending_order_by_score(int xvehao_score[][2], char student[][4], int n);
void sort_in_ascending_order_by_number(int xvehao_score[][2], char student[][4], int n);
void sort_in_dictionary_order_by_name(int xvehao_score[][2], char student[][4], int n);
void search_by_number(int xvehao_score[][2], char student[][4], int n);
void search_by_name(int xvehao_score[][2], char student[][4], int n);
void statistic(int xvehao_score[][2], int n);
int main()
{
int n;
cout << "Input student number(n<30):";
cin >> n;
int choice;
int xvehao_score[30][2];
char student[30][4];
while (1)
{
cout << endl;
cout << "Management for Students' scores" << endl;
cout << "1.Input record" << endl;
cout << "2.Caculate total and average score of course" << endl;
cout << "3.Sort in descending order by score" << endl;
cout << "4.Sort in ascending order by score" << endl;
cout << "5.Sort in ascending order by number" << endl;
cout << "6.Sort in dictionary order by name" << endl;
cout << "7.Search by number" << endl;
cout << "8.Search by name" << endl;
cout << "9.Statistic analysis" << endl;
cout << "10.List record" << endl;
cout << "0.Exit" << endl;
cout << "Please Input your choice:";
cin >> choice;
if (choice == 1)input(xvehao_score,student,n);
else if (choice == 2)caculate(xvehao_score, n);
else if (choice == 3)sort_in_descending_order_by_score(xvehao_score, student, n);
else if (choice == 4)sort_in_ascending_order_by_score(xvehao_score, student, n);
else if (choice == 5)sort_in_ascending_order_by_number(xvehao_score, student, n);
else if (choice == 6)sort_in_dictionary_order_by_name(xvehao_score, student, n);
else if (choice == 7)search_by_number(xvehao_score, student, n);
else if (choice == 8)search_by_name(xvehao_score, student, n);
else if (choice == 9)statistic(xvehao_score, n);
else if (choice == 10)sort_in_ascending_order_by_number(xvehao_score, student, n);
else if (choice == 0)break;
else
{
cout << "Input error!" << endl;
cout << "Please input any number from 0 to 10!"<> xvehao_score[i][0];
cin >> student[i];
cin >> xvehao_score[i][1];
}
}
void caculate(int xvehao_score[][2], int n)
{
float sum=0,aver;
int i;
for (i = 1; i <= n; i++)
{
sum += xvehao_score[i][1];
}
aver = sum / n;
cout << "sum=" << sum << " , aver=" << fixed << setprecision(2) << aver;
cout << endl;
}
void sort_in_descending_order_by_score(int xvehao_score[][2], char student[][4], int n)
{
int i, j,k,t;
for (i = 1; i <= n; i++)
{
k = i;
for (j = i + 1; j <= n; j++)
{
if (xvehao_score[k][1] < xvehao_score[j][1])k = j;
}
for (t = 0; t <= 1; t++)
{
int change = xvehao_score[i][t];
xvehao_score[i][t] = xvehao_score[k][t];
xvehao_score[k][t] = change;
}
char a;
for (t = 0; t < 4; t++)
{
a = student[i][t];
student[i][t] = student[k][t];
student[k][t] = a;
}
}
for (i = 1; i <= n; i++)
{
cout << xvehao_score[i][0] << " ";
for (j = 0; j < 4; j++)
{
cout << student[i][j];
}
cout << " " << xvehao_score[i][1];
cout << endl;
}
}
void sort_in_ascending_order_by_score(int xvehao_score[][2], char student[][4], int n)
{
int i, j, k, t;
for (i = 1; i <= n; i++)
{
k = i;
for (j = i + 1; j <= n; j++)
{
if (xvehao_score[k][1] > xvehao_score[j][1])k = j;
}
for (t = 0; t <= 1; t++)
{
int change = xvehao_score[i][t];
xvehao_score[i][t] = xvehao_score[k][t];
xvehao_score[k][t] = change;
}
char a;
for (t = 0; t < 4; t++)
{
a = student[i][t];
student[i][t] = student[k][t];
student[k][t] = a;
}
}
for (i = 1; i <= n; i++)
{
cout << xvehao_score[i][0] << " ";
for (j = 0; j < 4; j++)
{
cout << student[i][j];
}
cout << " " << xvehao_score[i][1];
cout << endl;
}
}
void sort_in_ascending_order_by_number(int xvehao_score[][2], char student[][4], int n)
{
int i, j, k, t;
for (i = 1; i <= n; i++)
{
k = i;
for (j = i + 1; j <= n; j++)
{
if (xvehao_score[k][0] > xvehao_score[j][0])k = j;
}
for (t = 0; t <= 1; t++)
{
int change = xvehao_score[i][t];
xvehao_score[i][t] = xvehao_score[k][t];
xvehao_score[k][t] = change;
}
char a;
for (t = 0; t < 4; t++)
{
a = student[i][t];
student[i][t] = student[k][t];
student[k][t] = a;
}
}
for (i = 1; i <= n; i++)
{
cout << xvehao_score[i][0] << " ";
for (j = 0; j < 4; j++)
{
cout << student[i][j];
}
cout << " " << xvehao_score[i][1];
cout << endl;
}
}
void sort_in_dictionary_order_by_name(int xvehao_score[][2], char student[][4], int n)
{
int i, j, k, t;
for (i = 1; i <= n; i++)
{
k = i;
for (j = i + 1; j <= n; j++)
{
for (t = 0; t < 4; t++)
{
if (student[k][t] > student[j][t])
{
k = j; break;
}
else if (student[k][t] < student[j][t])break;
}
}
for (t = 0; t <= 1; t++)
{
int change = xvehao_score[i][t];
xvehao_score[i][t] = xvehao_score[k][t];
xvehao_score[k][t] = change;
}
char a;
for (t = 0; t < 4; t++)
{
a = student[i][t];
student[i][t] = student[k][t];
student[k][t] = a;
}
}
for (i = 1; i <= n; i++)
{
cout << xvehao_score[i][0] << " ";
for (j = 0; j < 4; j++)
{
cout << student[i][j];
}
cout<<" "<< xvehao_score[i][1];
cout << endl;
}
}
void search_by_number(int xvehao_score[][2], char student[][4], int n)
{
int i,k;
int number;
cin >> number;
for (i = 1; i <= n; i++)
{
if (xvehao_score[i][0] == number)
{
cout << xvehao_score[i][0] << " ";
for (int t = 0; t < 4; t++)
{
cout << student[i][t];
}
cout << " " << xvehao_score[i][1] << endl;
k = 1;
break;
}
else k = 0;
}
if (k == 0)cout << "Can't find this student!"<> name;
int k = 0;
for (int i = 1; i <= n; i++)
{
for (int t = 0; t < 4; t++)
{
if (name[t] == student[i][t])k = 1;
else
{
k = 0; break;
}
}
if (k == 1)
{
cout << xvehao_score[i][0] << " ";
for (int t = 0; t < 4; t++)
{
cout << student[i][t];
}
cout << " " << xvehao_score[i][1] << endl;
break;
}
}
if (k == 0)cout << "can't find this student!" << endl;
}
void statistic(int xvehao_score[][2], int n)
{
int i;
int A = 0, B = 0, C = 0, D = 0, E = 0, F = 0;
for (i = 1; i <= n; i++)
{
if (xvehao_score[i][1] == 100)A++;
else if (xvehao_score[i][1] >= 90 && xvehao_score[i][1] <= 99)B++;
else if (xvehao_score[i][1] >= 80 && xvehao_score[i][1] <= 89)C++;
else if (xvehao_score[i][1] >= 70 && xvehao_score[i][1] <= 79)D++;
else if (xvehao_score[i][1] >= 60 && xvehao_score[i][1] <= 69)E++;
else F++;
}
cout << "<60 " << F<< " " <