使用c语言书写一个成绩排序程序,要求读入数据以及输出数据均以文件形式,计算每个学生三门功课的平均分,并按平均分排列名次,若平均分相同则名次并列;结果写入文件。 3、统计全班每门课程的平均分,并计算各分数段(60以下,60~69,70~79,80~89,90以上)的学生人数;结果写入文件。 4、按格式在屏幕上打印每名学生成绩条。 5、在屏幕上打印出所有不及格学生的下列信息:学号,姓名,不及格的课程名,该不及格课程成绩。 6、在屏幕打印优等生名单(学号,姓名,三门课程成绩,平均成绩,名次),优等生必须满足下列条件: 1)平均成绩大于90分;或平均分大于85分且至少有一门功课为100分;或者平均分大于85分且至少两门课程成绩为95分以上; 2)名次在前三名; 3)每门功课及格以上。,同时要求运行时有菜单
时间: 2023-09-22 13:06:36 浏览: 184
C语言程序设计-输入2个学生3门课的成绩,计算学生成绩平均分,课程平均分和最高分.
5星 · 资源好评率100%
很高兴为您解答这道题目,以下是 C 语言的代码,其中使用了结构体和链表来存储学生信息和成绩信息,并实现了菜单功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "scores.txt"
typedef struct Student {
char id[20];
char name[20];
int score1;
int score2;
int score3;
double avgScore;
int rank;
struct Student* next;
} Student;
Student* readStudentsFromFile(char* fileName);
void writeStudentsToFile(Student* head, char* fileName);
void printStudent(Student* student);
void printStudents(Student* head);
void sortStudentsByAvgScore(Student* head);
void calculateRank(Student* head);
void calculateCourseAvgScore(Student* head, double* avg1, double* avg2, double* avg3);
void printCourseStatistics(Student* head);
void printFailedStudents(Student* head);
void printExcellentStudents(Student* head);
int main() {
printf("Welcome to the score management system!\n");
Student* head = readStudentsFromFile(FILE_NAME);
if (!head) {
printf("Failed to read data from file, program exits.\n");
return -1;
}
printf("Data loaded successfully from file.\n");
while (1) {
printf("\nPlease select an operation:\n");
printf("1. Print all students' scores.\n");
printf("2. Print course statistics.\n");
printf("3. Print failed students.\n");
printf("4. Print excellent students.\n");
printf("5. Exit.\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printStudents(head);
break;
case 2: {
double avg1, avg2, avg3;
calculateCourseAvgScore(head, &avg1, &avg2, &avg3);
printf("Course 1 average score: %.2lf\n", avg1);
printf("Course 2 average score: %.2lf\n", avg2);
printf("Course 3 average score: %.2lf\n", avg3);
printCourseStatistics(head);
break;
}
case 3:
printFailedStudents(head);
break;
case 4:
printExcellentStudents(head);
break;
case 5:
writeStudentsToFile(head, FILE_NAME);
printf("Data saved to file and program exits.\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
}
Student* readStudentsFromFile(char* fileName) {
FILE* fp = fopen(fileName, "r");
if (!fp) {
printf("Failed to open file %s.\n", fileName);
return NULL;
}
Student* head = NULL, * tail = NULL;
while (1) {
char id[20], name[20];
int score1, score2, score3;
int count = fscanf(fp, "%s %s %d %d %d", id, name, &score1, &score2, &score3);
if (count == EOF) {
break;
}
if (count != 5) {
printf("Invalid data in file %s, program exits.\n", fileName);
fclose(fp);
return NULL;
}
Student* student = (Student*)malloc(sizeof(Student));
strcpy(student->id, id);
strcpy(student->name, name);
student->score1 = score1;
student->score2 = score2;
student->score3 = score3;
student->avgScore = (score1 + score2 + score3) / 3.0;
student->next = NULL;
if (!head) {
head = tail = student;
} else {
tail->next = student;
tail = student;
}
}
fclose(fp);
sortStudentsByAvgScore(head);
calculateRank(head);
return head;
}
void writeStudentsToFile(Student* head, char* fileName) {
FILE* fp = fopen(fileName, "w");
if (!fp) {
printf("Failed to open file %s.\n", fileName);
return;
}
Student* ptr = head;
while (ptr) {
fprintf(fp, "%s %s %d %d %d\n", ptr->id, ptr->name, ptr->score1, ptr->score2, ptr->score3);
ptr = ptr->next;
}
fclose(fp);
}
void printStudent(Student* student) {
printf("%s\t%s\t%d\t%d\t%d\t%.2lf\t%d\n", student->id, student->name, student->score1, student->score2, student->score3, student->avgScore, student->rank);
}
void printStudents(Student* head) {
printf("ID\tName\tCourse 1\tCourse 2\tCourse 3\tAverage\tRank\n");
Student* ptr = head;
while (ptr) {
printStudent(ptr);
ptr = ptr->next;
}
}
void sortStudentsByAvgScore(Student* head) {
int count = 0;
Student* ptr = head;
while (ptr) {
count++;
ptr = ptr->next;
}
for (int i = 0; i < count - 1; i++) {
ptr = head;
for (int j = 0; j < count - i - 1; j++) {
if (ptr->avgScore < ptr->next->avgScore) {
char tempId[20], tempName[20];
int tempScore1, tempScore2, tempScore3, tempRank;
double tempAvgScore;
strcpy(tempId, ptr->id);
strcpy(tempName, ptr->name);
tempScore1 = ptr->score1;
tempScore2 = ptr->score2;
tempScore3 = ptr->score3;
tempAvgScore = ptr->avgScore;
tempRank = ptr->rank;
strcpy(ptr->id, ptr->next->id);
strcpy(ptr->name, ptr->next->name);
ptr->score1 = ptr->next->score1;
ptr->score2 = ptr->next->score2;
ptr->score3 = ptr->next->score3;
ptr->avgScore = ptr->next->avgScore;
ptr->rank = ptr->next->rank;
strcpy(ptr->next->id, tempId);
strcpy(ptr->next->name, tempName);
ptr->next->score1 = tempScore1;
ptr->next->score2 = tempScore2;
ptr->next->score3 = tempScore3;
ptr->next->avgScore = tempAvgScore;
ptr->next->rank = tempRank;
}
ptr = ptr->next;
}
}
}
void calculateRank(Student* head) {
int rank = 1, count = 0;
double lastAvgScore = -1;
Student* ptr = head;
while (ptr) {
count++;
if (ptr->avgScore != lastAvgScore) {
rank = count;
}
ptr->rank = rank;
lastAvgScore = ptr->avgScore;
ptr = ptr->next;
}
}
void calculateCourseAvgScore(Student* head, double* avg1, double* avg2, double* avg3) {
double sum1 = 0, sum2 = 0, sum3 = 0;
int count = 0;
Student* ptr = head;
while (ptr) {
sum1 += ptr->score1;
sum2 += ptr->score2;
sum3 += ptr->score3;
count++;
ptr = ptr->next;
}
*avg1 = sum1 / count;
*avg2 = sum2 / count;
*avg3 = sum3 / count;
}
void printCourseStatistics(Student* head) {
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0;
Student* ptr = head;
while (ptr) {
if (ptr->score1 < 60 || ptr->score2 < 60 || ptr->score3 < 60) {
count1++;
} else if (ptr->avgScore < 70) {
count2++;
} else if (ptr->avgScore < 80) {
count3++;
} else if (ptr->avgScore < 90) {
count4++;
} else {
count5++;
}
ptr = ptr->next;
}
printf("60 or below: %d students\n", count1);
printf("60 - 69: %d students\n", count2);
printf("70 - 79: %d students\n", count3);
printf("80 - 89: %d students\n", count4);
printf("90 or above: %d students\n", count5);
}
void printFailedStudents(Student* head) {
printf("Failed students:\n");
printf("ID\tName\tCourse\tScore\n");
Student* ptr = head;
while (ptr) {
if (ptr->score1 < 60) {
printf("%s\t%s\tCourse 1\t%d\n", ptr->id, ptr->name, ptr->score1);
}
if (ptr->score2 < 60) {
printf("%s\t%s\tCourse 2\t%d\n", ptr->id, ptr->name, ptr->score2);
}
if (ptr->score3 < 60) {
printf("%s\t%s\tCourse 3\t%d\n", ptr->id, ptr->name, ptr->score3);
}
ptr = ptr->next;
}
}
void printExcellentStudents(Student* head) {
printf("Excellent students:\n");
printf("ID\tName\tCourse 1\tCourse 2\tCourse 3\tAverage\tRank\n");
Student* ptr = head;
while (ptr) {
if (ptr->avgScore >= 90 ||
(ptr->avgScore >= 85 && (ptr->score1 == 100 || ptr->score2 == 100 || ptr->score3 == 100)) ||
(ptr->avgScore >= 85 && (ptr->score1 >= 95 && ptr->score2 >= 95 || ptr->score1 >= 95 && ptr->score3 >= 95 || ptr->score2 >= 95 && ptr->score3 >= 95))) {
printStudent(ptr);
}
ptr = ptr->next;
}
}
```
在代码中,`Student` 结构体用于存储学生信息,包括学号、姓名、三门功课成绩、平均分和排名,`next` 指针用于链接链表。`readStudentsFromFile` 函数从文件中读取学生信息,返回链表头指针;`writeStudentsToFile` 函数将链表中的学生信息写入文件;`printStudent` 函数打印一名学生的所有信息;`printStudents` 函数打印所有学生的信息;`sortStudentsByAvgScore` 函数按平均分排序所有学生;`calculateRank` 函数计算每名学生的排名;`calculateCourseAvgScore` 函数计算每门课程的平均分;`printCourseStatistics` 函数打印各分数段学生人数;`printFailedStudents` 函数打印不及格学生信息;`printExcellentStudents` 函数打印优等生信息。
在 `main` 函数中,使用 `while` 循环实现菜单功能,不断读取用户输入的操作选项,并调用相应的函数。
阅读全文