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.

149 lines
4.3 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_LEN 20
struct Student {
char id[MAX_LEN];
char clas[MAX_LEN];
char name[MAX_LEN];
double score1;
double score2;
double score3;
double score;
} students[MAX_STUDENTS];
int studentCount = 0;
void inputStudent() {
if (studentCount >= MAX_STUDENTS) {
printf("Database is full.\n");
return;
}
struct Student s;
printf("Id: ");
scanf("%s", s.id);
printf("class: ");
scanf("%s", s.clas);
printf("name: ");
scanf("%s", s.name);
printf("score1: ");
scanf("%lf", &s.score1);
printf("score2: ");
scanf("%lf", &s.score2);
printf("score3: ");
scanf("%lf", &s.score3);
s.score = s.score1 + s.score2 + s.score3;
students[studentCount++] = s;
printf("continue? (yes/no): ");
char cont[3];
scanf("%s", cont);
if (strcmp(cont, "no") == 0 || strcmp(cont, "No") == 0 || strcmp(cont, "NO") == 0) {
return;
}
else
{
inputStudent() ;
}
return;
}
void deleteStudent() {
char searchIdOrName[MAX_LEN];
printf("Enter student id or name to delete: ");
scanf("%s", searchIdOrName);
int found = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].id, searchIdOrName) == 0 || strcmp(students[i].name, searchIdOrName) == 0) {
for (int j = i; j < studentCount - 1; j++) {
students[j] = students[j + 1];
}
studentCount--;
found = 1;
i--;
}
}
if (!found) {
printf("There is no eligible student.\n");
}
printf("continue? (yes/no): ");
char cont[3];
scanf("%s", cont);
if (strcmp(cont, "no") != 0) {
deleteStudent();
}
}
void selectStudent() {
char searchIdOrClass[MAX_LEN];
printf("Enter student id or class to select: ");
scanf("%s", searchIdOrClass);
int found = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].id, searchIdOrClass) == 0 || strcmp(students[i].clas, searchIdOrClass) == 0) {
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].score);
found = 1;
}
}
if (!found) {
printf("There is no eligible student.\n");
}
printf("continue? (yes/no): ");
char cont[3];
scanf("%s", cont);
if (strcmp(cont, "yes") == 0 || strcmp(cont, "Yes") == 0 || strcmp(cont, "YES") == 0) {
selectStudent();
}
}
void orderStudents() {
// Simple bubble sort for demonstration purposes
for (int i = 0; i < studentCount - 1; i++) {
for (int j = 0; j < studentCount - i - 1; j++) {
if (strcmp(students[j].clas, students[j + 1].clas) > 0 ||
(strcmp(students[j].clas, students[j + 1].clas) == 0 && students[j].score < students[j + 1].score)) {
struct Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
void outputStudents() {
for (int i = 0; i < studentCount; 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].score);
}
}
int main() {
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:
inputStudent();
break;
case 2:
deleteStudent();
break;
case 3:
selectStudent();
break;
case 4:
orderStudents();
break;
case 5:
outputStudents();
break;
case 6:
printf("Exiting program.\n");
break;
default:
printf("Invalid option.\n");
}
} while (option != 6);
return 0;
}