BUPT-homework/semester1/pset14/2-4-database.c

148 lines
3.5 KiB
C
Raw Normal View History

2021-12-16 17:27:53 +08:00
#include <stdio.h>
#include <string.h>
#pragma GCC optimize("Ofast,inline")
#define MAX 11
#define CLASS 3
typedef struct STUDENT {
char name[MAX];
char id[MAX];
int scores[CLASS];
} STUDENT;
int parser(int operation, STUDENT *students, int size);
int match(char *id, STUDENT *students, int size);
// Returns a location of students if found, else return -1
int add(char *id, char *name, int *scores, STUDENT *students, int size); // 1
int delete (char *id, STUDENT *students, int size); // 2
void update(char *id, int *scores, STUDENT *students, int size); // 3
void display(char *id, STUDENT *students, int size); // 4
int main(void) {
int operations;
scanf("%d", &operations);
STUDENT students[operations];
int operation;
int size = 0;
for (int i = 0; i < operations; i++) {
scanf("%d", &operation);
size += parser(operation, students, size);
}
return 0;
}
int parser(int operation, STUDENT *students, int size) {
if (operation == 1) {
char id[MAX];
char name[MAX];
int scores[CLASS];
scanf("%s%s", id, name);
/* printf("id:%s, name:%s\n", id, name); */
for (int i = 0; i < CLASS; i++) {
scanf("%d", &scores[i]);
/* printf("Score[%d]: %d\n", i, scores[i]); */
}
return add(id, name, scores, students, size);
} else if (operation == 2) {
char id[MAX];
scanf("%s", id);
return delete (id, students, size);
} else if (operation == 3) {
char id[MAX];
int scores[CLASS];
scanf("%s", id);
for (int i = 0; i < CLASS; i++) {
scanf("%d", &scores[i]);
}
update(id, scores, students, size);
return 0;
} else {
char id[MAX];
scanf("%s", id);
display(id, students, size);
return 0;
}
}
int add(char *id, char *name, int *scores, STUDENT *students, int size) { // 1
// returns if Students already exist
if (match(id, students, size) != -1) {
printf("Students already exist\n");
return 0;
}
// Add data to the location "size"
strcpy(students[size].id, id);
strcpy(students[size].name, name);
for (int i = 0; i < CLASS; i++) {
students[size].scores[i] = scores[i];
}
printf("Add success\n");
return 1;
}
int delete (char *id, STUDENT *students, int size) { // 2
// Returns if student doesn't exist
int loc = match(id, students, size);
if (loc == -1) {
printf("Students do not exist\n");
return 0;
}
// Shift the array left.
for (int i = loc; i < size - 1; i++) {
students[i] = students[i + 1];
}
printf("Delete success\n");
return -1;
}
void update(char *id, int *scores, STUDENT *students, int size) { // 3
int loc = match(id, students, size);
if (loc == -1) {
printf("Students do not exist\n");
return;
}
// Update the student
for (int i = 0; i < CLASS; i++) {
students[loc].scores[i] = scores[i];
}
printf("Update success\n");
}
void display(char *id, STUDENT *students, int size) { // 4
int loc = match(id, students, size);
if (loc == -1) {
printf("Students do not exist\n");
return;
}
// Display the average score
int sum = 0;
for (int i = 0; i < CLASS; i++) {
/* printf("score[%d]: %d\n", i, students[loc].scores[i]); */
sum += students[loc].scores[i];
}
printf("Student ID:%s\nName:%s\nAverage Score:%.1f\n", id, students[loc].name,
(float)sum / CLASS);
}
int match(char *id, STUDENT *students, int size) {
for (int i = 0; i < size; i++) {
if (strcmp(id, students[i].id) == 0) { // matched
return i;
}
}
return -1;
}