C++ 按字符将一个文件与另一个文件进行比较时出现迭代错误

C++ 按字符将一个文件与另一个文件进行比较时出现迭代错误,c++,function,compare,increment,C++,Function,Compare,Increment,我在一个字符一个字符地比较一个文件时遇到了一些困难。下面是我所讨论的函数。此函数的目标是比较每个字符,然后计算比较的效率。我看了一个类似的线程,但在我的程序运行后,它在第38个字符后停止递增。我检查了每个字符,它们都是相同的,所以我不明白为什么会发生这种情况。还有什么建议 float compareFiles(string originalMessage, string decodedMessage) { int totalChar = 0; int sameChar = 0;

我在一个字符一个字符地比较一个文件时遇到了一些困难。下面是我所讨论的函数。此函数的目标是比较每个字符,然后计算比较的效率。我看了一个类似的线程,但在我的程序运行后,它在第38个字符后停止递增。我检查了每个字符,它们都是相同的,所以我不明白为什么会发生这种情况。还有什么建议

float compareFiles(string originalMessage, string decodedMessage)
{
    int totalChar = 0;
    int sameChar = 0;
    int diffChar = 0;

    cout << originalMessage << endl; //original message below
    /* no one would have believed in the last years of the nineteenth century that this world was being watched keenly and closely by intelligences greater than mans and yet as mortal as his own that as men busied themselves about their various concerns they were scrutinised and studied perhaps almost as narrowly as a man with a microscope might scrutinise the transient creatures that swarm and multiply in a drop of water with infinite complacency men went to and fro over this globe about their little affairs serene in their assurance of their empire over matter it is possible that the infusoria under the microscope do the same no one gave a thought to the older worlds of space as sources of human danger or thought of them only to dismiss the idea of life upon them as impossible or improbable it is curious to recall some of the mental habits of those departed days at most terrestrial men fancied there might be other men upon mars perhaps inferior to themselves and ready to welcome a missionary enterprise yet across the gulf of space minds that are to our minds as ours are to those of the beasts that perish intellects vast and cool and unsympathetic regarded this earth with envious eyes and slowly and surely drew their plans against us and early in the twentieth century came the great disillusionment the planet mars i scarcely need remind the reader revolves about the sun at a mean distance of 140000000 miles and the light and heat it receives from the sun is barely half of that received by this world it must be if the nebular hypothesis has any truth older than our world and long before this earth ceased to be molten life upon its surface must have begun its course the fact that it is scarcely one seventh of the volume of the earth must have accelerated its cooling to the temperature at which life could begin it has air and water and all that is necessary for the support of animated existence yet so vain is man and so blinded by his vanity that no writer up to the very end of the nineteenth century expressed any idea that intelligent life might have developed there far or indeed at all beyond its earthly level nor was it generally understood that since mars is older than our earth with scarcely a quarter of the superficial area and remoter from the sun it necessarily follows that it is not only more distant from times beginning but nearer its end */

    cout << decodedMessage << endl; //decoded message below
    /* no one would have believed in the last years of the nineteenth century that this world was being watched keenly and closely by intelligences greater than mans and yet as mortal as his own that as men busied themselves about their various concerns they were scrutinised and studied perhaps almost as narrowly as a man with a microscope might scrutinise the transient creatures that swarm and multiply in a drop of water with infinite complacency men went to and fro over this globe about their little affairs serene in their assurance of their empire over matter it is possible that the infusoria under the microscope do the same no one gave a thought to the older worlds of space as sources of human danger or thought of them only to dismiss the idea of life upon them as impossible or improbable it is curious to recall some of the mental habits of those departed days at most terrestrial men fancied there might be other men upon mars perhaps inferior to themselves and ready to welcome a missionary enterprise yet across the gulf of space minds that are to our minds as ours are to those of the beasts that perish intellects vast and cool and unsympathetic regarded this earth with envious eyes and slowly and surely drew their plans against us and early in the twentieth century came the great disillusionment the planet mars i scarcely need remind the reader revolves about the sun at a mean distance of 140000000 miles and the light and heat it receives from the sun is barely half of that received by this world it must be if the nebular hypothesis has any truth older than our world and long before this earth ceased to be molten life upon its surface must have begun its course the fact that it is scarcely one seventh of the volume of the earth must have accelerated its cooling to the temperature at which life could begin it has air and water and all that is necessary for the support of animated existence yet so vain is man and so blinded by his vanity that no writer up to the very end of the nineteenth century expressed any idea that intelligent life might have developed there far or indeed at all beyond its earthly level nor was it generally understood that since mars is older than our earth with scarcely a quarter of the superficial area and remoter from the sun it necessarily follows that it is not only more distant from times beginning but nearer its end */

    for(int q = 0; q <= originalMessage.length(); q++)
    {
            if(decodedMessage[q] == originalMessage[q])
            {
                    sameChar++;
            }
            else
            {
                    diffChar++;
            }
            totalChar++;
    }

    return ((float(sameChar)/float(totalChar))*100.00);
}
浮点比较文件(字符串原始消息、字符串解码消息)
{
int totalChar=0;
int-sameChar=0;
int diffChar=0;

cout如果你只是想知道这两个字符串有多相似,我建议使用以下简单的解决方案:

float compareFiles(const std::string& original, const std::string& decoded) {
    size_t length = original.size();
    size_t errorCount = 0;

    for (size_t i = 0; i < length; ++i)
        if (original[i] != decode[i])
            ++errorCount;

    return 1.0f - float(errorCount)/float(lenght);
}
float compareFiles(常量std::string和original,常量std::string和decoded){
size_t length=original.size();
大小\u t错误计数=0;
对于(尺寸i=0;i

另外,如果这两条消息是您从磁盘读取的真实文件,我建议您使用streams解决此问题,因为这是它最初读取的格式,而不是将其转换为字符串。

此程序解决了一个Caesar密码。这两块无意义的文本只是原始文件的真实含义和我的文本解码版本。很抱歉,我想我应该包括它。@Ron
q
否则您将超出范围。这是一个凯撒密码,两条消息的长度应该相等,但这并不意味着它们每次都相等。更好的安全性是抱歉。由于密码的工作方式,它们的长度不能不同,因为它用不同的字符替换字符,因此几乎可以保证消息是same length。我已进行了编辑,以不检查字符串大小。此解决方案对您有效吗?在我的程序中,我删除了所有标点符号和大写字母。此函数的目标尤其是计算字母转换的效率。我真的不知道如何使用此建议答案中使用的内容。我不知道属性尺寸为\u t或1.0f