Untitled

 avatar
unknown
plain_text
a month ago
4.0 kB
6
Indexable
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 1000
typedef struct
{
	char studentID[11];
	char submitDate[11];
	char submitTime[10];
	int result;
} LOG;

int readLog (LOG *logArr) {
	int count = 0;
	while (1) {
		LOG temp;
		scanf("%s", temp.studentID);
		if (strcmp(temp.studentID, "-1") == 0) break;
		scanf("%s%s%d", temp.submitDate, temp.submitTime, &temp.result);
		getchar();
		logArr[count++] = temp;
	}
	return count;
}

int numberofSubmits(LOG *logArr, int size) {
	int count = 1;
	for (int i = 1; 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;
}

char* maximumNumberofSubmitPerStudent(LOG *logArr, int size) {
	char **studentIDs = (char **)malloc(size * sizeof(char *));
	int run = 0;
	for (int i = 0; i < size; i++) {
		int check = 1;
		for (int j = i - 1; j >= 0; j--) {
			if (strcmp(logArr[j].studentID, logArr[i].studentID) == 0) {
			check = 0;
			break;
			}
		}
		if (check) {
			studentIDs[run] = (char *)malloc(11 * sizeof(char));
			strcpy(studentIDs[run], logArr[i].studentID);
			run++;
		}
	}
	
	int *count = (int *)calloc(run, sizeof(int));
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < run; j++) {
			if (strcmp(studentIDs[j], logArr[i].studentID) == 0) {
				count[j]++;
				break;
			}
		}
	}
	
	int max_index = 0;
	for (int i = 0; i < run; i++) {
		if (count[i] > count[max_index]) {
			max_index = i;
		}
	}
	char *maxStudentID = (char *)malloc(11 * sizeof(char));
	strcpy(maxStudentID, studentIDs[max_index]);
	for (int i = 0; i < run; i++) {
		free(studentIDs[i]);
	}
	
	free(studentIDs);
	free(count);
	return maxStudentID;
}

void minMaxPoint(LOG *logArr, int size, int* minPoint, int* maxPoint) {
	*minPoint = 1e6, *maxPoint = -1;
	for (int i = 0; i < size; i++) {
		if (logArr[i].result > *maxPoint) {
			*maxPoint = logArr[i].result;
		}
		if (logArr[i].result < *minPoint) {
			*minPoint = logArr[i].result;
		}
	}
}

int getResultofStudentId(LOG *logArr, int size, const char* studentID) {
	int max = 0;
	for (int i = 0; i < size; i++) {
		if (strcmp(logArr[i].studentID, studentID) == 0 && logArr[i].result > max) {
			max = logArr[i].result;
		}
	}
	return max;
}

void getStudentResult(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(logArr,size,nextStudentID);
		printf("StudentId %s result %d\n",nextStudentID,result);
	}
}

int main()
{
	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], "readLog") == 0) {
		size = readLog(logArr);
       }
       else if (strcmp(&nextCommand[2], "numberofSubmits") == 0) {
           int numSubmits = numberofSubmits(logArr,size);
           printf("Number of Submits: %d\n",numSubmits);
       }
       else if (strcmp(&nextCommand[2], "maximumNumberofSubmitPerStudent") == 0) {
           char *studentID = maximumNumberofSubmitPerStudent(logArr,size);
           printf("The ID of student who submits the most times: %s\n",studentID);
       }
       else if (strcmp(&nextCommand[2], "minMaxPoint") == 0) {
           int min, max;
           minMaxPoint(logArr, size, &min, &max);
           printf("Point min: %d, max: %d\n", min, max);
       }
       else if (strcmp(&nextCommand[2], "getResultofStudentId") == 0) {
           getStudentResult(logArr, size);
       }
   }
   free(logArr);
   return 0;
}
Leave a Comment