#include <algorithm>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

const int size = 23; //矩阵大小

int sgn(int i)       //返回i的符号
{
	if (i > 0)
		return 1;
	else if (i < 0)
		return -1;
	else
		return 0;
}

double correlation(const string &sub_path,const string &obj_path)
{
	//建立主观矩阵和客观矩阵
	int sub[size][size] = {0},obj[size][size] = {0};
	char comma = ',';							      //用于读取逗号分隔符
	ifstream fin;									  //建立输入流用于读入主客观矩阵csv数据
	//读入主观矩阵-好坏人数对比
	fin.open(sub_path.c_str());
	if (sub_path.empty() || !fin)
	{
		AfxMessageBox("CANNOT OPEN SUB_FILE!");
		return 0;
	}
	for (int i = 0;i < size;++i)
	{
		for (int j = 0;j < size;++j)
		{
			fin >> sub[i][j];
			if (j != size - 1)
			fin >> comma;
		}
	}
	fin.close();
	fin.clear();

	//读入客观矩阵-01矩阵，表示任意两个视频名次高低
	fin.open(obj_path.c_str());
	if (obj_path.empty() || !fin)
	{
		AfxMessageBox("CANNOT OPEN OBJ_FILE!");
		return 0;
	}
	for (int i = 0;i < size;++i)
	{
		for (int j = 0;j < size;++j)
		{
			fin >> obj[i][j];
			if (j != size - 1)
				fin >> comma;
		}
	}
	fin.close();
	fin.clear();
	
	//开始计算。结果为[-1,1]的值（越大表示相关度越高）。
	double res = 0;
	for (int i = 0;i < size;++i)
	{
		for (int j = 0;j < size;++j)
		{
			if (obj[i][j] == 1 && i != j)
			{
				if (sub[i][j] + sub[j][i] != 0)
					res += static_cast<double>(std::max<int>(sub[i][j],sub[j][i])) / (sub[i][j] + sub[j][i]) * sgn(sub[i][j] - sub[j][i]);
			}
		}
	}
	res /= (size * (size - 1) / 2);

	return res;
}
