- 老师答疑区
- 帖子详情
第十周 学生成绩管理系统 codeblock正常,复制过来显示结果错误
木一林
发表于2017年11月26日
<p><p><code class="brush:cpp;toolbar:false" >#include <stdio.h>
#define N 30
//这几个函数分别对应第二项到第六项的功能
void Function2(float *sum, float *aver, float score[], int number);
void Function3(long stuNum[], float score[], int number);
void Function4(long stuNum[], float score[], int number);
void Function5(long stuNum[], float score[], int number);
void Function6(long stuNum[], float score[], int number);
int main()
{
int number = 0;//number 为学生的人数
int result1 = 0;
do{
printf("Input student number(n<30):\n");
result1 = scanf("%d", &number);
}while(result1 != 1 || number <= 0 || number > 30);
int choice = -1;// choice 为输入的选择
long stuNum[N];
float score[N];
float sum = 0, aver = 0;
int result3 = 0;
do{
printf("Management for Students' scores\n"
"1.Input record\n"
"2.Caculate total and average score of course\n"
"3.Sort in descending order by score\n"
"4.Sort in ascending order by number\n"
"5.Search by number\n"
"6.Statistic analysis\n"
"7.List record\n"
"0.Exit\n"
"Please Input your choice:\n");
result3 = scanf("%d", &choice);
if(choice == 1){
//用两个数组读入学生的学号及对应的成绩
printf("Input student's ID, name and score:\n");
int i;
int result2 = 0;
for(i = 0; i < number; i++){
do{
result2 = scanf("%ld%f", &stuNum[i], &score[i]);
}while(result2 != 2);//防止输入非法字符,可重新输入将错误的输入覆盖掉
}
}else if(choice == 2){
//计算成绩的总分及平均分
Function2(&sum, &aver, score, number);
printf("sum=%.0f,aver=%.2f\n", sum, aver);
}else if(choice == 3){
//按成绩由高到低对学号和成绩进行排序
Function3(stuNum, score, number);
}else if(choice == 4){
//按学号由小到大对学号和成绩进行排序
Function4(stuNum, score, number);
}else if(choice == 5){
//按学号查询学生排名及其考试成绩
Function5(stuNum, score, number);
}else if(choice == 6){
//按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比
Function6(stuNum, score, number);
}else if(choice == 7){
//输出每个学生的学号、考试成绩
Function4(stuNum, score, number);//用了第四项的功能,将按学号由小到大对学号和成绩进行排序
}else if(choice > 7 || choice < 0 || result3 != 1) {
printf("Input error!\n");
}
}while(choice != 0);
printf("End of program!");
return 0;
}
void Function2(float *sum, float *aver, float score[], int number)
{
int i;
for(i = 0; i < number; i++){
*sum += score[i];
}
*aver = *sum / number;
}
void Function3(long stuNum[], float score[], int number)
{
int i, j, k;
float tempS = 0;
long tempSN = 0;
for(i = 0; i < number - 1; i++){
k = i;
for(j = i + 1; j < number; j++){
if(score[j] > score[k]){
k = j;
}
}
if(i != k){
tempS = score[i];
score[i] = score[k];
score[k] = tempS;
tempSN = stuNum[i];
stuNum[i] = stuNum[k];
stuNum[k] = tempSN;
}
}
printf("Sort in descending order by score:\n");
for(i = 0; i < number; i++){
printf("%ld\t%.0f\n", stuNum[i], score[i]);
}
}
void Function4(long stuNum[], float score[], int number)
{
int i, j, k;
float tempS = 0;
long tempSN = 0;
for(i = 0; i < number - 1; i++){
k = i;
for(j = i + 1; j < number; j++){
if(stuNum[j] < stuNum[k]){
 
2
回复