用c语言编程: 班上有30人,每人有学号,数学,英语,c语言3门课程的成绩数据。编程完成如下任务: 1、将数据输入到文本文件score.txt 2、计算每人总分,将学号,数学,英语,c语言,总分等数据按总分从高到低输出每位同学的全部数据。 3、将每位同学的数据按学号顺序计算输出各同学的总分排名,分数最高的名次为1,分数相同的名次相同,若有i个同学排在第x位,则下个分数的名次为x+i位。并将这些数据均写入文本文件newscore.txt
时间: 2023-05-28 19:06:01 浏览: 79
c语言入门编程之数学问题Nim游戏.zip
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 30
#define MAX_SCORE_NUM 3
struct student{
char id[10];
int score[MAX_SCORE_NUM];
int totalScore;
int rank;
};
typedef struct student Student;
void inputScore(Student students[]);
void calculateTotalScore(Student students[]);
void sortByTotalScore(Student students[]);
void printAllData(Student students[]);
void calculateRank(Student students[]);
void printRank(Student students[]);
int main()
{
Student students[MAX_STUDENT_NUM];
inputScore(students);
calculateTotalScore(students);
sortByTotalScore(students);
printAllData(students);
calculateRank(students);
printRank(students);
return 0;
}
void inputScore(Student students[])
{
FILE* fp = fopen("score.txt", "w");
if(fp == NULL){
printf("Failed to open file.\n");
exit(1);
}
printf("Please input the score of each student:\n");
fprintf(fp, "id math english c total\n");
for(int i = 0; i < MAX_STUDENT_NUM; i++){
printf("student %d:\n", i+1);
scanf("%s %d %d %d", students[i].id, &students[i].score[0], &students[i].score[1], &students[i].score[2]);
fprintf(fp, "%s %d %d %d\n", students[i].id, students[i].score[0], students[i].score[1], students[i].score[2]);
}
fclose(fp);
}
void calculateTotalScore(Student students[])
{
for(int i = 0; i < MAX_STUDENT_NUM; i++){
students[i].totalScore = 0;
for(int j = 0; j < MAX_SCORE_NUM; j++){
students[i].totalScore += students[i].score[j];
}
}
}
void sortByTotalScore(Student students[])
{
Student temp;
for(int i = 0; i < MAX_STUDENT_NUM-1; i++){
for(int j = i+1; j < MAX_STUDENT_NUM; j++){
if(students[i].totalScore < students[j].totalScore){
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void printAllData(Student students[])
{
FILE* fp = fopen("newscore.txt", "w");
if(fp == NULL){
printf("Failed to open file.\n");
exit(1);
}
printf("id math english c total\n");
fprintf(fp, "id math english c total\n");
for(int i = 0; i < MAX_STUDENT_NUM; i++){
printf("%s %d %d %d %d\n", students[i].id, students[i].score[0], students[i].score[1], students[i].score[2], students[i].totalScore);
fprintf(fp, "%s %d %d %d %d\n", students[i].id, students[i].score[0], students[i].score[1], students[i].score[2], students[i].totalScore);
}
fclose(fp);
}
void calculateRank(Student students[])
{
for(int i = 0; i < MAX_STUDENT_NUM; i++){
students[i].rank = 1;
for(int j = 0; j < MAX_STUDENT_NUM; j++){
if(students[j].totalScore > students[i].totalScore){
students[i].rank++;
}
}
}
}
void printRank(Student students[])
{
FILE* fp = fopen("newscore.txt", "a");
if(fp == NULL){
printf("Failed to open file.\n");
exit(1);
}
printf("id rank\n");
fprintf(fp, "id rank\n");
for(int i = 0; i < MAX_STUDENT_NUM; i++){
printf("%s %d\n", students[i].id, students[i].rank);
fprintf(fp, "%s %d\n", students[i].id, students[i].rank);
}
fclose(fp);
}
阅读全文