如何简单比较 [英] How to simple compare

查看:69
本文介绍了如何简单比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从两个文件中读取每个字符:PISMENA.TXT和PISMENA2.txt,然后比较它们(一个来自PISMENA,一个来自PISMENA2)。但我在这一部分有一个问题:



I need to read every character from a two files: PISMENA.TXT and PISMENA2.txt and then compare them (one from the PISMENA and one from PISMENA2). But I have a problem in this part:

while(((fscanf(fr_pismena,"%c",&character_pismena))!=EOF)&&((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF)){
     printf("\nZ prveho suboru:%c"
            "\nZ druheho suboru:%c",character_pismena, character_pismena2);
     if(character_pismena!=character_pismena2)
       count_of_differences++;
     else
     ;
   }



因为当PISMENA.TXT有一个(例如)13个字符而PISMENA2.TXT有14个字符的时候程序结束后是第13个字符。

如果两个文件的字符数相同,那么一切都还可以......



对不起我的英文,我希望你理解...如果您需要完整的代码,我可以发布它。

谢谢


because when the PISMENA.TXT has got a (for example) a 13 characters and PISMENA2.TXT has got a 14 characters then end of the program is after 13th character.
When both files has got same number of characters then everything is ok...

Sorry for my english, I hope, you understand... If you need a full code, I can post it.
Thanks

推荐答案

稍后再创建两个while块,然后单独检查每个文件的EOF。不是EOF的那个将被char扫描char并自动计为差异。



Create two more while blocks after the while checking each file singularly for EOF. The one which is not EOF will be scanned char by char and automatically be counted as difference.

// ... (your code at the moment)

// When you are here you will have at most ONE file which is not EOF,
// so only ONE of the loops will be executed
while ((fscanf(fr_pismena,"%c",&character_pismena))!=EOF){
    count_of_differences++;
}

while ((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF){
    count_of_differences++;
}


或初始化 count_of_differences 是两个长度的差异而不是零。



如果这不是家庭作业,你可能更愿意研究Levenshtein距离。

http://en.wikipedia.org/wiki/Levenshtein_distance [ ^ ]
Or initialize count_of_differences to be the difference of the two lengths rather than zero.

If this is not a homework assignment, you might prefer to look into Levenshtein Distance.
http://en.wikipedia.org/wiki/Levenshtein_distance[^]


den2k88 非常感谢, PIEBALDconsult ! :)



den2k88 我有一个下一个短路评估问题(&&),这就是为什么我用不同的方式写的,但你的方式非常帮助我(我用它),所以非常感谢!



现在完整代码://任何捷克语:D
den2k88 Thank you very much, PIEBALDconsult too ! :)

den2k88 I had a one next problem with short-circuit evaluation (&&), that's why i wrote it differently, but your way helped me very much (I used it), so thx very much !

Now full code: //anything in czech language :D
#include <stdio.h>
#include <conio.h>
#include <time.h>

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

int main()
{
    FILE *fr_pismena,*fr_pismena2;
    char character_pismena;
    char character_pismena2;
    int count_of_differences=0;

    openfile_1:
    if((fr_pismena=fopen("C:\\Users\\Maroš\\Documents\\Programming\\C\\Projects\\Exercises\\86.2\\PISMENA.TXT","r")) == NULL){
      printf("\nERROR: Program can't open the file 'PISMENA.TXT' for read. "
             "Press 'Enter' to try again or 'q' to close the program.");

      repair_open_1:
      switch(getchar()){
      case 'q':return 0;break;
      case '\012':goto openfile_1;
      default    :printf("Incorrect character, try again..");goto repair_open_1;
      }
    }
    else
      printf("\nThe file PISMENA.TXT is successfully opened.\n\n");


    openfile_2:
    if((fr_pismena2=fopen("C:\\Users\\Maroš\\Documents\\Programming\\C\\Projects\\Exercises\\86.2\\PISMENA2.TXT","r")) == NULL){
      printf("\nERROR: Program can't open the file 'PISMENA2.TXT' for read. "
             "Press 'Enter' to try again or 'q' to close the program.");

      repair_open_2:
      switch(getch()){
      case 'q':return 0;break;
      case '\012':goto openfile_2;
      default    :printf("Incorrect character, try again..");goto repair_open_2;
      }
    }
    else
      printf("\nThe PISMENA2.TXT is successfully opened.\n\n");

    while(1){
      if(fscanf(fr_pismena,"%c",&character_pismena)!=EOF){
        if(fscanf(fr_pismena2,"%c",&character_pismena2)!=EOF){
          printf("\nZ prveho suboru:%c"
                 "\nZ druheho suboru:%c",character_pismena, character_pismena2);
          if(character_pismena!=character_pismena2)
            count_of_differences++;
          else
          ;
        }
        else{
          ungetc(character_pismena,fr_pismena);
          break;
        }
      }
      else{
        break;
      }
    }

    while((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF){
      count_of_differences++;
    }
    while ((fscanf(fr_pismena,"%c",&character_pismena))!=EOF){
      count_of_differences++;
    }

    if(count_of_differences==0)
      printf("\n\nSoubory jsou zhodne\n");
    else
      printf("\n\nSoubory se lisi v %d znacich\n",count_of_differences);

    fclose(fr_pismena);
    fclose(fr_pismena2);

    getch();
    return 0;

}
</time.h></conio.h></stdio.h>


这篇关于如何简单比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆