2010-03-19 3 views
0

이 계산을 위해 다음과 같은 포럼이 주어졌다.이진 코사인 계수

sim = | Q∩D |/√ | Q | √ | D |

나는 단어

#pragma once 

#include <vector> 
#include <string> 
#include <iostream> 
#include <vector> 

using namespace std; 

class StringSet 
{ 
public: 
StringSet(void); 
StringSet(const string the_strings[], const int no_of_strings); 
~StringSet(void); 
StringSet(const vector<string> the_strings); 
void add_string(const string the_string); 
bool remove_string(const string the_string); 
void clear_set(void); 
int no_of_strings(void) const; 
friend ostream& operator <<(ostream& outs, StringSet& the_strings); 
friend StringSet operator *(const StringSet& first, const StringSet& second); 
friend StringSet operator +(const StringSet& first, const StringSet& second); 
double binary_coefficient(const StringSet& the_second_set); 

private: 
vector<string> set; 
}; 

#include "StdAfx.h" 
#include "StringSet.h" 
#include <iterator> 
#include <algorithm> 
#include <stdexcept> 
#include <iostream> 
#include <cmath> 


StringSet::StringSet(void) 
{ 
} 

StringSet::~StringSet(void) 
{ 
} 

StringSet::StringSet(const vector<string> the_strings) 
{ 
set = the_strings; 
} 

StringSet::StringSet(const string the_strings[], const int no_of_strings) 
{ 
copy(the_strings, &the_strings[no_of_strings], back_inserter(set)); 
} 

void StringSet::add_string(const string the_string) 
{ 
try 
{ 
    if(find(set.begin(), set.end(), the_string) == set.end()) 
    { 
    set.push_back(the_string); 
    } 
    else 
    { 
    //String is already in the set. 
    throw domain_error("String is already in the set"); 
    } 
} 
catch(domain_error e) 
{ 
    cout << e.what(); 
    exit(1); 
} 

} 

bool StringSet::remove_string(const string the_string) 
{ 
//Found the occurrence of the string. return it an iterator pointing to it. 
vector<string>::iterator iter; 
if((iter = find(set.begin(), set.end(), the_string)) != set.end()) 
{ 
    set.erase(iter); 
    return true; 
} 
return false; 
} 
void StringSet::clear_set(void) 
{ 
set.clear(); 
} 

int StringSet::no_of_strings(void) const 
{ 
return set.size(); 
} 

ostream& operator <<(ostream& outs, StringSet& the_strings) 
{ 
vector<string>::const_iterator const_iter = the_strings.set.begin(); 
for(; const_iter != the_strings.set.end(); const_iter++) 
{ 
    cout << *const_iter << " "; 
} 
cout << endl; 
return outs; 
} 

//This function returns the union of the two string sets. 

StringSet operator *(const StringSet& first, const StringSet& second) 
{ 
vector<string> new_string_set; 
new_string_set = first.set; 
for(unsigned int i = 0; i < second.set.size(); i++) 
{ 
    vector<string>::const_iterator const_iter = find(new_string_set.begin(), new_string_set.end(), second.set[i]); 
    //String is new - include it. 
    if(const_iter == new_string_set.end()) 
    { 
    new_string_set.push_back(second.set[i]); 
    } 
} 
StringSet the_set(new_string_set); 
return the_set; 
} 
//This method returns the intersection of the two string sets. 

StringSet operator +(const StringSet& first, const StringSet& second) 
{ 
//For each string in the first string look though the second and see if 
//there is a matching pair, in which case include the string in the set. 
vector<string> new_string_set; 
vector<string>::const_iterator const_iter = first.set.begin(); 
for (; const_iter != first.set.end(); ++const_iter) 
{ 
    //Then search through the entire second string to see if 
    //there is a duplicate. 
    vector<string>::const_iterator const_iter2 = second.set.begin(); 
    for(; const_iter2 != second.set.end(); const_iter2++) 
    { 
    if(*const_iter == *const_iter2) 
    { 
    new_string_set.push_back(*const_iter); 
    } 
    } 
} 
StringSet new_set(new_string_set); 
return new_set; 

} 

double StringSet::binary_coefficient(const StringSet& the_second_set) 
{ 
double coefficient; 
StringSet intersection = the_second_set + set; 

coefficient = intersection.no_of_strings()/sqrt((double) no_of_strings()) * sqrt((double)the_second_set.no_of_strings()); 
return coefficient; 
} 

의 시리즈로 구성된 문자열을 비교하는 클래스를 아 헤드 가서 구현 그러나 나는 시도하고 다음과 같은 주요 기능을 사용하여 계수를 계산할 때 내가 얻을

// Exercise13.cpp : main project file. 

#include "stdafx.h" 
#include <boost/regex.hpp> 
#include "StringSet.h" 

using namespace System; 
using namespace System::Runtime::InteropServices; 

using namespace boost; 

//This function takes as input a string, which 
//is then broken down into a series of words 
//where the punctuaction is ignored. 



StringSet break_string(const string the_string) 
{ 
regex re; 
cmatch matches; 
StringSet words; 
string search_pattern = "\\b(\\w)+\\b"; 

try 
{ 
    // Assign the regular expression for parsing. 
    re = search_pattern; 
} 
catch(regex_error& e) 
{ 
    cout << search_pattern << " is not a valid regular expression: \"" 
    << e.what() << "\"" << endl; 
    exit(1); 
} 

sregex_token_iterator p(the_string.begin(), the_string.end(), re, 0); 
sregex_token_iterator end; 
for(; p != end; ++p) 
{ 
    string new_string(p->first, p->second); 
    String^ copy_han = gcnew String(new_string.c_str()); 
    String^ copy_han2 = copy_han->ToLower(); 
    char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(copy_han2); 
    string new_string2(str2); 
    words.add_string(new_string2); 
} 


return words; 


} 

int main(array<System::String ^> ^args) 
{ 
StringSet words = break_string("Here is a string, with some; words"); 
StringSet words2 = break_string("There is another string,"); 

cout << words.binary_coefficient(words2); 
    return 0; 
} 

을 0에서 1 사이의 값이 아니라 1.5116 인 인덱스.

누구에게 이것이 사실인지 실마리가 있습니까?

도움을 주시면 감사하겠습니다.

+0

Visual Studio에서이 기능을 사용하려면 Windows CLI 콘솔 응용 프로그램을 만들어야합니다. 왜냐하면 광기로 일부를 사용하기로 결정했기 때문입니다. – hairyyak

+0

질문이 해결되면 체크 표시를 사용하여 공식 답변을 작성하여 가장 유용한 답변을 수락하십시오 (이전 질문에 대한 대답을 고려하십시오). –

답변

0

은 아마 문제

coefficient = intersection.no_of_strings()/sqrt((double) no_of_strings()) * sqrt((double)the_second_set.no_of_strings()); 

는 분할 후, 먼저 곱해야한다는 지정하지 않습니다 단지 우선 순위입니다. 그들의 우선 순위는 동일하지만 .. 선택이 끝난 행동에 대해 확실하지 않다 당신이 그것을 지정하려고했던 :

coefficient = intersection.no_of_strings()/(sqrt((double) no_of_strings()) * sqrt((double)the_second_set.no_of_strings())); 
2

당신은 최종 계산에 더 많은 괄호가 필요합니다. a/b * c(a/b) * c으로 파싱되지만 a/(b * c)이 필요합니다.

+0

아, 너는 유머러스하다. 나는 결코 그것을 발견하지 못했을 것이다. – hairyyak

+0

나는 내가 수식을 이해하지 못한다고 생각했기 때문에 하루 종일 웹 검색을 보냈다. – hairyyak

+0

@hairyyak : 귀하가 효과가 있다면 답변을 수락하십시오. 응답을 받고 응답하는 것은 SO 커뮤니티의 다른 회원에게 도움이 될 수 있습니다. –