You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.8 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
char id[20];
char clas[20];
char name[20];
double score1;
double score2;
double score3;
double total_score;
} Student;
Student students[MAX_STUDENTS];
int student_count = 0;
void display_menu();
void input_students();
void delete_student();
void select_student();
void order_students();
void output_students();
int compare_students(const void *a, const void *b);
int main() {
char option;
while (1) {
display_menu();
printf("please input your option\n");
scanf(" %c", &option);
switch (option) {
case '1':
input_students();
break;
case '2':
delete_student();
break;
case '3':
select_student();
break;
case '4':
order_students();
break;
case '5':
output_students();
break;
case '6':
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid option. Please try again.\n");
}
}
return 0;
}
void display_menu() {
printf("1.input\n");
printf("2.delete\n");
printf("3.select\n");
printf("4.order\n");
printf("5.output\n");
printf("6.quit\n");
}
void input_students() {
char cont[4];
do {
if (student_count >= MAX_STUDENTS) {
return;
}
printf("id ");
scanf("%s", students[student_count].id);
printf("class ");
scanf("%s", students[student_count].clas);
printf("name ");
scanf("%s", students[student_count].name);
printf("score1 ");
scanf("%lf", &students[student_count].score1);
printf("score2 ");
scanf("%lf", &students[student_count].score2);
printf("score3 ");
scanf("%lf", &students[student_count].score3);
students[student_count].total_score = students[student_count].score1 +
students[student_count].score2 +
students[student_count].score3;
student_count++;
printf("continue? ");
scanf("%s", cont);
} while (strcmp(cont, "yes") == 0);
}
void delete_student() {
char input[20];
char cont[4];
do{
scanf("%s", input);
int i, found = 0;
for (i = 0; i < student_count; i++) {
if (strcmp(students[i].id, input) == 0 || strcmp(students[i].name, input) == 0) {
found = 1;
break;
}
}
if (found) {
for (int j = i; j < student_count - 1; j++) {
students[j] = students[j + 1];
}
student_count--;
} else {
output_students();
}
printf("continue?\n");
scanf("%s",cont);
}while (strcmp(cont, "yes") == 0);
}
void select_student() {
char input[20];
scanf("%s", input);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, input) == 0 || strcmp(students[i].clas, input) == 0) {
found = 1;
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n",
students[i].id,
students[i].clas,
students[i].name,
students[i].score1,
students[i].score2,
students[i].score3,
students[i].total_score);
}
}
if (!found) {
printf("there is no eligible student\n");
}
}
void order_students() {
qsort(students, student_count, sizeof(Student), compare_students);
printf("Students ordered successfully.\n");
}
void output_students() {
for (int i = 0; i < student_count; i++) {
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n",
students[i].id,
students[i].clas,
students[i].name,
students[i].score1,
students[i].score2,
students[i].score3,
students[i].total_score);
}
}
int compare_students(const void *a, const void *b) {
Student *studentA = (Student *)a;
Student *studentB = (Student *)b;
int class_cmp = strcmp(studentA->clas, studentB->clas);
if (class_cmp != 0) {
return class_cmp;
}
if (studentA->total_score > studentB->total_score) {
return -1;
} else if (studentA->total_score < studentB->total_score) {
return 1;
}
return 0;
}