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.
223 lines
6.2 KiB
223 lines
6.2 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 inputStudent(struct Student students[], int *count);
|
|
void deleteStudent(struct Student students[], int *count);
|
|
void selectStudent(struct Student students[], int count);
|
|
void orderStudent(struct Student students[], int count);
|
|
void outputStudent(struct Student students[], int count);
|
|
void freeStudentMemory(struct Student student);
|
|
int isValidId(const char *id);
|
|
int isValidScore(double score);
|
|
|
|
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\n");
|
|
printf("please input your option\n");
|
|
scanf("%d", &option);
|
|
|
|
switch (option) {
|
|
case 1:
|
|
inputStudent(students, &count);
|
|
break;
|
|
case 2:
|
|
deleteStudent(students, &count);
|
|
break;
|
|
case 3:
|
|
selectStudent(students, count);
|
|
break;
|
|
case 4:
|
|
orderStudent(students, count);
|
|
break;
|
|
case 5:
|
|
outputStudent(students, count);
|
|
break;
|
|
case 6:
|
|
printf("Exiting program...\n");
|
|
break;
|
|
default:
|
|
printf("Invalid option. Please try again.\n");
|
|
}
|
|
} while (option!= 6);
|
|
int i;
|
|
for (i = 0; i < count; i++) {
|
|
freeStudentMemory(students[i]);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void inputStudent(struct Student students[], int *count) {
|
|
char continueInput;
|
|
do {
|
|
students[*count].id = (char *)malloc(sizeof(char) * 10);
|
|
students[*count].clas = (char *)malloc(sizeof(char) * 10);
|
|
students[*count].name = (char *)malloc(sizeof(char) * 20);
|
|
|
|
printf("Id ");
|
|
scanf("%s", students[*count].id);
|
|
if (!isValidId(students[*count].id)) {
|
|
printf("\n");
|
|
free(students[*count].id);
|
|
continue;
|
|
}
|
|
|
|
printf("class ");
|
|
scanf("%s", students[*count].clas);
|
|
|
|
printf("name ");
|
|
scanf("%s", students[*count].name);
|
|
|
|
printf("score1 ");
|
|
scanf("%lf", &students[*count].score1);
|
|
if (!isValidScore(students[*count].score1)) {
|
|
printf("\n");
|
|
continue;
|
|
}
|
|
|
|
printf("score2 ");
|
|
scanf("%lf", &students[*count].score2);
|
|
if (!isValidScore(students[*count].score2)) {
|
|
printf("\n");
|
|
continue;
|
|
}
|
|
|
|
printf("score3 ");
|
|
scanf("%lf", &students[*count].score3);
|
|
if (!isValidScore(students[*count].score3)) {
|
|
printf("\n");
|
|
continue;
|
|
}
|
|
|
|
students[*count].score = students[*count].score1 + students[*count].score2 + students[*count].score3;
|
|
|
|
(*count)++;
|
|
|
|
printf("continue? \n");
|
|
scanf(" %c", &continueInput);
|
|
} while (continueInput == 'y');
|
|
}
|
|
|
|
void deleteStudent(struct Student students[], int *count) {
|
|
char input[20];
|
|
int i, j;
|
|
do {
|
|
printf("Enter the student ID or name to delete: ");
|
|
scanf("%s", input);
|
|
|
|
for (i = 0; i < *count; i++) {
|
|
if (strcmp(students[i].id, input) == 0 || strcmp(students[i].name, input) == 0) {
|
|
freeStudentMemory(students[i]);
|
|
for (j = i; j < *count - 1; j++) {
|
|
students[j] = students[j + 1];
|
|
}
|
|
(*count)--;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (i == *count) {
|
|
printf("Student not found.\n");
|
|
} else {
|
|
printf("Student deleted successfully.\n");
|
|
}
|
|
|
|
outputStudent(students, *count);
|
|
|
|
printf("continue? \n");
|
|
scanf("%s", input);
|
|
} while (strcmp(input,"yes") == 0);
|
|
}
|
|
|
|
void selectStudent(struct Student students[], int count) {
|
|
char input[20];
|
|
int i;
|
|
do {
|
|
rewind(stdin);
|
|
printf("Enter the student ID or class to select: ");
|
|
scanf("%s", input);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
if (strcmp(students[i].id, input) == 0 || strcmp(students[i].clas, input) == 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);
|
|
}
|
|
}
|
|
|
|
if (i == count) {
|
|
printf("there is no eligible student\n");
|
|
}
|
|
|
|
printf("continue? \n");
|
|
scanf("%s", input);
|
|
} while (strcmp(input,"yes") == 0);
|
|
}
|
|
|
|
void orderStudent(struct Student students[], int count) {
|
|
int i, j;
|
|
struct Student temp;
|
|
|
|
for (i = 0; i < count - 1; i++) {
|
|
for (j = 0; j < count - 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)) {
|
|
temp = students[j];
|
|
students[j] = students[j + 1];
|
|
students[j + 1] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
outputStudent(students, count);
|
|
}
|
|
|
|
void outputStudent(struct Student students[], int count) {
|
|
int i;
|
|
for (i = 0; i < count; 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);
|
|
}
|
|
}
|
|
|
|
void freeStudentMemory(struct Student student) {
|
|
free(student.id);
|
|
free(student.clas);
|
|
free(student.name);
|
|
}
|
|
|
|
int isValidId(const char *id) {
|
|
if (strlen(id)!= 5) {
|
|
return 0;
|
|
}
|
|
int i;
|
|
for ( i = 0; i < 5; i++) {
|
|
if (id[i] < '0' || id[i] > '9') {
|
|
return 0;
|
|
}
|
|
}
|
|
return id[0]!= '0';
|
|
}
|
|
|
|
int isValidScore(double score) {
|
|
int integerPart = (int)score;
|
|
double decimalPart = score - integerPart;
|
|
if (integerPart < 0 || integerPart > 99) {
|
|
return 0;
|
|
}
|
|
if (decimalPart < 0 || decimalPart >= 1) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|