要求:
(1)录入每个学生的学号和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按学号由小到大排出成绩表;
(5)按学号查询学生排名及其考试成绩;
(6)按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比;
(7)输出每个学生的学号、考试成绩。
#include<stdio.h>
int main()
{
int b;//接收每次选择回车字符
long choice; //输入的选择的输入学号
int countx[6] = { 0 };//记录个分段的人数,方便计算占比
int n, choice1, ret = 1;//n:学生人数。choice1:选择的功能。ret:判断输入学号是否正确。
int mid = 0, ex = 0;
long ID, ID_student[30] = { 0 };
float scoer, scoer_student[30] = { 0 };
float sum = 0, average = 0;
printf("Input student number(n<30):\n");
scanf("%d", &n);
do
{
printf("Management for Students' scores\n");
printf("1.Input record\n");
printf("2.Caculate total and average score of course\n");
printf("3.Sort in descending order by score\n");
printf("4.Sort in ascending order by number\n");
printf("5.Search by number\n");
printf("6.Statistic analysis\n");
printf("7.List record\n");
printf("0.Exit\n");
printf("Please Input your choice:\n");
scanf("%d", &choice1);
b = getchar();
switch (choice1)
{
case 1:
printf("Input student's ID, name and score:\n");
for (int i = 0; i < n; i++)
{
char a; //接收回车字符
scanf("%ld%f", &ID, &scoer);
a = getchar();
ID_student[i] = ID;
scoer_student[i] = scoer;
}
break;
case 2:
for (int j = 0; j < n; j++)
{
sum += scoer_student[j];
}
average = sum / n;
printf("sum=%.0f,aver=%.2f\n", sum, average);
break;
case 3:
for (int k = 0; k < n; k++)
{
float mid = scoer_student[k];
long ex = ID_student[k];
for (int j = k + 1; j < n; j++)
{
if (mid <= scoer_student[j])
{
scoer_student[k] = scoer_student[j];
scoer_student[j] = mid;
mid = scoer_student[k];
ID_student[k] = ID_student[j];
ID_student[j] = ex;
ex = ID_student[k];
}
}
}
printf("Sort in descending order by score:\n");
for (int j = 0; j < n; j++)
{
printf("%ld\t%.0f\n", ID_student[j], scoer_student[j]);
}
break;
case 4:
for (int l = 0; l < n; l++)
{
long mid2 = ID_student[l];
float scoerchange = scoer_student[l];
for (int m = l + 1; m < n; m++)
{
if (mid2 >= ID_student[m])
{
ID_student[l] = ID_student[m];
ID_student[m] = mid2;
mid2 = ID_student[l];
scoer_student[l] = scoer_student[m];
scoer_student[m] = scoerchange;
scoerchange = scoer_student[l];
}
}
}
printf("Sort in ascending order by number:\n");
for (int i = 0; i < n; i++)
{
printf("%ld\t%.0f\n", ID_student[i], scoer_student[i]);
}
break;
case 5:
printf("Input the number you want to search:\n");
scanf("%ld", &choice);
for (int i = 0; i < n; i++)
{
if (choice == ID_student[i])
{
printf("%ld\t%.0f\n", ID_student[i], scoer_student[i]);
ret++;
}
}
if (ret == 1)
printf("Not found!\n");
break;
case 6:
for (int i = 0; i < n; i++)
{
if (scoer_student[i] < 60 && scoer_student[i] >= 0)
{
countx[0]++;
}
if (scoer_student[i] >= 60 && scoer_student[i] < 70)
{
countx[1]++;
}
if (scoer_student[i] >= 70 && scoer_student[i] < 80)
{
countx[2]++;
}
if (scoer_student[i] >= 80 && scoer_student[i] < 90)
{
countx[3]++;
}
if (scoer_student[i] >= 90 && scoer_student[i] < 100)
{
countx[4]++;
}
if (scoer_student[i] == 100)
{
countx[5]++;
}
}
printf("<60\t%d\t%.2f%%\n", countx[0], (float)countx[0] * 100 / n);
printf("%d-%d\t%d\t%.2f%%\n", 60, 69, countx[1], (float)countx[1] * 100 / n);
printf("%d-%d\t%d\t%.2f%%\n", 70, 79, countx[2], (float)countx[2] * 100 / n);
printf("%d-%d\t%d\t%.2f%%\n", 80, 89, countx[3], (float)countx[3] * 100 / n);
printf("%d-%d\t%d\t%.2f%%\n", 90, 99, countx[4], (float)countx[4] * 100 / n);
printf("%d\t%d\t%.2f%%\n", 100, countx[5], (float)countx[5] * 100 / n);
break;
case 7:
for (int i = 0; i < n; i++)
{
printf("%ld\t%.0f\n", ID_student[i], scoer_student[i]);
}
break;
case 0:
printf("End of program!\n");
break;
default:
printf("Input error!\n");
break;
}
} while (choice1 != 0);
return 0;
}
测试:
?
?
?
此题有明显的各部分功能,所以还可采用函数来实现各部分的功能,这里就不写了........
———————————————————————————————————————————
注:编者水平有限,若有更好的方法欢迎在评论区提出讨论。
|