QZHY-HTX / Data-Structure

泉州海洋职业学院信息工程学院2023-2024学年第二学期数据结构课程仓库
MIT License
6 stars 9 forks source link

孙谙炫2302580056 #62

Open sunanxuan opened 7 months ago

sunanxuan commented 7 months ago

include

include

strucu student { int id; int score; } void swap(struct student a,struct student b){ struct student temp=a; a=b; b=temp; } void bubblesort(struct student arr[],int n){ int i,j; for(i=0;j<n-i-1;j++){ if(arr[j].score>arr[j+1],score){ swap(&arr[j],&arr[j+1]); } } } void calculateStatistics(struct Student arr[], int n) { int failCount = 0; int totalScore = 0;

for (int i = 0; i < n; i++) {
    totalScore += arr[i].score;

    if (arr[i].score < 60) {
        failCount++;
    }
}

if (n != 0) {
    printf("平均成绩: %.2f\n", (float)totalScore / n);
} else {
    printf("没有学生成绩\n");
}

printf("不及格人数: %d\n", failCount);

} int main() { int n; printf("请输入学生人数: "); scanf("%d", &n);

struct Student *students = (struct Student *)malloc(n * sizeof(struct Student));
printf("请输入学生学号和成绩:\n");

for (int i = 0; i < n; i++) {
    printf("学号: ");
    scanf("%d", &students[i].id);

    printf("成绩: ");
    scanf("%d", &students[i].score);
}

bubbleSort(students, n);
printf("按成绩从高到低排序的学生学号和成绩:\n");

for (int i = 0; i < n; i++) {
    printf("学号: %d, 成绩: %d\n", students[i].id, students[i].score);
}

calculateStatistics(students, n); free(students); return 0;