这是我为验证密码而编写的代码,但我无法让系统验证新密码。在此程序中,输入来自用户并使用预定义的引脚进行验证。如果用户未能验证代码,则会显示一个新代码作为新密码。
#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
#include<windows.h>
int pin();
int main()
{
int pin();
{
int pin,new_pin,old_pin;
{
old_pin=1111;
printf("Enter your pin: ");
scanf("%d", &pin);
if(pin==old_pin)
{
printf("PIN succefully verified \n");
}
else if(pin!=old_pin)
{
printf("PIN couldn't be verified. \n");
printf("Press 1 to generate a new pin, 2 to re-enter or 0 to exit\n");
scanf("%d", &new_pin);
if (new_pin==1)
{
printf("Your new pin is: \n");
//generating random 4numbered pin
int i,random;
for(i=0; i<3; i++)
random= (rand() );
printf("%d\n", random);
main();
random=new_pin;
new_pin=old_pin;
}
else if (new_pin==2) {
main();
}
else{
printf("Exiting the program....\n");
exit;
}
}
}
}
return(0);
}
最佳答案
哪里出了问题?
首先,需要重新访问代码。您引用了名为 pin()
的函数,我们不知道该源代码。接下来,我们遇到了名称的微小冲突(int pin;
、int pin();
)。代码块太多了。变量命名不当(new_pin
不是新的 pin,old_pin
不是旧的)。用于生成 pin 的循环是无用的,因为您在不存储旧随机数的情况下覆盖变量。 printf
(和其他)中没有 \
的多行字符串是被禁止的(我在您的原始代码附近发现了它们)。 exit
缺少括号。这与语法、代码优化和命名质量有关。
代码递归调用 main,这当然不是一个好主意(除非你知道,你在做什么!)
如何修复代码。
我已经对其进行格式化并删除了 int pin();
声明,因为它们是多余的。代码块的复杂性降低了,不必要的库导入也被去除了。 for 循环被简化了(因为它是多余的)。代码现在看起来像这样:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int pin, new_pin, old_pin = 1111;
printf("Enter your pin: ");
scanf("%d", &pin);
if (pin == old_pin)
printf("PIN succefully verified.\n");
else if (pin != old_pin) {
printf("PIN couldn't be verified.\n");
printf("Press 1 to generate a new pin, 2 to re-enter or 0 to exit\n");
new_pin = getchar();
if (new_pin == '1') {
int i, random;
printf("Your new pin is: %4d\n" rand() % 9999);
main();
} else if (new_pin=='2')
main();
else {
printf("Exiting the program....\n");
return 0;
}
}
}
现在,让我们来实现功能是错误的。新的销钉机制坏得很厉害,没有解释的意义。最好买一个全新的。值已静态化,因此在创建新的局部变量集后它们不会消失。
#include <stdio.h>
#include <stdlib.h>
int main(void) {
static int pin, new_pin, validPin = 1111;
printf("Enter your pin: ");
scanf("%d", &pin);
if (pin == validPin)
printf("PIN succefully verified.\n");
else if (pin != validPin) {
printf("PIN couldn't be verified.\n");
printf("Press 1 to generate a new pin, 2 to fall back or any other key to exit.\n");
new_pin = getchar();
if (new_pin == '1') {
validPin = rand() % 9999;
printf("Your new pin is: %4d.\n", validPin);
main();
} else if (new_pin=='2')
main();
else {
printf("Exiting the program.\n");
return 0;
}
}
}
现在,让我们重新审视主要的递归反模式并将代码划分为子过程。
#include <stdio.h>
#include <stdlib.h>
int proc() {
/* ... the code in partially unmodified form goes here. */
}
int main(void) {
while(!proc());
}
最后,让我们弄乱变量范围以将其减少到绝对最小值并将退出消息放在 main 的末尾。最后的代码如下所示:
#include <stdio.h>
#include <stdlib.h>
int proc() {
int pin;
static int validPin = 1111;
printf("Enter your pin: ");
scanf("%d", &pin);
if (pin == validPin)
printf("PIN succefully verified.\n");
else if (pin != validPin) {
int command;
printf("PIN couldn't be verified.\n");
printf("Press 1 to generate a new pin, 2 to fall back or any other key to exit.\n");
command = getchar();
if (command == '1') {
validPin = rand() % 9999;
printf("Your new pin is: %4d.\n", validPin);
return 0;
} else if (command == '2')
return 0;
else
return 1;
}
return 1;
}
int main(void) {
while(!proc());
printf("Exiting the program.\n");
}
由于某些在线 C 编译器可能对 getchar()
做出错误的 react ,因此只需使用 scanf()
,就像您实际所做的那样:
int command;
printf("PIN couldn't be verified.\n");
printf("Press 1 to generate a new pin, 2 to fall back or 0 to exit:\n");
scanf("%d", &command);
if (command == 1) {
validPin = rand() % 9999;
printf("Your new pin is: %4d.\n", validPin);
return 0;
} else if (command == 0)
return 1;
else
return 0;
附录
我的最终代码本可以通过将 god-procedure proc()
分解为 pin 验证器和命令处理程序来改进,但该应用程序足够简单以保持简洁。在您的学习曲线中,此应用程序示例可能是一段很好的代码来训练重构技能。
希望我的评论对您有所帮助。
关于c - 该程序似乎没有更新值并验证新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263322/