Programme du coefficient de DICE ne s’exécute pas

Fermé
benkharrouba - 7 juil. 2020 à 20:46
 benkharrouba - 8 juil. 2020 à 10:32
Bonjour,

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

#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;
}




Configuration: Windows / Chrome 83.0.4103.97

4 réponses

Dalfab Messages postés 706 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 2 novembre 2023 101
7 juil. 2020 à 21:18
Bonjour,

ligne 10, tu crées une variable nommée array, qui ne sera jamais remplie.
Ligne 17, une autre variable nommée array est créée, elle est remplie correctement.
Ligne 100, encore une variable nommée array est créée, elle aussi est remplie correctement.
Mais c'est la variable vide de la ligne 10 qui est utilisé dans la fonction DiceMatch()
0
Bonsoir;

Merci Dalfab pour ta réponse;
j'ai ajouter la ligne 10, la déclaration de la variable array, parceque lors de l'éxecution une erreur est afficher: "`array' undeclared (first use this function) "
mais sinon je comprend pas ce qu'il faut faire
0
Dalfab Messages postés 706 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 2 novembre 2023 101
8 juil. 2020 à 09:37
Déjà, ce qu'il ne faut pas faire : créer des variables globales.
Pour qu'un fonction aie accès à une variables, le plus trivial c'est de passer en paramètre la variable
Tu peux passer en paramètre le tableau à la fonction DiceMatch (qui reçoit le flux fichier mais on se sait pas pourquoi!), utiliser une référence constante souhaitable.
0
Bonjour;
merci Dalfab pour ton aide,
voici les changement que j'ai fait:

double DiceMatch(vector<vector<string> >array)
double result = DiceMatch(array);

le programme s'execute et il m'affiche correctement le contenu de la variable array, mais il m'affiche pas la valeur de la variable result

#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(vector<vector<string>>array);
//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)
 double DiceMatch(vector<vector<string> >array)
{
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);
      double result = DiceMatch(array);
      cout<<result<<endl;
         
    system("PAUSE");
    return EXIT_SUCCESS;
}
0