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.

162 lines
4.2 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char id[20];
char clas[20];
char name[20];
double score1;
double score2;
double score3;
double score;
};
struct Student* students[1000];
int studentCount = 0;
void printMenu() {
printf("1. Input\n");
printf("2. Delete\n");
printf("3. Select\n");
printf("4. Order\n");
printf("5. Output\n");
printf("6. Quit\n");
printf("Please input your option: ");
}
void inputStudent() {
char continueInput[4];
do {
if (studentCount < 1000) {
struct Student* student = (struct Student*)malloc(sizeof(struct Student));
printf("Id: ");
scanf("%s", student->id);
printf("Class: ");
scanf("%s", student->clas);
printf("Name: ");
scanf("%s", student->name);
printf("Score1: ");
scanf("%lf", &student->score1);
printf("Score2: ");
scanf("%lf", &student->score2);
printf("Score3: ");
scanf("%lf", &student->score3);
student->score = student->score1 + student->score2 + student->score3;
students[studentCount++] = student;
printf("continue?\n");
scanf("%s", continueInput);
} else {
printf("Student database is full!\n");
break;
}
} while (strcmp(continueInput, "yes") == 0);
}
void deleteStudent() {
char deleteId[20];
printf("Enter student ID or name to delete: ");
scanf("%s", deleteId);
int deleted = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i]->id, deleteId) == 0 || strcmp(students[i]->name,deleteId)==0) {
free(students[i]);
for (int j=i;j<studentCount-1;j++) {
students[j]=students[j+1];
}
studentCount--;
deleted=1;
i--;
}
}
if (deleted==0) {
printf("Student not found.\n");
}
}
void selectStudent() {
char query[20];
printf("Enter student ID or class to select: ");
scanf("%s", query);
int found=0;
for (int i=0;i<studentCount;i++) {
if (strcmp(students[i]->id,query)==0||strcmp(students[i]->clas,query)==0) {
printf("%s, %s, %s, %.1lf, %.1lf, %.1lf, %.1lf\n", students[i]->id, students[i]->clas, students[i]->name, students[i]->score1, students[i]->score2, students[i]->score3, students[i]->score);
found=1;
}
}
if (found==0) {
printf("There is no eligible student.\n");
}
}
void orderStudents() {
for (int i = 0; i < studentCount; i++) {
for (int j = i + 1; j < studentCount; j++) {
if (strcmp(students[i]->clas, students[j]->clas) > 0 || (strcmp(students[i]->clas, students[j]->clas) == 0 && students[i]->score < students[j]->score)) {
struct Student* temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void outputStudents() {
for (int i = 0; i < studentCount; i++) {
printf("%s, %s, %s, %.1lf, %.1lf, %.1lf, %.1lf\n", students[i]->id, students[i]->clas, students[i]->name, students[i]->score1, students[i]->score2, students[i]->score3, students[i]->score);
}
}
int main() {
int option;
do {
printMenu();
scanf("%d", &option);
switch (option) {
case 1:
inputStudent();
break;
case 2:
deleteStudent();
break;
case 3:
selectStudent();
break;
case 4:
orderStudents();
outputStudents();
break;
case 5:
outputStudents();
break;
case 6:
break;
default:
printf("Invalid option. Please try again.\n");
}
} while (option != 6);
for (int i = 0; i < studentCount; i++) {
free(students[i]);
}
return 0;
}