编码/解码程序(C)出现逻辑错误

问题描述 投票:2回答:2

我是C编程课程的初学者,我们为课本第三章分配的任务是创建一个编码/解码程序,该程序从用户输入中提取四位数字,并根据用户是否需要重新排列它们被编码或解码。我也无法使程序同时进行在一个文件中]的解码和编码。但是,当我启动程序时,每当我选择编码并输入四位数时,该程序不仅会给我一个很大的数字,而且编码结果应该只能是四位数。它还给了我“按任意键继续”的提示,表示程序结束,而不是完全重新开始并返回到要求用户解码或编码的位置。

也附有输出。


我试图四处查看并将一些变量从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 . . .```


我是C编程课程的初学者,我们为课本第三章分配的任务是创建一个编码/解码程序,该程序从用户输入中提取四位数并对其进行重新排列...]]

c decode encode
2个回答
0
投票

您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');


0
投票

最简单的方法可能是直接在由四个数字组成的字符串上工作:

© www.soinside.com 2019 - 2024. All rights reserved.