Bonsoir;
j'ai un fichier csv de type string, il contient pluieurs lignes et une seul colonne, chaque case contient un nom,
je veux calculer le coefficient de dice sur ce fichier, la lecture du fichier marche bien, j'ai meme fait des test pour vérifier si la lecture est correcte, mais en utilisant la fonction de dice, le résultat n'est pas correct, en faisant des test d'affichage sur les variables array[i][0], le résultat est vide

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdlib>
#include <iostream>
#include<set>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
vector<vector<string> > array_read(istream &filename);
static double DiceMatch(string string1, string string2);
string array[30][30];
float result;
 
/************* lire le fichier **********************************************************/
vector<vector<string> > array_read(istream &filename)
{
 
    vector< std::vector<string> > array;
    std::vector<string> array_line;
 
    std::string csv_line, word;
    string value;
 
    while ( getline(filename, csv_line) ) {
        array_line.clear();
        std::stringstream ss(csv_line);
 
        //std::cout << "line: " << csv_line << std::endl;
 
        while ( getline(ss, word, ';') ) {
            //value = strtod(word.c_str(), NULL);
            //std::cout << "*) value: " << value << std::endl;
 
            array_line.push_back(word);
            cout<<word<<endl;
        }
 
        array.push_back(array_line);
    }
    return array;
}
 
/***************************   DICE  ************************************************/
//static double DiceMatch(string string1, string string2)
 
static double DiceMatch(istream &filename)
 
{
for(int i=0;i<2;++i)
for(int j=i+1;j<2;++j)
{cout<<"1="<<array[i][0]<<endl;
 
	if (array[i][0].empty() || array[j][1].empty())
       {cout<<"1="<<array[i][0]<<endl;
		return 0;
    }
 
	if (array[i][0] == array[j][1])
	{cout<<"1="<<array[i][0]<<endl;
		return 1;
}
	size_t strlen1 = array[i][0].size();
	size_t strlen2 = array[j][1].size();
 
	if (strlen1 < 2 || strlen2 < 2)
		return 0;
 
	size_t length1 = strlen1 - 1;
	size_t length2 = strlen2 - 1;
 
	double matches = 0;
	int i = 0;
	int j = 0;
 
	while (i < length1 && j < length2)
	{
		string a = array[i][0].substr(i, 2);
		string b = array[j][1].substr(j, 2);
		int cmp = a.compare(b);
 
		if (cmp == 0)
			matches += 2;
 
		++i;
		++j;
	}
 
	return matches / (length1 + length2);
}
}
/**********************  MAIN*****************************************************************/
int main(int argc, char *argv[])
{
    ifstream filename("chaine.csv");
    if (!filename) {
        std::cerr << "main - error: can't open file " << filename << std::endl;
 
        return EXIT_FAILURE;
    }
 
vector<vector<string> > array=array_read( filename);
 
  for (int i=0;i<2;++i)
  {
      //for (int j=i+1;j<2;++j)
      {
        cout<<"d="<<array[i][0]<<endl;
        }
      } 
      double result = DiceMatch(filename);
      //cout<<result<<endl;
 
    system("PAUSE");
    return EXIT_SUCCESS;
}