改进一下代码(换一种方式达到相同目的)

#include
#include
using namespace std;
void Input_record();
void final_sorce_and_average(int* p, int* q, int n);
void Sort_descend_ord_by_score(int* p, int* q, int n);
void Sort_ascend_ord_by_score(int* p, int* q, int n);
void Sort_ascend_ord_by_number(int* p, int* q, int n);
void query_rank_and_score_by_number(int* p, int* q, int n);
void precentage(int* p, int* q, int n);
void print_number_score(int* p, int* q, int n);
void choice_number();
int arr[30], a[30];//arr数组代表学生的学号,a数组代表学生的成绩
float n;
int main()
{
cout << "input student number(n < 30):" << endl;
cin >> n;//n代表学生人数
while (1)
{
choice_number();
}
}
void choice_number()
{
int* p, * q;
p = arr;
q = a;
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.Search by number " << endl;
cout << " 7.Statistic " << endl;
cout << " 8.List record " << endl;
cout << " 0.Exit " << endl;
cout << "please input your choice:" << endl;
char ch;
cin >> ch;
switch (ch)
{
case '1':
Input_record();
break;
case '2':
final_sorce_and_average(arr, a, n);
break;
case '3':
Sort_descend_ord_by_score(arr, a, n);
break;
case '4':
Sort_ascend_ord_by_score(arr, a, n);
break;
case '5':
Sort_ascend_ord_by_number(arr, a, n);
break;
case '6':
query_rank_and_score_by_number(arr, a, n);
break;
case '7':
precentage(arr, a, n);
break;
case '8':
print_number_score(arr, a, n);
break;
default:
cout << "Input error!" << endl;
break;
case '0':
cout << "end of program!" << endl;
break;
}
}
void Input_record()
{
cout << "Input student's ID and score:" << endl;
for (int i = 0; i < n; i++)
{
cout << "请输入第" << i+1 << "个学生的信息" << endl;
cin >> arr[i];//输入每个学生的学号
cin >> a[i];//输入每个学生的成绩
}
}
void Sort_descend_ord_by_score(int* p, int* q, int n)
{
cout << "Sort in ascending order by score:" << endl;
int max;
for (int i = 0; i < n; i++)
{
max = i;
for (int j = i + 1; j < n; j++)
{
if (*(q + max) < *(q + j)) max = j;
}
if (max != i)
{
int temp;
temp = *(q + i);
*(q + i) = *(q + max);
*(q + max) = temp;
int x;
x = *(p + i);
*(p + i) = *(p + max);
*(p + max) = x;
}
}
for (int i = 0; i < n; i++)
{
cout << (p + i) << " " << (q + i) << endl;
}
}
void final_sorce_and_average(int
p, int
q, int n)
{
int sum = 0;
double aver = 0;
for (int i = 0; i < n; i++)
{
sum += *(q + i);
}
aver = (double)sum / n;
cout << "sum=" << sum << "," << fixed << setprecision(2) << "aver=" << aver << endl;
}

void Sort_ascend_ord_by_score(int* p, int* q, int n)
{
cout << "Sort in ascending order by score:" << endl;
int min;
for (int i = 0; i < n; i++)
{
min = i;
for (int j = i + 1; j < n; j++)
{
if (*(q + min) > *(q + j)) min = j;
}
if (min != i)
{
int temp;
temp = *(q + i);
*(q + i) = *(q + min);
*(q + min) = temp;
int x;
x = *(p + i);
*(p + i) = *(p + min);
*(p + min) = x;
}
}
for (int i = 0; i < n; i++)
{
cout << *(p + i) << " " << *(q + i) << endl;
}
}
void Sort_ascend_ord_by_number(int* p, int* q, int n)
{
cout << "Sort in ascendind order by number:" << endl;
int min;
for (int i = 0; i < n; i++)
{
min = i;
for (int j = i + 1; j < n; j++)
{
if (*(p + min) > *(p + j)) min = j;
}
if (min != i)
{
int temp;
temp = *(q + i);
*(q + i) = *(q + min);
*(q + min) = temp;
int x;
x = *(p + i);
*(p + i) = *(p + min);
*(p + min) = x;
}
}
for (int i = 0; i < n; i++)
{
cout << *(p + i) << " " << *(q + i) << endl;
}
}
void query_rank_and_score_by_number(int* p, int* q, int n)
{
int x;
cout << "input the number you want to search:" << endl;
cin >> x;
for (int i = 0; i < n; i++)
{
if (x == *(p + i))
{
cout << *(p + i) << " " << *(q + i) << endl;
}
}
}
void precentage(int* p, int* q, int n)
{
int count[100] = { 0 };
float percentage[100] = { 0 };
for (int i = 0; i < n; i++)
{
if (*(q + i) >= 90 && *(q + i) <= 100)
{
count[i]++;
percentage[i] = 100 * count[i] / n;
}
else if (*(q + i) >= 80 && *(q + i) <= 89)
{
count[i]++;
percentage[i] = 100 * count[i] / n;
}
else if (*(q + i) >= 70 && (q + i) <= 79)
{
count[i]++;
percentage[i] = 100 * count[i] / n;
}
else if (*(q + i) >= 60 && (q + i) <= 69)
{
count[i]++;
percentage[i] = 100 * count[i] / n;
}
else
{
count[i]++;
percentage[i] = 100 * count[i] / n;
}
}
for (int i = 0; i < n; i++)
{
if (i == 0) cout << "<60" << " " << count[i] << " " << percentage[i] << "%" << endl;
else if (i == 1) cout << "60-69" << " " << count[i] << " " << percentage[i] << "%" << endl;
else if (i == 2) cout << "70-79" << " " << count[i] << " " << percentage[i] << "%" << endl;
else if (i == 3) cout << "80-89" << " " << count[i] << " " << percentage[i] << "%" << endl;
else if (i == 4) cout << "90-99" << " " << count[i] << " " << percentage[i] << "%" << endl;
else if (i == 5) cout << " 100 " << " " << count[i] << " " << percentage[i] << "%" << endl;
}
}
void print_number_score(int
p, int
q, int n)
{
for (int i = 0; i < n; i++)
{
cout << *(p + i) << " " << *(q + i) << endl;
}
}

把学号和成绩放在一个结构体中,用结构体数组保存所有数据就可以了:
struct Student
{
int id;//学号
int score; //成绩
};

struct Student stu[30]; //stu[30]替代原来的arr[30]和a[30]就可以了
函数中,涉及到arr和a的都用stu替换了就是了