Функция:
public float simpleSimilarity(String u, String v) {
String[] a = u.split(" ");
String[] b = v.split(" ");
long correct = 0;
int minLen = Math.min(a.length, b.length);
for (int i = 0; i < minLen; i++) {
String aa = a[i];
String bb = b[i];
int minWordLength = Math.min(aa.length(), bb.length());
for (int j = 0; j < minWordLength; j++) {
if (aa.charAt(j) == bb.charAt(j)) {
correct++;
}
}
}
return (float) (((double) correct) / Math.max(u.length(), v.length()));
}
Тест:
String a = "This is the first string.";
String b = "this is not 1st string!";
// for exact string comparison, use .equals
boolean exact = a.equals(b);
// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote
float similarity = simple_similarity(a,b);
Ваш журнал valgrind показывает «fscanf первых двух» как часть вывода программы. Эта часть происходит, когда вызов fscanf
не выполняется внутри цикла. Вы звоните exit
, но не закрываете файл. Сначала нужно закрыть файл:
if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
{
fprintf(stderr,"fscanf of first two\n");
fclose(fp);
free(test);
exit(EXIT_FAILURE);
}
void lectureFichier(const char * nomFichier, message ** tete)
{
message * test=creationCellule();
FILE * fp = fopen(nomFichier, "r");
if(!fp)
{
perror("fopen failed\n");
exit( EXIT_FAILURE );
}
while(fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin))==2)
{
// Here you are reading again. That's not what you want.
// if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
// {
// fprintf(stderr,"fscanf of first two\n");
// free(test);
// exit(EXIT_FAILURE);
// }
fgets(test->text,100,fp);
insertion(tete,test);
test=creationCellule();
}
// Here you can free the last test, because it is not used
free(test);
fclose(fp);
}
Если ваша проблема связана с утечками памяти, вам следует использовать free (fb) для динамически размещаемых указателей.