#include <stdio.h>
#include <string.h>
#define SIZE 129
#define LEN 11
// student record structure
typedef struct Student
{
char ID[LEN];
char name[LEN];
int math;
int eng;
int cs;
} STUDENT;
// check if the record is in the system
// nedded by all the 4 functions
int locate(STUDENT stu[SIZE], char sid[LEN], int count);
// Followings are 4 functionalities
void addRecord(STUDENT stu[SIZE], char sid[LEN], int *count);
void deleteRecord(STUDENT stu[SIZE], char sid[LEN], int *count);
void updateRecord(STUDENT stu[SIZE], char sid[LEN], int count);
void printRecord(STUDENT stu[SIZE], char sid[LEN], int count);
int main()
{
STUDENT stu[SIZE];
int op;
char sid[LEN];
int i, n;
int count = 0; // Total number of records in the system
scanf("%d", &n); // how many actions to do
for (i = 0; i < n; i++)
{
// read in ops and studentID in order to determine what to do
scanf("%d%s", &op, sid);
if (op == 1)
{
addRecord(stu, sid, &count);
}
else if (op == 2)
{
deleteRecord(stu, sid, &count);
}
else if (op == 3)
{
updateRecord(stu, sid, count);
}
else // op == 4
{
printRecord(stu, sid, count);
}
}
return 0;
}
int locate(STUDENT stu[SIZE], char sid[LEN], int count)
{
int loc = 0;
while (loc < count)
{
if (strcmp(stu[loc].ID, sid) == 0)
return loc;
loc++;
}
return -1; // means no record
}
void addRecord(STUDENT stu[SIZE], char sid[LEN], int *count)
{
int loc;
loc = locate(stu, sid, *count);
// read and put the record at the end of the array temporarily
// NECESSARY!!! even if the record already exists
strcpy(stu[*count].ID, sid);
scanf("%s", stu[*count].name);
scanf("%d%d%d", &stu[*count].math, &stu[*count].eng, &stu[*count].cs);
if (loc == -1)
{
// if not already exist, increment count to make the record valid
*count += 1;
printf("Add success\n");
}
else
{
printf("Students already exist\n");
}
}
void deleteRecord(STUDENT stu[SIZE], char sid[LEN], int *count)
{
int i, loc;
loc = locate(stu, sid, *count);
if (loc != -1)
{
// if the record exists, delete it
// by shift the following records one index up,
// and then decrement count by 1
for (i = loc; i < *count - 1; i++)
{
stu[i] = stu[i + 1];
}
*count -= 1;
printf("Delete success\n");
}
else
{
printf("Students do not exist\n");
}
}
void updateRecord(STUDENT stu[SIZE], char sid[LEN], int count)
{
int loc;
loc = locate(stu, sid, count);
if (loc != -1)
{
// if the record exists, update by reading new values
scanf("%d%d%d", &stu[loc].math, &stu[loc].eng, &stu[loc].cs);
printf("Update success\n");
}
else
{
// abandon input value by putting them after the end of list
// NECESSARY!!!
scanf("%d%d%d", &stu[count].math, &stu[count].eng, &stu[count].cs);
printf("Students do not exist\n");
}
}
void printRecord(STUDENT stu[SIZE], char sid[LEN], int count)
{
int loc;
loc = locate(stu, sid, count);
if (loc != -1)
{
// if record exists, print out student info
printf("Student ID:%s\nName:%s\n", stu[loc].ID, stu[loc].name);
printf("Average Score:%.1f\n", (stu[loc].math + stu[loc].eng + stu[loc].cs) / 3.0);
}
else
{
printf("Students do not exist\n");
}
}