Untitled
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_SIZE 1000 #define MAX_QUESTION_SIZE 10 typedef struct { char studentID[11]; char questionID[50]; char submitDate[11]; char submitTime[10]; int result; } LOG; void readExamDetail(char lstExamQuestions[][50], int* numQuestions) { int index = 0; char nextLine[255]; while (1) { fgets(nextLine, sizeof(nextLine), stdin); nextLine[strcspn(nextLine, "\r\n")] = 0; if (strcmp(nextLine, "-1") == 0) break; strcpy(lstExamQuestions[index], nextLine); index++; } *numQuestions = index; } int readLog(LOG* logArr) { int lineCount = 0; char nextLine[255]; while (1) { fgets(nextLine, sizeof(nextLine), stdin); nextLine[strcspn(nextLine, "\r\n")] = 0; if (strcmp(nextLine, "-1") == 0) break; sscanf(nextLine, "%s %s %s %s %d", logArr[lineCount].studentID, logArr[lineCount].questionID, logArr[lineCount].submitDate, logArr[lineCount].submitTime, &logArr[lineCount].result); lineCount++; } return lineCount; } int numberofSubmits(LOG* logArr, int size) { int count = 0; for (int i = 0; i < size; i++) { count++; for (int j = i - 1; j >= 0; j--) { if (strcmp(logArr[j].studentID, logArr[i].studentID) == 0) { count--; break; } } } return count; } int getResultofStudentId(char lstExamQuestions[][50], int numQuestions, LOG* logArr, int size, const char* studentID) { int sum = 0; for (int i = 0; i < numQuestions; i++) { int temp = 0; for (int j = 0; j < size; j++) { if (strcmp(logArr[j].studentID, studentID) == 0) { if (strcmp(logArr[j].questionID, lstExamQuestions[i]) == 0 && logArr[j].result > temp) { temp = logArr[j].result; } } } sum += temp; } return sum; } void printStudentExamDetail(char lstExamQuestions[][50], int numQuestions, LOG* logArr, int size, const char* studentID) { int *mark = (int *)calloc(MAX_SIZE, sizeof(int)); for (int i = 0; i < numQuestions; i++) { int temp = 0; int index = 0; int check = 0; // check xem cau hoi nay no co trong cai log khong for (int j = 0; j < size; j++) { if (strcmp(logArr[j].studentID, studentID) == 0) { if (strcmp(logArr[j].questionID, lstExamQuestions[i]) == 0 && logArr[j].result > temp) { temp = logArr[j].result; index = j; check = 1; } } } if (check) mark[index]++; } for (int i = 0; i < MAX_SIZE; i++) { if (mark[i]) { printf("%s %s %s %d\n", logArr[i].questionID, logArr[i].submitDate, logArr[i].submitTime, logArr[i].result); } } free(mark); } void printSubmitStatistic(char lstExamQuestions[][50], int numQuestions, LOG* logArr, int size) { for (int i = 0; i < numQuestions; i++) { int sum = 0, count = 0; for (int j = 0; j < size; j++) { if (strcmp(logArr[j].questionID, lstExamQuestions[i]) == 0) { sum += logArr[j].result; count++; } } double average = 0.00; if (count == 0) average == 0.00; else average = (double)sum / count; printf("%s %d %.2lf\n", lstExamQuestions[i], count, average); } } void getStudentResult(char lstExamQuestions[][50], int numQuestions, LOG* logArr, int size) { char nextStudentID[255]; while (1) { fgets(nextStudentID, sizeof(nextStudentID), stdin); nextStudentID[strcspn(nextStudentID, "\r\n")] = 0; if (strcmp(nextStudentID, "$") == 0) break; int result = getResultofStudentId(lstExamQuestions, numQuestions, logArr, size, nextStudentID); printf("StudentId %s result %d\n", nextStudentID, result); } } void getStudentSubmitDetail(char lstExamQuestions[][50], int numQuestions, LOG* logArr, int size) { char nextStudentID[255]; while (1) { fgets(nextStudentID, sizeof(nextStudentID), stdin); nextStudentID[strcspn(nextStudentID, "\r\n")] = 0; if (strcmp(nextStudentID, "$") == 0) break; printStudentExamDetail(lstExamQuestions, numQuestions, logArr, size, nextStudentID); } } int main() { char lstExamQuestions[10][50]; int lstQuestionSize = 0; LOG* logArr = NULL; int size; logArr = (LOG*)malloc(MAX_SIZE * sizeof(LOG)); char nextCommand[100]; while (1) { fgets(nextCommand, sizeof(nextCommand), stdin); nextCommand[strcspn(nextCommand, "\r\n")] = 0; if (strlen(nextCommand) == 0) continue; if (nextCommand[0] != '?') break; if (strcmp(&nextCommand[2], "loadLogSubmit") == 0) { size = readLog(logArr); } else if (strcmp(&nextCommand[2], "loadExamQuestions") == 0) { readExamDetail(lstExamQuestions, &lstQuestionSize); printf("Number of Questions: %d\n", lstQuestionSize); } else if (strcmp(&nextCommand[2], "numberofSubmits") == 0) { printf("Number of submit: %d\n", numberofSubmits(logArr, size)); } else if (strcmp(&nextCommand[2], "getSubmitStatistic") == 0) { printSubmitStatistic(lstExamQuestions, lstQuestionSize, logArr, size); } else if (strcmp(&nextCommand[2], "getStudentResults") == 0) { getStudentResult(lstExamQuestions, lstQuestionSize, logArr, size); } else if (strcmp(&nextCommand[2], "getStudentSubmitDetail") == 0) { getStudentSubmitDetail(lstExamQuestions, lstQuestionSize, logArr, size); } } free(logArr); return 0; }
Leave a Comment