了解逻辑 [英] Understanding the logic

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

问题描述

我试图了解以下C ++代码,需要将其重新编写为C#.
我的问题是,如果我输入字母A,我希望单词是B,但它却显示为Z.有人可以解释其功能.

在此先感谢

I am trying to understand the following C++ Code which I need to re-write into C#
My questions is that if I pass in the letter A I expect word to be B but instead it comes out as Z. Can someone explain the functionality.

Thanks in advance

char alphabet[]   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void mkyScramble(char *word)
{
    unsigned int pos, last_pos, algo;
    char c[2];

    last_pos = 0;
    c[1] = '\0';

    while(*word)
    {
        strncpy(c, word, 1);
        pos = (unsigned int)strcspn(alphabet, c);

        if (pos == 26)
            word++;
        else
        {
            algo = pos + last_pos;
            if (algo > 24) algo -= 26;

            *word++ = alphabet[algo + 1];

            last_pos = pos;
        }
    }
}

推荐答案

algo = pos + last_pos;


这是违规行,因为它使用last_pos的值来计算偏移量.因此,字符串"AYA"将显示为"BZZ",因为在转换第二个"A"时,last_pos的值将为24("Y"的位置),从而将"A"转换为"Z".也许您的问题应该是:此代码的目的是什么?";答案是我不知道".


This is the offending line as it uses the value of last_pos to calculate the offset. Thus the string "AYA" will appear as "BZZ" because the value of last_pos when converting the second "A" will be 24 (the position of "Y") and thus it will convert "A" to "Z". Maybe your question should be: "What is the purpose of this code?"; the answer being "I have no idea".


传递仅包含字母"A"的字符串时,将返回"B".每个输出字母都是从其自身的偏移量加上前一个字母的偏移量再加上一个包装字符(以避免溢出)构成的.
When passing a string containing only the letter ''A'', ''B'' will be returned. Each output letter is build from its own offset plus the offset of the previous letter plus one with wrapping to avoid overflows.


我怀疑这是一种简单的 ^ ]:
这是一种替换密码,其中明文中的每个字母都被一个字母替换为字母表中固定位置的字母.例如,以3的偏移量,A将被D取代,B将变为E,等等.该方法以尤利乌斯·凯撒(Julius Caesar)命名,他在私人通讯中使用了该方法."
I suspect this is a kind of simple Caesar cipher[^]:
"It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it in his private correspondence."


这篇关于了解逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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