【问题标题】:Arrays in C and Prime NumbersC 和素数中的数组
【发布时间】:2015-08-30 10:28:06
【问题描述】:

我的编程任务需要帮助,我已经完成了除了一部分之外的所有工作!这是作业:

问题1:字符串操作和数据安全:为了提高数据和信息传输中的数据安全性,正在实践动态字符编码。原始字符的修改可以使用前 8 个素数成员 [1, 2, 3, 5, 7, 11, 13, 17]:第一个字符增强 1;第二个字符加 2,第三个字符加 3,.. 第 8 个字符加 17。接下来的 8 个字符以相反的顺序 17..1 使用质数,并减小值。使用至少 64 个字符的总消息 [8 个字符的数量] 并重复前 8 个修改 1-17 的过程;为接下来的 8 修改 17 -1,依此类推。制作自己的信息。消息编码后,还要进行解码,恢复原来的消息。您可能还想更改小写和大写转换。

   Example:  Original Message           A     B     C    D. …..
                     Normal ASCII      65    66    67   68   ….
                     Prime Numbers      1     2     3    5   ….          
                     Enhanced ASCII    66    68    70   73 ….
                     Coded Message      B     D     F    I   ……


我在打印素数时遇到了麻烦,之后又不会弄乱代码,例如编码和解码的 ASCII 代码以及编码和解码的代码。到目前为止,这是我的代码: 如果你能以某种方式帮助我,那就太好了!我真的很感激,谢谢。

size_t x;
int i, c;

i = 1;
c = 0;

char text[65];
int s[8] = {1, 2, 3, 5, 7, 11, 13, 17};

printf("Enter a line of text: "); // prompts user to enter text
fgets(text, 65, stdin); // reads input from user
printf("\nOriginal Message: ");
for(x = 0; x < strlen(text); ++x) // for loop to print original message
{
    printf("%c", text[x]);
} // end for

// ASCII Code
printf("\nASCII Code: ");
for(x = 0; x < strlen(text) - 1; ++x)
{
    printf("%d ", text[x]);
}

// prime numbers
// my issue is here
printf("\n\nPrime Numbers: ");
for(x = 0; x <= 8 ; ++x)
{
    if(c == 0)
    {
        if(i == 1)
        {
            printf("1 ");
            ++i;
        }
        else if(i == 2)
        {
            printf("2 ");
            ++i;
        }
        else if(i == 3)
        {
            printf("3 ");
            ++i;
        }
        else if(i == 4)
        {
            printf("5 ");
            ++i;
        }
        else if(i == 5)
        {
            printf("7 ");
            ++i;
        }
        else if(i == 6)
        {
            printf("11 ");
            ++i;
        }
        else if(i == 7)
        {
            printf("13 ");
            ++i;
        }
        else if(i == 8)
        {
            printf("17 ");
            ++c;
            ++x;
        }
    }
    if(c == 1)
    {
        if(i == 8)
        {
            printf("17 ");
            --i;
        }
        else if(i == 7)
        {
            printf("13 ");
            --i;
        }
        else if(i == 6)
        {
            printf("11 ");
            --i;
        }
        else if(i == 5)
        {
            printf("7 ");
            --i;
        }
        else if(i == 4)
        {
            printf("5 ");
            --i;
        }
        else if(i == 3)
        {
            printf("3 ");
            --i;
        }
        else if(i == 2)
        {
            printf("2 ");
            --i;
        }
        else if(i == 1)
        {
            printf("1 ");
            --c;
        }
    } // end outer if
} // issue ends here


for(x = 0; x < strlen(text) - 1; ++x)
{
    if(c == 0) // outer if statement increasing
    {
        if(i == 1) // inner if statement
        {
            text[x] = text[x] + 1;
            ++i;
        }
        else if (i == 2)
        {
            text[x] = text[x] + 2;
            ++i;
        }
        else if (i == 3)
        {
            text[x] = text[x] + 3;
            ++i;
        }
        else if(i == 4)
        {
            text[x] = text[x] + 5;
            ++i;
        }
        else if(i == 5)
        {
            text[x] = text[x] + 7;
            ++i;
        }
        else if(i == 6)
        {
            text[x] = text[x] + 11;
            ++i;
        }
        else if(i == 7)
        {
            text[x] = text[x] + 13;
            ++i;
        }
        else if(i == 8)
        {
            text[x] = text[x] + 17;
            ++c;
            ++x;
        } // end if statement
    } // end outer if statement
    if(c == 1) // outer if statement decreasing
    {
        if(i == 8)
        {
            text[x] = text[x] + 17;
            --i;
        }
        else if(i == 7)
        {
            text[x] = text[x] + 13;
            --i;
        }
        else if(i == 6)
        {
            text[x] = text[x] + 11;
            --i;
        }
        else if(i == 5)
        {
            text[x] = text[x] + 7;
            --i;
        }
        else if(i == 4)
        {
            text[x] = text[x] + 5;
            --i;
        }
        else if(i == 3)
        {
            text[x] = text[x] + 3;
            --i;
        }
        else if(i == 2)
        {
            text[x] = text[x] + 2;
            --i;
        }
        else if(i == 1)
        {
            text[x] = text[x] + 1;
            --c;
        }
    } // end outer if
} // end for
printf("\n\nEncrypted Message: ");
for(x = 0; x <= strlen(text) - 1; ++x)
{
   printf("%c", text[x]);
}

printf("\nEncrypted ASCII: ");
for(x = 0; x < strlen(text) - 1; x++)
{
    printf("%d ", text[x]);
}

c = 0;
i = 1;

for(x = 0; x < strlen(text) - 1; ++x)
{
    if(c == 0)
    {
        if(i == 1)
        {
            text[x] = text[x] - 1;
            ++i;
        }
        else if(i == 2)
        {
            text[x] = text[x] - 2;
            ++i;
        }
        else if(i == 3)
        {
            text[x] = text[x] - 3;
            ++i;
        }
        else if(i == 4)
        {
            text[x] = text[x] - 5;
            ++i;
        }
        else if(i == 5)
        {
            text[x] = text[x] - 7;
            ++i;
        }
        else if(i == 6)
        {
            text[x] = text[x] - 11;
            ++i;
        }
        else if(i == 7)
        {
            text[x] = text[x] - 13;
            ++i;
        }
        else if(i == 8)
        {
            text[x] = text[x] - 17;
            ++c;
            ++x;
        } // end if statement
    } // end outer if statement
    if(c == 1)
    {
        if(i == 8)
        {
            text[x] = text[x] - 17;
            --i;
        }
        else if(i == 7)
        {
            text[x] = text[x] - 13;
            --i;
        }
        else if(i == 6)
        {
            text[x] = text[x] - 11;
            --i;
        }
        else if(i == 5)
        {
            text[x] = text[x] - 7;
            --i;
        }
        else if(i == 4)
        {
            text[x] = text[x] - 5;
            --i;
        }
        else if(i == 3)
        {
            text[x] = text[x] - 3;
            --i;
        }
        else if(i == 2)
        {
            text[x] = text[x] - 2;
            --i;
        }
        else if(i == 1)
        {
            text[x] = text[x] - 1;
            --c;
        } // end inner if statements
    } // end outer if statements
} // end for

printf("\n\nDecrypted Message: ");
for(x = 0; x < strlen(text); ++x)
{
    printf("%c", text[x]);
}

printf("\nDecrypted ASCII: ");
for(x = 0; x < strlen(text) - 1; ++x)
{
    printf("%d ", text[x]);
}

puts(" "); } // 结束主程序

【问题讨论】:

  • 你能用输出例子描述你的程序哪里出错了吗?在这里乱扔一大堆代码,要求找bug,有点粗鲁。
  • 我编辑了我的帖子并指定了问题出在哪里使用 cmets,基本上我正在尝试打印素数,但我不能,因为它弄乱了我的其余代码,我想知道如果有另一种打印素数的方法,这样它就不会搞砸了?例如,如果用户输入“工程”,则它必须输出为“1, 2, 3, 5, 7, 11, 13, 17, 17, 15, 13”。它每 8 个字符增加到 17 个,然后在接下来的 8 个字符中减少。但是我一直在尝试解决这个问题,但它一直在弄乱我之后的解密和加密代码。
  • 哦!我会发布输出的照片,但它不允许我:/

标签: c arrays numbers variable-assignment


【解决方案1】:

字符由数字表示。例如,对于 ASCII,z 由数字 122 表示。如果向该值添加一些内容,则会得到一个新值(例如 122 + 17 = 139)。该新值可能不是有效的 ASCII(例如 128 或更大),并且可能是控制代码(例如 127 是“删除”控制字符)。

因为这些值可能不再代表有效字符,所以您不能将它们打印为字符。您需要将它们打印为值(例如for(x = 0; text[x] != 0; x++) { printf("%d ", text[x]); })。

更糟糕的是,对于“signed char”,添加可能会导致溢出,这是 C 中未定义的行为。您需要使用不同的数据类型(例如 unsigned charuint8_t)以确保代码已定义/预期的行为。

还请注意,您的“if .. else if ... else if”语句的长链应该是带有 case 的“switch()”;更像这样:

    switch(i) {
        case 1:
            text[x] = text[x] + 1;
            ++i;
            break;
        case 2:
            text[x] = text[x] + 2;
            ++i;
            break;
        ...
    }

但是;通过使用int s[16] = {1, 2, 3, 5, 7, 11, 13, 17, 17, 13, 11, 7, 5, 3, 2, 1};,您可以将许多类似的代码行替换为以下内容:

    for(x = 0; text[x] != 0; x++) {
        text[x] += s[x % 16]);
    }

【讨论】:

    猜你喜欢
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    相关资源
    最近更新 更多