按字符

时间:2017-12-10 20:31:13

标签: c++ function compare increment

我在尝试按字符比较一个文件与另一个字符时遇到了一些困难。下面我有问题的功能。此功能的目标是比较每个字符然后计算比较的效率。我查看了一个类似的线程,但在我的程序运行后,它在第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);
}

1 个答案:

答案 0 :(得分:1)

如果你只想找到这两个字符串的相似之处,我会建议这个简单的解决方案:

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

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