void Sort(Student stu[]) { system("cls"); int i, j, k; float s; char t[20]; cout << endl << "======>> 降序排列 <<======" << endl; int n = Read(stu); for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1; j++) { if (stu[j].average < stu[j + 1].average) { //num k = stu[j + 1].num; stu[j + 1].num = stu[j].num; stu[j].num = k; //c_program s = stu[j + 1].c_program; stu[j + 1].c_program = stu[j].c_program; stu[j].c_program = s; //total_scores s = stu[j + 1].total_scores; stu[j + 1].total_scores = stu[j].total_scores; stu[j].total_scores= s; //english s = stu[j + 1].english; stu[j + 1].english = stu[j].english; stu[j].english = s; //math s = stu[j + 1].math; stu[j + 1].math = stu[j].math; stu[j].math = s; //average s = stu[j + 1].average; stu[j + 1].average = stu[j].average; stu[j].average = s; } } } cout << "------------------------------------------------------------------------------------" << endl; cout << "班级" << "\t" << "学号" << "\t" << "姓名" << "\t" << "C++" << "\t" << "总分" << "\t" << "英语" << "\t" << "数学" << "\t" << "平均分" << endl; cout << "------------------------------------------------------------------------------------" << endl; for (int i = 0; i < n; i++) { stu[i].order = i + 1; cout << stu[i].class_0 << "\t" << stu[i].num << "\t" << stu[i].name << "\t" << stu[i].c_program << "\t" << stu[i].total_scores << "\t" << stu[i].english << "\t" << stu[i].math << "\t" << stu[i].average << endl; } Write(stu, n); }实验结果分析
时间: 2024-04-13 12:26:01 浏览: 62
这段代码是一个降序排列学生信息的函数。代码首先通过调用Read函数获取学生信息,并存储在stu数组中。然后使用冒泡排序算法对学生信息进行降序排序,排序的依据是学生的平均分。在排序过程中,如果前一个学生的平均分小于后一个学生的平均分,则交换这两个学生的所有信息(学号、C++成绩、总分、英语成绩、数学成绩、平均分)。最后,函数输出排序后的学生信息,并调用Write函数将排序后的结果写入文件。
实验结果分析的话,可以根据输出的结果观察每个学生的平均分是否按降序排列,以及其他信息是否正确。如果排序结果与预期不符,可以检查代码中是否存在逻辑错误或者数据读取写入的问题。
相关问题
补全下段代码:#include <stdio.h> struct student { int num; char name[15]; float score[3]; float sum; }; void calc(struct student *p,int n); void sort(struct student *p,int n); int main() { struct student stu[5]; int i,j; float f; for(i=0;i<5;i++) { scanf("%d%s",&stu[i].num,stu[i].name); for(j=0;j<3;j++) { scanf("%f",&f); stu[i].score[j]=f; } } calc(stu,5); sort(stu,5); for(i=0;i<5;i++) { printf("%5d%15s",stu[i].num,stu[i].name); printf(" %.1f %.1f %.1f %.1f\n",stu[i].score[0],stu[i].score[1],stu[i].score[2], stu[i].sum); } return 0;
}void calc(struct student *p, int n) {
int i, j;
for (i = 0; i < n; i++) {
p[i].sum = 0;
for (j = 0; j < 3; j++) {
p[i].sum += p[i].score[j];
}
}
}
void sort(struct student *p, int n) {
int i, j;
struct student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (p[j].sum < p[j + 1].sum) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
C语言查错。#include <stdio.h> #include <stdlib.h> #define N 10 typedef struct student { long studentID; char studentName[20]; float score[3]; }STUDENT; void input (STUDENT * stu_inf,int n) { int j; for(j=0;j<n;j++) { scanf("%ld",stu_inf->studentID); scanf("%s",stu_inf->studentName); for(int k=0;k<3;k++) scanf("%f",stu_inf->score[k]); stu_inf++; } } void average(STUDENT * stu_inf,int n,int * ave_course) { int i,j,s; for(i=0;i<N;i++) { s=0; for(j=0;j<3;j++) s+=stu_inf->score[j]; *ave_course=s/3; ave_course++; } } STUDENT max(STUDENT * stu_inf,int n) { STUDENT *top; int i,j,k; float sumscore[10]={0}; for(i=0;i<N;i++) { for(j=0;j<3;j++) sumscore[i]+=stu_inf->score[j]; } for(i=0,k=0;i<9;i++) { if(sumscore[i]>sumscore[k]) k=i; } for(j=0;j<10;j++) { if(j==k) top=stu_inf; break; } return *top; } int main() { int i; float av[10]; STUDENT *inf; STUDENT *ave; ave=av; for(i=0;i<N;i++) {input(inf,N);} average(inf,N,ave); STUDENT m=max(inf,N); printf("总平均分是%f\n最高分学生数据如下:\n",*ave); printf("%ld %s ",m.studentID,m.studentName); int su=0; for(i=0;i<3;i++) { printf("%f ",m.score[i]); su+=m.score[i]; } printf("%f",su/3); return 0; }
这段代码有几个错误:
1.在input函数中,scanf函数的参数应该是&stu_inf->studentID和stu_inf->studentName,因为它们是指向long和char数组的指针,而不是实际的变量。
2.在average函数中,变量i应该从0开始,而不是从N开始。另外,在计算平均分时应该使用float类型的变量,而不是int类型的指针。
3.在max函数中,变量i应该从0开始,而不是从N开始。在查找最高分时,变量k应该初始化为0,而不是为i。
4.在max函数中,返回的是指向STUDENT结构体的指针,而不是STUDENT结构体本身。因此,在main函数中,应该使用*号来解引用指针。
5.在main函数中,变量inf没有分配内存空间,因此无法使用。应该使用malloc函数来分配内存空间,并将返回的指针赋值给inf。另外,变量ave应该是float类型的数组,而不是指针。
以下是修改后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct student {
long studentID;
char studentName[20];
float score[3];
} STUDENT;
void input(STUDENT* stu_inf, int n) {
int j;
for (j = 0; j < n; j++) {
scanf("%ld", &stu_inf->studentID);
scanf("%s", stu_inf->studentName);
for (int k = 0; k < 3; k++)
scanf("%f", &stu_inf->score[k]);
stu_inf++;
}
}
void average(STUDENT* stu_inf, int n, float* ave_course) {
int i, j;
for (i = 0; i < N; i++) {
float s = 0;
for (j = 0; j < 3; j++)
s += stu_inf->score[j];
*ave_course = s / 3;
ave_course++;
stu_inf++;
}
}
STUDENT* max(STUDENT* stu_inf, int n) {
STUDENT* top;
int i, j, k = 0;
float sumscore[10] = { 0 };
for (i = 0; i < N; i++) {
for (j = 0; j < 3; j++)
sumscore[i] += stu_inf->score[j];
stu_inf++;
}
for (i = 0; i < 9; i++) {
if (sumscore[i] > sumscore[k])
k = i;
}
stu_inf -= N;
for (j = 0; j < 10; j++) {
if (j == k) {
top = stu_inf;
break;
}
stu_inf++;
}
return top;
}
int main() {
int i;
float av[10];
STUDENT* inf = (STUDENT*)malloc(N * sizeof(STUDENT));
float* ave = av;
for (i = 0; i < N; i++) {
input(inf, N);
}
average(inf, N, ave);
STUDENT* m = max(inf, N);
printf("总平均分是%f\n最高分学生数据如下:\n", *ave);
printf("%ld %s ", m->studentID, m->studentName);
int su = 0;
for (i = 0; i < 3; i++) {
printf("%f ", m->score[i]);
su += m->score[i];
}
printf("%f", su / 3.0);
free(inf);
return 0;
}
```
阅读全文