194 lines
4 KiB
C
194 lines
4 KiB
C
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <stdlib.h>
|
||
|
||
#define NUM_USERS 611
|
||
#define NUM_MOVIES 9743
|
||
#define NUM_GENRES 20
|
||
|
||
typedef struct Movies
|
||
{
|
||
int movieID;
|
||
struct Movies *next;
|
||
} Movies;
|
||
|
||
typedef struct Genres
|
||
{
|
||
int genreID;
|
||
struct Genres *next;
|
||
} Genres;
|
||
|
||
//输入电影风格返回其映射的id
|
||
int movie_style_map(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"};
|
||
|
||
while (strcmp(movies[index], style) != 0)
|
||
{
|
||
index++;
|
||
}
|
||
return index;
|
||
}
|
||
|
||
// 函数原型
|
||
Movies *appendMovies(Movies *last, int id);
|
||
int getGenreID(int movieID, Genres ** movies); // 传入movieID,输出风格下标
|
||
void clearArrInt(int* arr, int size); // 将所有元素设为0
|
||
|
||
int main(void)
|
||
{
|
||
// Firstly open the ratings.csv
|
||
FILE *fp;
|
||
fp = fopen("./ratings.csv", "r");
|
||
|
||
char *buf = malloc(256 * sizeof(char));
|
||
|
||
// Declare the users table and init with null.
|
||
Movies *users[NUM_USERS];
|
||
for (int i = 0; i < NUM_USERS; i++)
|
||
{
|
||
users[i] = NULL;
|
||
}
|
||
|
||
fgets(buf, 256 * sizeof(char), fp);
|
||
|
||
// 看每个用户喜欢什么电影
|
||
int userID, movieID;
|
||
char input[64];
|
||
int loc, loc1;
|
||
while (fgets(buf, 256 * sizeof(char), fp) != NULL)
|
||
{
|
||
loc = 0;
|
||
input[0] = '\0'; // clear the array
|
||
// get userID
|
||
while (buf[loc] != ',')
|
||
{
|
||
input[loc] = buf[loc];
|
||
loc++;
|
||
}
|
||
input[loc++] = '\0';
|
||
userID = atoi(input);
|
||
// printf("UserID: %d\n", userID);
|
||
|
||
// get movieID
|
||
loc1 = 0;
|
||
input[0] = '\0';
|
||
while (buf[loc] != ',')
|
||
{
|
||
input[loc1++] = buf[loc];
|
||
loc++;
|
||
}
|
||
input[loc1] = '\0';
|
||
movieID = atoi(input);
|
||
// printf("movieID: %d\n", movieID);
|
||
users[userID] = appendMovies(users[userID], movieID);
|
||
}
|
||
|
||
fclose(fp);
|
||
|
||
fp = fopen("./movies.csv", 'r');
|
||
|
||
// 下标表示电影id
|
||
Genres *movies[NUM_MOVIES];
|
||
|
||
|
||
// TODO: 仿照我的代码填充*movies数组
|
||
// 李王子写 ok 你写吧
|
||
//
|
||
|
||
//
|
||
for(int i=0;i<NUM_MOVIES;i++)
|
||
{
|
||
movies[i]=NULL;
|
||
}
|
||
|
||
|
||
char*str=(char*)malloc(256*sizeof(char));
|
||
|
||
//读第一行
|
||
fgets(str, 256 * sizeof(char), fp);
|
||
|
||
// fgets() returns s on success, and NULL on error or when end of file
|
||
// occurs while no characters have been read.
|
||
// 你那个该写!=NULL 你看注释
|
||
// ok
|
||
|
||
|
||
int id_index=0;
|
||
|
||
while(fgets(str,256*sizeof(char),fp)!=NULL)
|
||
{
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
fclose(fp);
|
||
|
||
// 开始输出答案到文件answer.txt
|
||
|
||
fp = fopen("./answer.txt", 'w+'); // w+: 如果文件已存在先清空
|
||
|
||
int count[NUM_GENRES] = {};
|
||
|
||
// 开始从第一个用户循环 (1)
|
||
Movies *ptr;
|
||
for (int i = 1; i < NUM_GENRES; i++) {
|
||
cleararrInt(count, NUM_GENRES);
|
||
// 便历他喜欢的电影
|
||
ptr = users[i];
|
||
while (ptr != NULL) {
|
||
count[getGenreID(ptr->movieID, movies)] ++;
|
||
ptr = ptr->next;
|
||
}
|
||
}
|
||
|
||
//那个 就是你帮我写的那个数组 我感觉开个一维数组就可以了啊
|
||
|
||
fclose(fp);
|
||
|
||
return 0;
|
||
}
|
||
|
||
Movies *appendMovies(Movies *last, int id)
|
||
{
|
||
Movies *new = malloc(sizeof(Movies));
|
||
|
||
new->movieID = id;
|
||
new->next = NULL;
|
||
|
||
// 1.如果是null
|
||
if (last == NULL)
|
||
{
|
||
return new;
|
||
}
|
||
// 2.不是null,在最后加上新节点
|
||
Movies *ptr = last;
|
||
|
||
while (ptr->next != NULL)
|
||
{
|
||
ptr = ptr->next;
|
||
}
|
||
|
||
ptr->next = new;
|
||
|
||
return last;
|
||
}
|
||
|
||
|
||
int getGenreID(int movieID, Genres **movies) {
|
||
Genres *ptr = movies[movieID];
|
||
}
|
||
|
||
void clearArrInt(int* arr, int size) {
|
||
for (int i = 0; i < size; i++) {
|
||
arr[i] = 0;
|
||
}
|
||
} |