嗨,我是“ C”的新手,我对我得到的某些输出有疑问。
我们正在处理的数字为:[ 4003600000000014 ]。除了一个我不确定如何获取最后一个要打印的数字,我将每个元素附加到此数组。
我对数字6422180感到好奇,不确定是否可以解释。
// This is an example of Luhn's algorithm.
#include <stdio.h>
#include <ctype.h>
#define MAX_CRED_LEN 16
// Function prototypes.
void payment_method();
int main(void)
{
payment_method();
return 0;
}
long long int get_credit_card_num()
/*
Gets the users credit card number.
*/
{
long long int d;
do
{
printf("\nPlease enter in your credit card number: ");
if (scanf("%lld", &d) == 1)
{
printf("\nYour credit card number: (%lld)\n%s", d,
"------------------------\n\n");
continue;
}
else
{
printf("\n\tINVALID INPUT CHARACTER ENTERED PLEASE TRY AGAIN!\n%s",
"\t*************************************************");
fflush(stdin); // Clear input buffer of any characters!
get_credit_card_num(); // Start the function again to prompt user.
}
} while (d < 0);
return d;
}
int num_of_ccdigits()
/*
Counts the amount of digits the user entered. Then compares the number of
digits to a specific length offered by cardholders they are 13, 15, and 16
digits long.
*/
{
long long int cc_num = get_credit_card_num();
int digits = 0, arr[MAX_CRED_LEN];
// Count how many digits the user entered then compare the length.
while (cc_num != 0)
{
cc_num /= 10;
arr[digits] = cc_num % 10;
digits++;
}
// Display the number of digits that the user entered.
printf("\nNumber of digits: [%i]", digits);
int a = 0, i;
for (i = 0; i < digits; i++)
{
printf("\n[%d]:\tYOUR DIGIT ARRAY: %i", a, arr[i - 1]);
++a;
}
printf("\n\nARRAY WITHOUT LINE BREAKS: %i", arr);
// Check to see if this is a valid credit card number or a supported one.
if ((digits != 13) && (digits != 15) && (digits != 16))
{
printf("\n\tMUST BE AT LEAST 13, 15, AS HIGH AS 16 DIGITS.\n%s",
"\t**********************************************\n");
// if no 13, 15, or 16 digits long restart loop.
num_of_ccdigits();
}
return digits; // return the amount of digits in the credit card number.
}
void payment_method()
{
int num_of_digits = num_of_ccdigits();
}
我在这里看到一些问题。对于第一个问题,代码在的范围之外建立了索引arr
,从而导致未定义的行为:
for (i = 0; i < digits; i++)
{
printf("\n[%d]:\tYOUR DIGIT ARRAY: %i", a, arr[i - 1]);
++a;
}
在第一次迭代中,i = 0
我们继续索引arr[0 - 1]
或arr[-1]
。哎呀 这可能会产生垃圾值或导致崩溃(或其他情况)。
顺便说一句,a
这里是多余的-仅用于i
获取索引(必须诉诸a
于获取索引的气味,好像有些东西关闭了)。该arr[i - 1]
技巧可能已经完成,以弥补另一个bug:
while (cc_num != 0)
{
cc_num /= 10;
arr[digits] = cc_num % 10;
digits++;
}
这里的问题是cc_num /= 10;
说“移至下一位”。但这会完全跳过第一个数字。应该是:
while (cc_num != 0)
{
arr[digits] = cc_num % 10;
digits++;
cc_num /= 10;
}
可以通过打开警告来识别的另一个问题是
ARRAY WITHOUT LINE BREAKS: -1747166688
在这里,编译器告诉我们问题所在:
$ clang-7 -o main main.c
main.c:67:49: warning: format specifies type 'int' but the
argument has type 'int *' [-Wformat]
printf("\n\nARRAY WITHOUT LINE BREAKS: %i", arr);
~~ ^~~
1 warning generated.
要转储数组的值,请使用循环并分别打印每个元素。
其他说明:
num_of_ccdigits
,除了简单地获取信用卡位数外,还可以做很多事情。它还执行验证和I / O。如果删除打印语句并使它仅负责计算数字中的数字,则可以对其count_digits(long long int num)
进行重命名并使其可在任何位置重用long long int
。然后,在其他地方(例如print_digits(long long int num)
和)进行打印和验证validate_cc_digit_length(long long int num)
。找出单一责任原则。我对功能的目的有点困惑
void payment_method()
{
int num_of_digits = num_of_ccdigits();
}
函数名称通常是描述函数功能的动作,而不是名词(通常是变量)。考虑类似的东西get_payment_method()
。
同样,由于num_of_ccdigits
对坏数据进行递归调用,因此有可能导致堆栈溢出。使用while
循环是处理可能会无限运行的错误输入的更好方法。
对于像这样的初始化:
int digits = 0, arr[MAX_CRED_LEN];
喜欢:
int digits = 0;
int arr[MAX_CRED_LEN];
这更容易阅读。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句