diff --git a/semester2/challenge1/main.c b/semester2/challenge1/main.c index 102fa4b..a18d0f1 100644 --- a/semester2/challenge1/main.c +++ b/semester2/challenge1/main.c @@ -18,28 +18,51 @@ typedef struct Genres struct Genres *next; } Genres; +const char *genreMap[] = {"Action", "Adventure", "Animation", "Children", + "Comedy", "Crime", "Documentary", "Drama", + "Fantasy", "Film-Noir", "Horror", "IMAX", "Musical", + "Mystery", "Romance", "Sci-Fi", "Thriller", + "War", "Western"}; + //输入电影风格返回其映射的id -int movie_style_map(char *style) +int getGenreID(char *style) { - int index = 0; - const char *movies[] = {"Action", "Adventure", "Animation", "Children's", - "Comedy", "Crime", "Documentary", "Drama", - "Fantasy", "Film-Noir", "Horror", "IMAX", "Musical", - "Mystery", "Romance", "Sci-Fi", "Thriller", - "War", "Western"}; + // int index = 0; + // const char *genreMap[] = {"Action", "Adventure", "Animation", "Children's", + // "Comedy", "Crime", "Documentary", "Drama", + // "Fantasy", "Film-Noir", "Horror", "IMAX", "Musical", + // "Mystery", "Romance", "Sci-Fi", "Thriller", + // "War", "Western"}; - while (strcmp(movies[index], style) != 0) - { - index++; + // while (strcmp(genreMap[index], style) != 0) + // { + // index++; + // } + + int found = 0; + int i; + for (i = 0; i < NUM_GENRES - 1; i++) { + if (strcmp(genreMap[i], style) == 0) { + found = 1; + break; + } + } + if (found == 1) { + return i; + } + else { + return -1; } - return index; } // 函数原型 Movies *appendMovies(Movies *last, int id); -int getGenreID(int movieID, Genres ** movies); // 传入movieID,输出风格下标 -void clearArrInt(int* arr, int size); // 将所有元素设为0 +Genres *appendGenres(Genres *head, int id); +void incremendGenres(int count[], int movieID, Genres **movies); // 传入movieID,输出风格下标 +void clearArrInt(int *arr, int size); // 将所有元素设为0 +void parseGenreString(char *inputStr, int movieID, Genres **movies_arr); +// inputStr: "Adventure|IMAX..." movieID: the id to make genre linked list. movies_arr[]: to store the linked list. int main(void) { @@ -59,7 +82,7 @@ int main(void) fgets(buf, 256 * sizeof(char), fp); // 看每个用户喜欢什么电影 - int userID, movieID; + int userID, movieID, rating; char input[64]; int loc, loc1; while (fgets(buf, 256 * sizeof(char), fp) != NULL) @@ -85,72 +108,133 @@ int main(void) loc++; } input[loc1] = '\0'; + loc++; + + // ATOI(3) Linux Programmer's Manual ATOI(3) + // NAMint count[], E + // atoi, atol, atoll - convert a string to an integer + movieID = atoi(input); // printf("movieID: %d\n", movieID); - users[userID] = appendMovies(users[userID], movieID); + + // get ratings + input[0] = '\0'; + loc1 = 0; + while (buf[loc] != ',') + { + input[loc1++] = buf[loc]; + loc++; + } + input[loc1] = '\0'; + rating = atoi(input); + + if (rating >= 3) + { + printf("Rate: %d, userID: %d, movieID: %d\n", rating, userID, movieID); + users[userID] = appendMovies(users[userID], movieID); + } } fclose(fp); - fp = fopen("./movies.csv", 'r'); + fp = fopen("./movies.csv", "r"); // 下标表示电影id Genres *movies[NUM_MOVIES]; - // TODO: 仿照我的代码填充*movies数组 // 李王子写 ok 你写吧 - // - // - for(int i=0;imovieID, movies)] ++; + while (ptr != NULL) + { + incremendGenres(count, ptr->movieID, movies); + printf("Debug: userID: %d, movieID: %d\n", i, ptr->movieID); ptr = ptr->next; } - } - //那个 就是你帮我写的那个数组 我感觉开个一维数组就可以了啊 + // 输出答案 + fprintf(fp, "User id: %d\n", i); + for (int j = 0; j < NUM_GENRES - 1; j++) + { + fprintf(fp, "%s: %d\t", genreMap[j], count[j]); + } + fprintf(fp, "\n"); + } fclose(fp); @@ -182,13 +266,79 @@ Movies *appendMovies(Movies *last, int id) return last; } - -int getGenreID(int movieID, Genres **movies) { +void incremendGenres(int count[], int movieID, Genres **movies) +{ Genres *ptr = movies[movieID]; + while (ptr != NULL) + { + count[ptr->genreID]++; + printf("Debug: movidID: %d, genreID: %d, genre: %s\n", movieID, ptr->genreID, genreMap[ptr->genreID]); + ptr = ptr->next; + } } -void clearArrInt(int* arr, int size) { - for (int i = 0; i < size; i++) { +void clearArrInt(int *arr, int size) +{ + for (int i = 0; i < size; i++) + { arr[i] = 0; } +} + +void parseGenreString(char *inputStr, int movieID, Genres **movies_arr) +{ + // 处理输入的string + int strLen = strlen(inputStr); + char buf[64]; + int locPtr = 0; + printf("Debug: inputStr: %s\n", inputStr); + int genreID; + + for (int i = 0; i < strLen; i++) + { + if (inputStr[i] == '|') + { // time to divide the string and make magic happen + buf[locPtr] = '\0'; + printf("Debug: buf: %s, genreID: %d\n", buf, getGenreID(buf)); + genreID = getGenreID(buf); + if (genreID != -1) + movies_arr[movieID] = appendGenres(movies_arr[movieID], genreID); + locPtr = 0; + } + else + { + buf[locPtr++] = inputStr[i]; + } + } + + // do this again + buf[locPtr] = '\0'; + printf("Debug: buf: %s, genreID: %d\n", buf, getGenreID(buf)); + genreID = getGenreID(buf); + if (genreID != -1) + movies_arr[movieID] = appendGenres(movies_arr[movieID], genreID); + locPtr = 0; +} + +Genres *appendGenres(Genres *head, int id) +{ + Genres *new = malloc(sizeof(Genres)); + + new->genreID = id; + new->next = NULL; + + if (head == NULL) + { + return new; + } + + Genres *ptr = head; + + while (ptr->next != NULL) + { + ptr = ptr->next; + } + ptr->next = new; + + return head; } \ No newline at end of file