某班有最多不超过30人(具体人数由键盘输入)参加期末考试,最多不超过6门(具体门数由键盘输入)。定义结构体类型描述学生信息,每个学生信息包括:学号、姓名、多门课的成绩、总成绩和平均成绩。用结构体数组作为函数参数,编程实现如下菜单驱动的学生成绩管理系统。(注:红色部分为必做功能,黑色部分为选做功能)录入每个学生的学号、姓名和各科考试成绩。计算每门课程的总分和平均分。计算每个学生的总分和平均分。按每个学生的总分由高到低排出名次表。按学号由小到大排出成绩表。按姓名的字典顺序排出成绩表。按学号查询学生排名极其考试成绩。按姓名查询学生排名极其考试成绩。按优秀(90100)、良好(8090)、中等(7080)、及格(6070)、不及格(0~59)5个类别,对每门课程分别统计每个类别的人数以及所占的百分比。输出每个学生的学号、姓名、各科考试成绩以及每门课程的总分和平均分。要求程序运行后先显示如下菜单,并提示用户输入选项:
Input record
Caculate total and average score of every course
Caculate total and average score of every student
Sort in descending order by total score of every student
Sort in ascending order by number
Sort in dictionary order by name
Search by number
Search by name
Statistic analysis for every course
List record
Exit
Please input your choice:
可以借鉴下
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
typedef struct PNode*LinkList;
typedef struct PNode{
int stdio;
char name[30];
double chinese;
double math;
double english;
double politics;
double sum;
PNode *next;
}PNode;
int main(){
LinkList llist=(LinkList)malloc(sizeof(PNode));
llist->next=NULL;
int a,b=1;
while(b){
printf("\n-----------------------学生成绩管理系统-----------------------\n");
printf("请选择你需要的功能:\n");
printf(" 1.查找 2.插入 3.排序 4.展示 5.删除 6.退出\n");
scanf("%d",&a);
switch(a){
case 1:printf("\n-----------------------学生成绩查找功能-----------------------\n"); check(llist);break;
case 2:printf("\n-----------------------学生成绩插入功能-----------------------\n"); input(llist);break;
case 3:printf("\n-----------------------学生成绩排序功能-----------------------\n"); sortList(llist);break;
case 4:printf("\n-----------------------学生成绩展示功能-----------------------\n"); display(llist);break;
case 5:printf("\n-----------------------学生成绩删除功能-----------------------\n"); deleteList(llist);break;
case 6:b = 0; break;
default:printf("输入有误!请重新输入!\n");
}
}
printf("\n-----------------------已退出成绩管理系统-----------------------\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 30
#define MAX_COURSE 6
// Define struct for student information
struct Student {
int id;
char name[20];
int scores[MAX_COURSE];
int total_score;
float avg_score;
};
// Function prototypes
void input_record(struct Student students[], int num_students);
void calc_total_avg_course(struct Student students[], int num_students, int num_courses);
void calc_total_avg_student(struct Student students[], int num_students, int num_courses);
void sort_desc_total_score(struct Student students[], int num_students);
void sort_asc_number(struct Student students[], int num_students);
void sort_dict_name(struct Student students[], int num_students);
void search_by_number(struct Student students[], int num_students);
void search_by_name(struct Student students[], int num_students);
void stat_analysis(struct Student students[], int num_students, int num_courses);
void list_record(struct Student students[], int num_students);
void exit_program();
int main() {
struct Student students[MAX_STUDENT];
int num_students, num_courses;
int choice;
printf("Welcome to the student score management system!\n");
// Get number of students and courses from user input
printf("Please enter the number of students (max %d): ", MAX_STUDENT);
scanf("%d", &num_students);
printf("Please enter the number of courses (max %d): ", MAX_COURSE);
scanf("%d", &num_courses);
// Display menu and prompt user for choice
do {
printf("\nMenu:");
printf("\n1. Input record");
printf("\n2. Calculate total and average score of every course");
printf("\n3. Calculate total and average score of every student");
printf("\n4. Sort in descending order by total score of every student");
printf("\n5. Sort in ascending order by number");
printf("\n6. Sort in dictionary order by name");
printf("\n7. Search by number");
printf("\n8. Search by name");
printf("\n9. Statistic analysis for every course");
printf("\n10. List record");
printf("\n11. Exit");
printf("\nPlease enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
input_record(students, num_students);
break;
case 2:
calc_total_avg_course(students, num_students, num_courses);
break;
case 3:
calc_total_avg_student(students, num_students, num_courses);
break;
case 4:
sort_desc_total_score(students, num_students);
break;
case 5:
sort_asc_number(students, num_students);
break;
case 6:
sort_dict_name(students, num_students);
break;
case 7:
search_by_number(students, num_students);
break;
case 8:
search_by_name(students, num_students);
break;
case 9:
stat_analysis(students, num_students, num_courses);
break;
case 10:
list_record(students, num_students);
break;
case 11:
exit_program();
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
} while (1); // Loop forever until user chooses to exit
return 0;
}
// Function to input record for each student
void input_record(struct Student students[], int num_students) {
int i, j;
for (i = 0; i < num_students; i++) {
printf("\nStudent %d:", i + 1);
printf("\nEnter student ID: ");
scanf("%d", &students[i].id);
printf("Enter student name: ");
scanf("%s", students[i].name);
for (j = 0; j < MAX_COURSE; j++) {
printf("Enter score for course %d: ", j + 1);
scanf("%d", &students[i].scores[j]);
}
}
printf("\nRecord input complete.\n");
}
// Function to calculate total and average score of every course
void calc_total_avg_course(struct Student students[], int num_students, int num_courses) {
int i, j;
int total_scores[MAX_COURSE] = {0};
float avg_scores[MAX_COURSE] = {0};
// Calculate total score for each course
for (i = 0; i < num_students; i++) {
for (j = 0; j < num_courses; j++) {// Calculate total score for each course
for (i = 0; i < num_students; i++) {
for (j = 0; j < num_courses; j++) {
total_scores[j] += students[i].scores[j];
}
}
// Calculate average score for each course
for (j = 0; j < num_courses; j++) {
avg_scores[j] = (float) total_scores[j] / num_students;
}
// Display results
printf("\nTotal and average scores for each course:\n");
for (j = 0; j < num_courses; j++) {
printf("Course %d: Total score = %d, Average score = %.2f\n", j + 1, total_scores[j], avg_scores[j]);
}
}
// Function to calculate total and average score of every student
void calc_total_avg_student(struct Student students[], int num_students, int num_courses) {
int i, j;
// Calculate total and average score for each student
for (i = 0; i < num_students; i++) {
int total_score = 0;
for (j = 0; j < num_courses; j++) {
total_score += students[i].scores[j];
}
students[i].total_score = total_score;
students[i].avg_score = (float) total_score / num_courses;
}
// Display results
printf("\nTotal and average scores for each student:\n");
for (i = 0; i < num_students; i++) {
printf("%d %s: Total score = %d, Average score = %.2f\n", students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
// Function to sort students in descending order by total score
void sort_desc_total_score(struct Student students[], int num_students) {
int i, j;
struct Student temp;
// Sort using bubble sort algorithm
for (i = 0; i < num_students - 1; i++) {
for (j = 0; j < num_students - 1 - i; j++) {
if (students[j].total_score < students[j+1].total_score) {
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
// Display results
printf("\nStudents sorted in descending order by total score:\n");
for (i = 0; i < num_students; i++) {
printf("%d %s: Total score = %d, Average score = %.2f\n", students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
// Function to sort students in ascending order by ID number
void sort_asc_number(struct Student students[], int num_students) {
int i, j;
struct Student temp;
// Sort using bubble sort algorithm
for (i = 0; i < num_students - 1; i++) {
for (j = 0; j < num_students - 1 - i; j++) {
if (students[j].id > students[j+1].id) {
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
// Display results
printf("\nStudents sorted in ascending order by ID number:\n");
for (i = 0; i < num_students; i++) {
printf("%d %s: Total score = %d, Average score = %.2f\n", students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
// Function to sort students in dictionary order by name
void sort_dict_name(struct Student students[], int num_students) {
int i, j;
struct Student temp;
// Sort using bubble sort algorithm
for (i = 0; i < num_students - 1; i++) {
for (j = 0; j < num_students - 1 - i; j++) {
if (strcmp(students[j].name, students[j+1].name) > 0) {
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
// Display results
printf("\nStudents sorted in dictionary order by name:\n");
for (i = 0; i < num_students; i++) {
printf("%d %s: Total score = %d, Average score = %.2f\n", students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
// Function to search for a student by ID number
void search_by_number(struct Student students[], int num_students) {
int i, id;
int found = 0;
printf("\nEnter student ID to search for: ");
scanf("%d", &id);
// Search for student with given ID number
for (i = 0; i < num_students; i++) {
if (students[i].id == id) {
found = 1;
printf("%d %s: Total score = %d, Average score = %.2f\n", students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
break;
}
}
if (!found) {
printf("Student not found.\n");
}
}
// Function to search for a student by name
void search_by_name(struct Student students[], int num_students) {
int i;
char name[20];
int found = 0;
printf("\nEnter student name to search for: ");
scanf("%s", name);
// Search for student with given name
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
found = 1;
printf("%d %s: Total score = %d, Average score = %.2f\n", students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
if (!found) {
printf("Student not found.\n");
}
}
// Function to perform statistical analysis for each course
void stat_analysis(struct Student students[], int num_students, int num_courses) {
int i, j;
int excellent[MAX_COURSE] = {0};
int good[MAX_COURSE] = {0};
int medium[MAX_COURSE] = {0};
int pass[MAX_COURSE] = {0};
int fail[MAX_COURSE] = {0};
// Calculate number of students in each category for each course
for (i = 0; i < num_students; i++) {
for (j = 0; j < num_courses; j++) {
if (students[i].scores[j] >= 90) {
excellent[j]++;
} else if (students[i].scores[j] >= 80) {
good[j]++;
} else if (students[i].scores[j] >= 70) {
medium[j]++;
} else if (students[i].scores[j] >= 60) {
pass[j]++;
} else {
fail[j]++;
}
}
}
// Display results
printf("\nStatistical analysis for each course:\n");
for (j = 0; j < num_courses; j++) {
printf("Course %d: Excellent = %d (%.2f%%), Good = %d (%.2f%%), Medium = %d (%.2f%%), Pass = %d (%.2f%%), Fail = %d (%.2f%%)\n", j + 1,
excellent[j], (float) excellent[j] / num_students * 100,
good[j], (float) good[j] / num_students * 100,
medium[j], (float) medium[j] / num_students * 100,
pass[j], (float) pass[j] / num_students * 100,
fail[j], (float) fail[j] / num_students * 100);
}
}
// Function to list all records
void list_record(struct Student students[], int num_students) {
int i, j;
printf("\nList of all records:\n");
for (i = 0; i < num_students; i++) {
printf("%d %s: ", students[i].id, students[i].name);
for (j = 0; j < MAX_COURSE; j++) {
printf("%d ", students[i].scores[j]);
}
printf("Total score = %d, Average score = %.2f\n", students[i].total_score, students[i].avg_score);
}
}
// Function to exit the program
void exit_program() {
printf("Exiting program...\n");
}
不知道你这个问题是否已经解决, 如果还没有解决的话:我可以尝试解决该问题。以下是一个参考解决方案,它包括录入学生信息、计算课程总分和平均分、计算学生总分和平均分、按学生总分排名、按学号和姓名排名、查询学生排名及对应成绩、按分数范围统计每门课程的学生数量及百分比、输出学生信息和退出程序的菜单驱动功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最大学生数
#define MAX_NAME_LEN 20 // 最大姓名长度
#define MAX_COURSES 10 // 最大课程数
// 学生结构体
struct Student {
int id; // 学号
char name[MAX_NAME_LEN]; // 姓名
float scores[MAX_COURSES]; // 成绩数组
float totalScore; // 总分
float averageScore; // 平均分
int rank; // 排名
};
// 可选菜单项
enum MenuOption {
ADD_STUDENT = 1,
CALC_TOTAL_AVER,
CALC_STUDENT_TOTAL_AVER,
RANK_STUDENTS,
SORT_STUDENTS_BY_ID,
SORT_STUDENTS_BY_NAME,
QUERY_STUDENT_RANK_AND_SCORE,
STATS_FOR_GRADE_RANGE,
PRINT_STUDENTS,
QUIT
};
// 声明函数
void displayMenu();
int readMenuOption();
void addStudent(struct Student students[], int* numStudents);
void calculateTotalAndAverageScore(struct Student students[], int numStudents);
void calculateTotalAndAverageScoreForStudent(struct Student* student);
void rankStudents(struct Student students[], int numStudents);
void sortStudentsById(struct Student students[], int numStudents);
void sortStudentsByName(struct Student students[], int numStudents);
void queryStudentRankAndScore(struct Student students[], int numStudents);
void statsForGradeRange(struct Student students[], int numStudents);
void printStudents(struct Student students[], int numStudents);
// 主函数
int main() {
struct Student students[MAX_STUDENTS];
int numStudents = 0;
int choice;
do {
displayMenu();
choice = readMenuOption();
switch (choice) {
case ADD_STUDENT:
addStudent(students, &numStudents);
break;
case CALC_TOTAL_AVER:
calculateTotalAndAverageScore(students, numStudents);
break;
case CALC_STUDENT_TOTAL_AVER:
if (numStudents > 0) {
int studentIndex;
printf("Enter student index (0-%d): ", numStudents-1);
scanf("%d", &studentIndex);
if (studentIndex < 0 || studentIndex >= numStudents) {
printf("Invalid index.\n");
} else {
calculateTotalAndAverageScoreForStudent(&students[studentIndex]);
}
} else {
printf("No students added yet.\n");
}
break;
case RANK_STUDENTS:
rankStudents(students, numStudents);
break;
case SORT_STUDENTS_BY_ID:
sortStudentsById(students, numStudents);
printf("Sorted by ID.\n");
break;
case SORT_STUDENTS_BY_NAME:
sortStudentsByName(students, numStudents);
printf("Sorted by name.\n");
break;
case QUERY_STUDENT_RANK_AND_SCORE:
queryStudentRankAndScore(students, numStudents);
break;
case STATS_FOR_GRADE_RANGE:
statsForGradeRange(students, numStudents);
break;
case PRINT_STUDENTS:
printStudents(students, numStudents);
break;
case QUIT:
printf("Bye.\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != QUIT);
return 0;
}
// 显示菜单
void displayMenu() {
printf("\n");
printf("1. Add student\n");
printf("2. Calculate total and average scores for all students\n");
printf("3. Calculate total and average score for a student\n");
printf("4. Rank students by total score\n");
printf("5. Sort students by ID\n");
printf("6. Sort students by name\n");
printf("7. Query student rank and score\n");
printf("8. stats for grade range for each course\n");
printf("9. Print all students\n");
printf("10. Quit\n");
printf("\n");
}
// 读取菜单选项
int readMenuOption() {
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
return choice;
}
// 录入一个学生信息到数组中
void addStudent(struct Student students[], int* numStudents) {
if (*numStudents >= MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
} else {
struct Student newStudent;
int i;
printf("Enter student ID: ");
scanf("%d", &newStudent.id);
printf("Enter student name: ");
scanf("%s", newStudent.name);
for (i = 0; i < MAX_COURSES; i++) {
printf("Enter score for course %d: ", i+1);
scanf("%f", &newStudent.scores[i]);
}
newStudent.totalScore = 0;
newStudent.averageScore = 0;
newStudent.rank = 0;
students[*numStudents] = newStudent;
(*numStudents)++;
printf("Student added.\n");
}
}
// 计算所有学生的总分和平均分,并打印结果
void calculateTotalAndAverageScore(struct Student students[], int numStudents) {
int i, j;
float totalScoreSum = 0, averageScoreSum = 0;
for (i = 0; i < numStudents; i++) {
float totalScore = 0;
for (j = 0; j < MAX_COURSES; j++) {
totalScore += students[i].scores[j];
}
students[i].totalScore = totalScore;
students[i].averageScore = totalScore / MAX_COURSES;
totalScoreSum += totalScore;
averageScoreSum += students[i].averageScore;
}
printf("Total scores: %.2f, average score: %.2f\n", totalScoreSum, averageScoreSum / numStudents);
}
// 计算一个学生的总分和平均分,并打印结果
void calculateTotalAndAverageScoreForStudent(struct Student* student) {
int i;
float totalScore = 0;
for (i = 0; i < MAX_COURSES; i++) {
totalScore += student->scores[i];
}
student->totalScore = totalScore;
student->averageScore = totalScore / MAX_COURSES;
printf("Total score: %.2f, average score: %.2f\n", student->totalScore, student->averageScore);
}
// 对所有学生按总分排名,并更新rank字段
void rankStudents(struct Student students[], int numStudents) {
int i, j;
for (i = 0; i < numStudents; i++) {
int rank = 1;
for (j = 0; j < numStudents; j++) {
if (students[j].totalScore > students[i].totalScore) {
rank++;
}
}
students[i].rank = rank;
}
printf("Students ranked by total score.\n");
}
// 对所有学生按学号排序,使用冒泡排序
void sortStudentsById(struct Student students[], int numStudents) {
int i, j;
for (i = 0; i < numStudents - 1; i++) {
for (j = 0; j < numStudents - i - 1; j++) {
if (students[j].id > students[j+1].id) {
struct Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
// 对所有学生按姓名排序,使用选择排序
void sortStudentsByName(struct Student students[], int numStudents) {
int i, j;
for (i = 0; i < numStudents - 1; i++) {
int minIndex = i;
for (j = i + 1; j < numStudents; j++) {
if (strcmp(students[j].name, students[minIndex].name) < 0) {
minIndex = j;
}
}
if (minIndex != i) {
struct Student temp = students[i];
students[i] = students[minIndex];
students[minIndex] = temp;
}
}
}
// 查询某个学生的排名和成绩
void queryStudentRankAndScore(struct Student students[], int numStudents) {
if (numStudents > 0) {
int studentIndex;
printf("Enter student index (0-%d): ", numStudents-1);
scanf("%d", &studentIndex);
if (studentIndex < 0 || studentIndex >= numStudents) {
printf("Invalid index.\n");
} else {
printf("%s's rank: %d, total score: %.2f\n", students[studentIndex].name,
students[studentIndex].rank, students[studentIndex].totalScore);
}
} else {
printf("No students added yet.\n");
}
}
// 统计每门课程在一定分数范围内的学生数量和百分比
void statsForGradeRange(struct Student students[], int numStudents) {
int i, j;
float min, max;
for (i = 0; i < MAX_COURSES; i++) {
printf("Stats for course %d:\n", i + 1);
printf("Enter min grade: ");
scanf("%f", &min);
printf("Enter max grade: ");
scanf("%f", &max);
int count = 0;
for (j = 0; j < numStudents; j++) {
float grade = students[j].scores[i];
if (grade >= min && grade <= max) {
count++;
}
}
printf("Count: %d, percentage: %.2f%%\n", count, count * 100.0 / numStudents);
}
}
// 打印所有学生信息
void printStudents(struct Student students[], int numStudents) {
int i;
for (i = 0; i < numStudents; i++) {
printf("%d\t%s\t%.2f\t%.2f\t%d\n", students[i].id, students[i].name, students[i].totalScore,
students[i].averageScore, students[i].rank);
}
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 学生信息结构体
struct Student {
int studentID;
char name[50];
int scores[6];
int totalScore;
float averageScore;
};
// 函数声明
void inputRecord(struct Student students[], int numStudents, int numCourses);
void calculateCourseScores(struct Student students[], int numStudents, int numCourses);
void calculateStudentScores(struct Student students[], int numStudents, int numCourses);
void sortStudentsByTotalScore(struct Student students[], int numStudents);
void sortStudentsByNumber(struct Student students[], int numStudents);
void sortStudentsByName(struct Student students[], int numStudents);
void searchByNumber(struct Student students[], int numStudents, int numCourses);
void searchByName(struct Student students[], int numStudents, int numCourses);
void statisticAnalysis(struct Student students[], int numStudents, int numCourses);
void listRecord(struct Student students[], int numStudents, int numCourses);
int main() {
int numStudents, numCourses;
int choice;
printf("Please enter the number of students (up to 30): ");
scanf("%d", &numStudents);
printf("Please enter the number of courses (up to 6): ");
scanf("%d", &numCourses);
struct Student* students = (struct Student*)malloc(numStudents * sizeof(struct Student));
do {
printf("\n");
printf("1. Input record\n");
printf("2. Calculate total and average score of every course\n");
printf("3. Calculate total and average score of every student\n");
printf("4. Sort in descending order by total score of every student\n");
printf("5. Sort in ascending order by number\n");
printf("6. Sort in dictionary order by name\n");
printf("7. Search by number\n");
printf("8. Search by name\n");
printf("9. Statistic analysis for every course\n");
printf("10. List record\n");
printf("11. Exit\n");
printf("Please input your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
inputRecord(students, numStudents, numCourses);
break;
case 2:
calculateCourseScores(students, numStudents, numCourses);
break;
case 3:
calculateStudentScores(students, numStudents, numCourses);
break;
case 4:
sortStudentsByTotalScore(students, numStudents);
break;
case 5:
sortStudentsByNumber(students, numStudents);
break;
case 6:
sortStudentsByName(students, numStudents);
break;
case 7:
searchByNumber(students, numStudents, numCourses);
break;
case 8:
searchByName(students, numStudents, numCourses);
break;
case 9:
statisticAnalysis(students, numStudents, numCourses);
break;
case 10:
listRecord(students, numStudents, numCourses);
break;
case 11:
printf("Exiting the program...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 11);
free(students);
return 0;
}
void inputRecord(struct Student students[], int numStudents, int numCourses) {
int i, j;
for (i = 0; i < numStudents; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Enter student ID: ");
scanf("%d", &students[i].studentID);
printf("Enter name: ");
scanf("%s", students[i].name);
for (j = 0; j < numCourses; j++) {
printf("Enter score for course %d: ", j + 1);
scanf("%d", &students[i].scores[j]);
}
}
}
void calculateCourseScores(struct Student students[], int numStudents, int numCourses) {
int i, j;
int totalScores[numCourses] = {0};
for (i = 0; i < numStudents; i++) {
for (j = 0; j < numCourses; j++) {
totalScores[j] += students[i].scores[j];
}
}
printf("\nCourse scores:\n");
for (i = 0; i < numCourses; i++) {
printf("Course %d - Total score: %d, Average score: %.2f\n", i + 1, totalScores[i], (float)totalScores[i] / numStudents);
}
}
void calculateStudentScores(struct Student students[], int numStudents, int numCourses) {
int i, j;
for (i = 0; i < numStudents; i++) {
int totalScore = 0;
for (j = 0; j < numCourses; j++) {
totalScore += students[i].scores[j];
}
students[i].totalScore = totalScore;
students[i].averageScore = (float)totalScore / numCourses;
}
}
void sortStudentsByTotalScore(struct Student students[], int numStudents) {
int i, j;
struct Student temp;
for (i = 0; i < numStudents - 1; i++) {
for (j = 0; j < numStudents - 1 - i; j++) {
if (students[j].totalScore < students[j + 1].totalScore) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("\nStudents sorted by total score in descending order:\n");
for (i = 0; i < numStudents; i++) {
printf("Student ID: %d, Name: %s, Total score: %d, Average score: %.2f\n",
students[i].studentID, students[i].name, students[i].totalScore, students[i].averageScore);
}
}
void sortStudentsByNumber(struct Student students[], int numStudents) {
int i, j;
struct Student temp;
for (i = 0; i < numStudents - 1; i++) {
for (j = 0; j < numStudents - 1 - i; j++) {
if (students[j].studentID > students[j + 1].studentID) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("\nStudents sorted by number in ascending order:\n");
for (i = 0; i < numStudents; i++) {
printf("Student ID: %d, Name: %s, Total score: %d, Average score: %.2f\n",
students[i].studentID, students[i].name, students[i].totalScore, students[i].averageScore);
}
}
void sortStudentsByName(struct Student students[], int numStudents) {
int i, j;
struct Student temp;
for (i = 0; i < numStudents - 1; i++) {
for (j = 0; j < numStudents - 1 - i; j++) {
if (strcmp(students[j].name, students[j + 1].name) > 0) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("\nStudents sorted by name in dictionary order:\n");
for (i = 0; i < numStudents; i++) {
printf("Student ID: %d, Name: %s, Total score: %d, Average score: %.2f\n",
students[i].studentID, students[i].name, students[i].totalScore, students[i].averageScore);
}
}
void searchByNumber(struct Student students[], int numStudents, int numCourses) {
int searchNumber;
int i;
printf("\nEnter student number to search: ");
scanf("%d", &searchNumber);
for (i = 0; i < numStudents; i++) {
if (students[i].studentID == searchNumber) {
printf("\nStudent found!\n");
printf("Student ID: %d, Name: %s, Total score: %d, Average score: %.2f\n",
students[i].studentID, students[i].name, students[i].totalScore, students[i].averageScore);
printf("Scores:\n");
int j;
for (j = 0; j < numCourses; j++) {
printf("Course %d: %d\n", j + 1, students[i].scores[j]);
}
return;
}
}
printf("\nStudent not found.\n");
}
void searchByName(struct Student students[], int numStudents, int numCourses) {
char searchName[50];
int i;
printf("\nEnter student name to search: ");
scanf("%s", searchName);
for (i = 0; i < numStudents; i++) {
if (strcmp(students[i].name, searchName) == 0) {
printf("\nStudent found!\n");
printf("Student ID: %d, Name: %s, Total score: %d, Average score: %.2f\n",
students[i].studentID, students[i].name, students[i].totalScore, students[i].averageScore);
printf("Scores:\n");
int j;
for (j = 0; j < numCourses; j++) {
printf("Course %d: %d\n", j + 1, students[i].scores[j]);
}
return;
}
}
printf("\nStudent not found.\n");
}
void statisticAnalysis(struct Student students[], int numStudents, int numCourses) {
int i, j;
int categoryCounts[5] = {0};
float categoryPercentages[5] = {0};
for (i = 0; i < numStudents; i++) {
for (j = 0; j < numCourses; j++) {
int score = students[i].scores[j];
if (score >= 90) {
categoryCounts[0]++;
} else if (score >= 80) {
categoryCounts[1]++;
} else if (score >= 70) {
categoryCounts[2]++;
} else if (score >= 60) {
categoryCounts[3]++;
} else {
categoryCounts[4]++;
}
}
}
for (i = 0; i < 5; i++) {
categoryPercentages[i] = (float)categoryCounts[i] / (numStudents * numCourses) * 100;
}
printf("\nStatistic analysis for every course:\n");
printf("Excellent (90-100): %d students (%.2f%%)\n", categoryCounts[0], categoryPercentages[0]);
printf("Good (80-89): %d students (%.2f%%)\n", categoryCounts[1], categoryPercentages[1]);
printf("Average (70-79): %d students (%.2f%%)\n", categoryCounts[2], categoryPercentages[2]);
printf("Pass (60-69): %d students (%.2f%%)\n", categoryCounts[3], categoryPercentages[3]);
printf("Fail (0-59): %d students (%.2f%%)\n", categoryCounts[4], categoryPercentages[4]);
}
void listRecord(struct Student students[], int numStudents, int numCourses) {
int i, j;
printf("\nStudent records:\n");
for (i = 0; i < numStudents; i++) {
printf("Student ID: %d, Name: %s, Total score: %d, Average score: %.2f\n",
students[i].studentID, students[i].name, students[i].totalScore, students[i].averageScore);
printf("Scores:\n");
for (j = 0; j < numCourses; j++) {
printf("Course %d: %d\n", j + 1, students[i].scores[j]);
}
printf("\n");
}
}