c - 编码/解码程序的逻辑错误 (C)

标签 c decode encode

我是 C 编程类(class)的初学者,我们教科书第三章的作业是创建一个编码/解码程序,该程序从用户输入中获取四位数字,并根据用户是否想要重新排列它们它们被编码或解码。我也无法让程序在一个文件中进行解码和编码。。然而,当我启动程序时,每当我选择编码并输入四位数字时,程序不仅会给出一个应该只有四位数字的大量数字作为编码结果。它还给我“按任意键继续”提示,表示程序结束,而不是完全重新开始并返回到要求用户解码或编码的地方。

输出也已附上。


我尝试环顾四周并将一些变量从 int 更改为 double,并检查我的数学...我不是 100% 确定为什么加密和解密不限于四位数字之外的任何内容。

至于让程序重新开始,我尝试过使用 do-while 循环,但它似乎仍然不起作用,所以我删除了它。我还尝试使用另一个 if 语句,然后使用 break ,但它被标记为错误。


作业中说:“每个数字加7,除以10后取余数进行加密;加密每个数字后,交换第一和第三位数字,然后交换第二和第四位数字。解密应该相反该过程。程序必须在一个文件中进行编码和解码。

这是我的讲师为此发布的示例,这就是输出的样子。

Enter a four digit number: 1234
Encoded Digits: 0189
Continue (1) Exit (0): 1
Encode (1) Decode (2): 2
Enter a four digit number: 0189
Decoded Digits: 1234
Continue (1) Exit (0): 0

这是我的代码:

///Compiler used: Microsoft Visual Studio
///Language: C
#include <string>
#pragma warning(disable: 4996) 
#include <math.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{

    int Encodechoice = 1;
    int inputdigit1 = 0;
    int Decodechoice = 2;
    int UserChoice = 0;
    int ContinueChoice = 0;
    int UserDigit1 = 0;
    int UserDigit2 = 0;
    int UserDigit3 = 0;
    int UserDigit4 = 0;

    {
        printf("(1) Encode, (2) Decode\n");
        printf("Make your selection.\n");//Asks user to make selection
        scanf_s("%d", &UserChoice);

        if (UserChoice == 1)//begin encryption

        {
            UserDigit1 = 0;
            UserDigit2 = 0;
            UserDigit3 = 0;
            UserDigit4 = 0;
            printf("Encode: Enter FOUR integers.\n");
            scanf_s("%d", &UserDigit1, &UserDigit2, &UserDigit3, &UserDigit4);
            int EncodedIntegers1 = (UserDigit1 + 7) % 10;
            int EncodedIntegers2 = (UserDigit2 + 7) % 10;
            int EncodedIntegers3 = (UserDigit3 + 7) % 10;
            int EncodedIntegers4 = (UserDigit4 + 7) % 10;
            printf("Encoded result:\n");
            printf("%d", "%d", "%d", "%d\n", EncodedIntegers3, &EncodedIntegers4, &EncodedIntegers1, &EncodedIntegers2); /// swap order of integers
        }///end if

        if (UserChoice == 2)///begin decryption
        {
            UserDigit1 = 0;
            UserDigit2 = 0;
            UserDigit3 = 0;
            UserDigit4 = 0;
            printf("Decode: Enter FOUR integers.\n");
            scanf_s("%d", &UserDigit1, &UserDigit2, &UserDigit3, &UserDigit4);
            int DecodedIntegers1 = (UserDigit1 - 7) * 10;
            int DecodedIntegers2 = (UserDigit2 - 7) * 10;
            int DecodedIntegers3 = (UserDigit3 - 7) * 10;
            int DecodedIntegers4 = (UserDigit4 - 7) * 10;
            printf("Decoded result:\n");
            printf("%d", "%d", "%d", "%d\n", DecodedIntegers1, &DecodedIntegers2, &DecodedIntegers3, &DecodedIntegers4); /// keep order the same to decrypt
        }///end if
        system("pause");
        return 0;
    }
}

输出:

Make your selection.
1
Encode: Enter FOUR integers.
1234
Encoded result:
11893608 Continue? (1) Yes, (0) No
Make your selection.
1
Press any key to continue . . .```


最佳答案

你的scanf是错误的。我宁愿这样做

char UserInputNumber[5];
printf("Encode: Enter FOUR integers.\n");
scanf("%s", UserInputNumber);
// optional error checking the input: length is 4? All char is a number?
UserDigit1 = (UserInputNumber[0] - '0');
UserDigit2 = (UserInputNumber[1] - '0');
UserDigit3 = (UserInputNumber[2] - '0');
UserDigit4 = (UserInputNumber[3] - '0');

您的 printf 应该如下所示:

printf("%d%d%d%d\n", EncodedIntegers3, EncodedIntegers4, EncodedIntegers1, EncodedIntegers2);

使用&,您尝试打印它们的内存地址而不是它们的值,但您的格式字符串也错误。

关于c - 编码/解码程序的逻辑错误 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60316347/

相关文章:

c - 如何在 GTK3 中处理鼠标移动事件?

json - Swift 将 json 数据获取到结构中并填充 pickerViews

java - 如何从 json 解码 ascii 代码

opencv - 为什么 cv::imencode 为 bgr8 和 mono8 图像提供相似大小的输出缓冲区?

c++ - cudamallocmanaged 是否足够聪明,不会复制不需要的数据?

c - SDL Audio - 只播放静态噪音

http - 如何修复unicode字母?

javascript - 在 Javascript 中保护 JSON?

循环创建、连接信号到按钮

performance - 解码面和输出面是什么意思?它们如何影响解码性能?