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.
147 lines
4.9 KiB
147 lines
4.9 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct Student {
|
|
char *id;
|
|
char *clas;
|
|
char *name;
|
|
double score1;
|
|
double score2;
|
|
double score3;
|
|
double score;
|
|
};
|
|
|
|
void input(struct Student students[], int *count);
|
|
void delete_student(struct Student students[], int *count);
|
|
void select_student(struct Student students[], int count);
|
|
void order_student(struct Student students[], int count);
|
|
void output_student(struct Student students[], int count);
|
|
void free_students(struct Student students[], int count);
|
|
|
|
int main() {
|
|
struct Student students[100];
|
|
int count = 0;
|
|
int option;
|
|
do {
|
|
printf("1. input\n2. delete\n3. select\n4. order\n5. output\n6. quit\nplease input your option: ");
|
|
scanf("%d", &option);
|
|
switch (option) {
|
|
case 1:
|
|
input(students, &count);
|
|
break;
|
|
case 2:
|
|
delete_student(students, &count);
|
|
break;
|
|
case 3:
|
|
select_student(students, count);
|
|
break;
|
|
case 4:
|
|
order_student(students, count);
|
|
break;
|
|
case 5:
|
|
output_student(students, count);
|
|
break;
|
|
case 6:
|
|
printf("Exiting the program...\n");
|
|
break;
|
|
default:
|
|
printf("Invalid option, please try again.\n");
|
|
}
|
|
} while (option != 6);
|
|
|
|
free_students(students, count);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void input(struct Student students[], int *count) {
|
|
char continue_input[5];
|
|
do {
|
|
if (*count >= 100) {
|
|
printf("Maximum number of students reached.\n");
|
|
break;
|
|
}
|
|
|
|
students[*count].id = (char *)malloc(sizeof(char) * 10);
|
|
students[*count].clas = (char *)malloc(sizeof(char) * 20);
|
|
students[*count].name = (char *)malloc(sizeof(char) * 20);
|
|
if (students[*count].id == NULL || students[*count].clas == NULL || students[*count].name == NULL) {
|
|
printf("Memory allocation failed.\n");
|
|
exit(1);
|
|
}
|
|
|
|
printf("Enter ID, Class, Name, Score1, Score2, Score3: ");
|
|
scanf("%s %s %s %lf %lf %lf", students[*count].id, students[*count].clas, students[*count].name,
|
|
&students[*count].score1, &students[*count].score2, &students[*count].score3);
|
|
students[*count].score = (students[*count].score1 + students[*count].score2 + students[*count].score3) / 3;
|
|
|
|
(*count)++;
|
|
printf("Continue input? (yes/no): ");
|
|
scanf("%s", continue_input);
|
|
} while (strcmp(continue_input, "yes") == 0);
|
|
}
|
|
|
|
void delete_student(struct Student students[], int *count) {
|
|
char id[10];
|
|
printf("Enter the ID of the student to delete: ");
|
|
scanf("%s", id);
|
|
|
|
for (int i = 0; i < *count; i++) {
|
|
if (strcmp(students[i].id, id) == 0) {
|
|
free(students[i].id);
|
|
free(students[i].clas);
|
|
free(students[i].name);
|
|
|
|
for (int j = i; j < *count - 1; j++) {
|
|
students[j] = students[j + 1];
|
|
}
|
|
(*count)--;
|
|
printf("Student with ID %s deleted successfully.\n", id);
|
|
return;
|
|
}
|
|
}
|
|
printf("Student with ID %s not found.\n", id);
|
|
}
|
|
|
|
void select_student(struct Student students[], int count) {
|
|
char id[10];
|
|
printf("Enter the ID of the student to select: ");
|
|
scanf("%s", id);
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
if (strcmp(students[i].id, id) == 0) {
|
|
printf("ID: %s, Class: %s, Name: %s, Score1: %.2f, Score2: %.2f, Score3: %.2f, Average Score: %.2f\n",
|
|
students[i].id, students[i].clas, students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].score);
|
|
return;
|
|
}
|
|
}
|
|
printf("Student with ID %s not found.\n", id);
|
|
}
|
|
|
|
void order_student(struct Student students[], int count) {
|
|
for (int i = 0; i < count - 1; i++) {
|
|
for (int j = 0; j < count - i - 1; j++) {
|
|
if (students[j].score < students[j + 1].score) {
|
|
struct Student temp = students[j];
|
|
students[j] = students[j + 1];
|
|
students[j + 1] = temp;
|
|
}
|
|
}
|
|
}
|
|
printf("Students ordered by average score.\n");
|
|
}
|
|
|
|
void output_student(struct Student students[], int count) {
|
|
for (int i = 0; i < count; i++) {
|
|
printf("ID: %s, Class: %s, Name: %s, Score1: %.2f, Score2: %.2f, Score3: %.2f, Average Score: %.2f\n",
|
|
students[i].id, students[i].clas, students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].score);
|
|
}
|
|
}
|
|
void free_students(struct Student students[], int count) {
|
|
for (int i = 0; i < count; i++) {
|
|
free(students[i].id);
|
|
free(students[i].clas);
|
|
free(students[i].name);
|
|
}
|
|
} |