首页 新闻 会员 周边 捐助

这C语言的实操代码怎么做(救救刚学C的孩子)

0
悬赏园豆:5 [待解决问题]

学生随机抽号演讲计分系统
设计一款用于课程大作业检查或比赛计分的软件,基本功能:
(1) 设置本课程的学生总数
(2) 根据本次参与的学生总数,随机抽取一个还未汇报演讲的学生的学号。
(3) 每个学生汇报演讲完毕,输入该学生的得分(假设有5个评委,则需输入5个成绩)。
(4) 计算该选手的得分:去掉最高分和最低分,其余的分数求平均,并显示选手序号、姓名、得分。
(5) 输出参加比赛过的所有学生的成绩及当前排名。
(6) 对演讲过的学生作上标记,下次程序运行时不再抽取该学号。
(7) 每次退出程序时(不一定一次能进行完所有的学生的汇报,可以分多次进行),将学生的序号、姓名、成绩写入文件。
(8) 结束对学生成绩进行排序,显示,并按照一等奖10%、二等奖20%、三等奖30%的比例确定选手的获奖名次,并将结果写入文件保存。
拓展功能:赛前事先输入评委人数,设置选手姓名、序号,选手上场顺序由程序随机抽取,已经抽取过的选手信息在文件中做出标记,下次不再抽取。其他更多方便用户使用的功能。

石梅湾的主页 石梅湾 | 初学一级 | 园豆:197
提问于:2024-01-02 22:45
< >
分享
所有回答(2)
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct {
    int id;
    char name[20];
    float scores[5];
    float average;
    int marked;
} Student;

void random_select(Student *students, int total);
void input_scores(Student *student);
void calculate_average(Student *student);
void sort_students(Student *students, int total);
void save_to_file(Student *students, int total);
void display_ranking(Student *students, int total);
void determine_prize(Student *students, int total);

int main() {
    int total;
    printf("请输入本课程的学生总数:");
    scanf("%d", &total);

    Student students[total];
    for (int i = 0; i < total; i++) {
        printf("请输入第%d个学生的序号和姓名:", i + 1);
        scanf("%d %s", &students[i].id, students[i].name);
        students[i].marked = 0;
    }

    int round = 1;
    while (1) {
        printf("第%d轮比赛开始,请按回车键继续...", round);
        getchar();

        random_select(students, total);
        for (int i = 0; i < total; i++) {
            if (!students[i].marked) {
                input_scores(&students[i]);
                calculate_average(&students[i]);
            }
        }

        sort_students(students, total);
        display_ranking(students, total);
        save_to_file(students, total);
        determine_prize(students, total);

        printf("是否继续下一轮比赛?(y/n):");
        char choice;
        scanf(" %c", &choice);
        if (choice == 'n' || choice == 'N') {
            break;
        }
        round++;
    }

    return 0;
}

void random_select(Student *students, int total) {
    srand(time(NULL));
    int index;
    do {
        index = rand() % total;
    } while (students[index].marked);
    students[index].marked = 1;
}

void input_scores(Student *student) {
    printf("请输入学生%d的得分(用空格隔开):", student->id);
    for (int i = 0; i < 5; i++) {
        scanf("%f", &student->scores[i]);
    }
}

void calculate_average(Student *student) {
    float sum = 0;
    for (int i = 1; i < 4; i++) {
        sum += student->scores[i];
    }
    student->average = sum / 3;
}

void sort_students(Student *students, int total) {
    for (int i = 0; i < total - 1; i++) {
        for (int j = 0; j < total - 1 - i; j++) {
            if (students[j].average < students[j + 1].average) {
                Student temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
}

void save_to_file(Student *students, int total) {
    FILE *fp = fopen("scores.txt", "w");
    if (fp == NULL) {
        printf("无法打开文件!");
        return;
    }
    for (int i = 0; i < total; i++) {
        fprintf(fp, "学号:%d 姓名:%s 得分:%.2f
", students[i].id, students[i].name, students[i].average);
    }
    fclose(fp);
}

void display_ranking(Student *students, int total) {
    printf("排名:
");
    for (int i = 0; i < total; i++) {
        printf("%d. 学号:%d 姓名:%s 得分:%.2f
", i + 1, students[i].id, students[i].name, students[i].average);
    }
}

void determine_prize(Student *students, int total) {
    float prize_total = total * 0.1f;
    float current_total = 0;
    for (int i = 0; i < total && current_total < prize_total; i++) {
        if (students[i].average >= 90) {
            current_total += 0.1f;
            printf("恭喜学生%d获得一等奖!
", students[i].id);
        } else if (students[i].average >= 80) {
            current_total += 0.1f;
            printf("恭喜学生%d获得二等奖!
", students[i].id);
        } else if (students[i].average >= 70) {
            current_total += 0.1f;
            printf("恭喜学生%d获得三等奖!
", students[i].id);
        }
    }
}

realzhangsan | 园豆:264 (菜鸟二级) | 2024-01-03 10:54
0

好的,我将为你提供一个简单的C语言实操代码,实现上述基本功能。请注意,这是一个基础版本,实际应用中可能需要更多的功能和优化。以下是一个简单的示例代码:

c
Copy code

include <stdio.h>

include <stdlib.h>

include <string.h>

include <time.h>

define MAX_STUDENTS 100

typedef struct {
int id;
char name[50];
float score;
int presented; // 是否已经演讲
} Student;

void initializeStudents(Student students[], int totalStudents) {
for (int i = 0; i < totalStudents; ++i) {
students[i].id = i + 1;
sprintf(students[i].name, "Student%d", i + 1);
students[i].score = 0.0;
students[i].presented = 0;
}
}

void drawRandomStudent(Student students[], int totalStudents, int *selectedId) {
int remainingStudents = 0;

for (int i = 0; i < totalStudents; ++i) {
    if (students[i].presented == 0) {
        remainingStudents++;
    }
}

if (remainingStudents == 0) {
    printf("All students have presented.\n");
    *selectedId = -1;
    return;
}

int randomIndex = rand() % remainingStudents;

for (int i = 0; i < totalStudents; ++i) {
    if (students[i].presented == 0) {
        if (randomIndex == 0) {
            *selectedId = students[i].id;
            students[i].presented = 1; // 标记为已经演讲
            break;
        } else {
            randomIndex--;
        }
    }
}

}

void recordScores(Student students[], int totalStudents, int selectedId) {
if (selectedId == -1) {
return;
}

printf("Enter scores for Student %d (%s):\n", selectedId, students[selectedId - 1].name);

for (int i = 0; i < 5; ++i) {
    printf("Judge %d: ", i + 1);
    scanf("%f", &(students[selectedId - 1].score));
}

}

void calculateAndDisplayResults(Student students[], int totalStudents) {
printf("\nResults:\n");
for (int i = 0; i < totalStudents; ++i) {
if (students[i].presented == 1) {
// 计算平均分(去掉最高分和最低分)
float minScore = students[i].score;
float maxScore = students[i].score;
float totalScore = students[i].score;

        for (int j = 1; j < 5; ++j) {
            float score;
            printf("Judge %d: ", j + 1);
            scanf("%f", &score);

            if (score < minScore) {
                minScore = score;
            }

            if (score > maxScore) {
                maxScore = score;
            }

            totalScore += score;
        }

        totalScore -= minScore + maxScore;
        students[i].score = totalScore / 3.0;

        // 显示选手序号、姓名、得分
        printf("Student %d (%s): %.2f\n", students[i].id, students[i].name, students[i].score);
    }
}

}

void displayAllResults(Student students[], int totalStudents) {
printf("\nAll Results:\n");
for (int i = 0; i < totalStudents; ++i) {
if (students[i].presented == 1) {
// 显示学生的序号、姓名、成绩
printf("Student %d (%s): %.2f\n", students[i].id, students[i].name, students[i].score);
}
}
}

void writeResultsToFile(Student students[], int totalStudents, const char *filename) {
FILE *file = fopen(filename, "w");

if (file == NULL) {
    printf("Error opening file for writing.\n");
    return;
}

for (int i = 0; i < totalStudents; ++i) {
    if (students[i].presented == 1) {
        fprintf(file, "%d %s %.2f\n", students[i].id, students[i].name, students[i].score);
    }
}

fclose(file);

}

int compareStudents(const void *a, const void *b) {
float scoreA = ((Student *)a)->score;
float scoreB = ((Student *)b)->score;

if (scoreA < scoreB) {
    return 1;
} else if (scoreA > scoreB) {
    return -1;
} else {
    return 0;
}

}

void displayRankings(Student students[], int totalStudents) {
// 对学生成绩进行排序
qsort(students, totalStudents, sizeof(Student), compareStudents);

printf("\nRankings:\n");
for (int i = 0; i < totalStudents; ++i) {
    if (students[i].presented == 1) {
        // 显示学生的序号、姓名、成绩
        printf("Rank %d: Student %d (%s): %.2f\n", i + 1, students[i].id, students[i].name, students[i].score);
    }
}

}

int main() {
srand((unsigned int)time(NULL));

int totalStudents;
printf("Enter the total number of students: ");
scanf("%d", &totalStudents);

if (totalStudents > MAX_STUDENTS || totalStudents < 1) {
    printf("Invalid number of students.\n");
    return 1;
}

Student students[MAX_STUDENTS];
initializeStudents(students, totalStudents);

int selectedId;

do {
    drawRandomStudent(students, totalStudents, &selectedId);

    if (selectedId != -1) {
        recordScores(students,
Technologyforgood | 园豆:7535 (大侠五级) | 2024-01-08 19:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册