[英]Logic error with an encode/decode program (C)
我是 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 . . .```
你扫描错了。 我宁愿这样做
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);
使用&
你试图打印它们的内存地址而不是它们的值,而且你的格式字符串也是错误的。
最简单的方法可能是直接处理由四位数字组成的字符串:
char d[5], e[5] = { 0 };
scanf("%4s", d)
这是用于编码:
for (int i = 0; i < 4; i++) e[(i+2)%4]=(d[i]-41)%10+'0';
对于解码,您必须将魔法41
更改为45
。 结果在e
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.