gpt4 book ai didi

c - 在 Luhn's Alg 上制作了一个脚本。无法清楚地理解如何为修改后的脚本创建和调用函数

转载 作者:行者123 更新时间:2023-12-02 11:02:00 39 4
gpt4 key购买 nike

我是编程新手。我目前正在非常严格地接受在线讲座,并使用卢恩算法完成了一项任务。这只是一个直接运行的脚本,但对于 future 的我来说,我想随着项目变得越来越大,学习更有效地编码。

这就是我的问题所在。我似乎无法理解如何正确定义或调用函数,也无法将我的脚本修改为更“高效”的东西。

(根据机器人的说法,一切都已经提交,我的脚本完美地完成了任务,所以我不想在这里半途而废我的工作只是为了清楚起见。)

这是完整的脚本,仅包含直接运行的 main 函数,我花了大约 12-15 个小时才让它从一开始就没有错误地运行。一切都是用 C 编写的

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
// this grabs the number and verifies the correct amount of digits
int count;
long number = 0;
do
{
number = get_long("Number: ");
count = (number == 0) ? 1 : (log10(number) + 1);
if (count < 13 || count == 14 || count > 16)
{
printf("INVALID\n"); // This is to satisfy the uni. bot command check. Need a EOF for numbers with the wrong amount of digits.
return (0);
}
}
while (count < 13 || count == 14 || count > 16);
//printf("Digits: %i\n", count); // test print for debugging

//This finds the first two digits of input number

long int two = number;
while (two >= 100)
{
two = two / 10;
}
//printf("First two numbers: %li\n", two); // test print for debugging

// verifies card using mod10 (Luhn's)
long sum = 0;
long bigdigit = 0;
//printf("\nLUHN Number: %li\n\n", number); // test print for debugging
if (count == 13 || count == 15)
{
count += 1;
}
for (int i = count; i > 0; i--)
{
if (i % 2 == 0)
{
sum += (number % 10);
}
else
{
bigdigit = (2 * (number % 10));
sum += (bigdigit / 10 + bigdigit % 10);
}
number = (number / 10);
//printf("\nI : %i\n", i); // test print for debugging
//printf("Sum: %li\n", sum); // test print for debugging
//printf("Number: %li\n", number); // test print for debugging
}
if (sum % 10 == 0)
{
printf("VALID\n");
}
else
{
printf("INVALID\n");
return (0);
}

// checks what type of card

if (two == 34 || two == 37)
{
printf("AMEX\n");
return (0);
}
else if (two >= 51 && two <= 55)
{
printf("MASTERCARD\n");
return (0);
}
else if (two >= 40 && two <= 49)
{
printf("VISA\n");
return (0);
}
else
{
printf("INVALID\n");
return (0);
}
}

我试图将它分成 3 个 main 函数来调用。

  • long input_number();
  • bool luhn_check();
  • 无效company_check();

我被第二个函数困住了,不确定第三个函数是否应该为空。

<小时/>

“修订”脚本 v2

#include <stdio.h>
#include <cs50.h>
#include <math.h>

long input_number(long CCN);
int counter(long CCN, int count);
bool luhn_check(long CCN, int count);
long firsttwo(long CCN, long two);
void card_type(long two);


int main()
{
long CCN = 0;
int count = 0;
long two = 0;
CCN = input_number(CCN);
count = counter(CCN, count);

//printf("CCN: %li\n", CCN); //debugging purposes
//printf("Digits: %i\n", count); //debugging purposes
luhn_check(CCN, count);
two = firsttwo(CCN, two);
//printf("First Two: %li\n", two); //debugging purposes
card_type(two);
}


// this grabs the number and verifies the correct amount of digits
long input_number(long CCN)
{
int count = 0;
do
{
CCN = get_long("Number: ");
count = (CCN == 0) ? 1 : (log10(CCN) + 1);
if (count < 13 || count == 14 || count > 16)
{
//printf("INVALID\n"); // This is to satisfy the uni. bot command check. Need a EOF
//return (0);
}
}
while (count < 13 || count == 14 || count > 16);
return (CCN);
}

int counter(long CCN, int count)
{
do
{
count = (CCN == 0) ? 1 : (log10(CCN) + 1);
}
while (count < 13 || count == 14 || count > 16);
return (count);
}

// verifies card using mod10 (Luhn's)
bool luhn_check(long CCN, int count)
{
long sum = 0;
long bigdigit = 0;
//printf("\nLUHN Number: %ld\n\n", CCN); // test print for debugging
if (count == 13 || count == 15)
{
count += 1;
}
for (int i = count; i > 0; i--)
{
if (i % 2 == 0)
{
sum += (CCN % 10);
}
else
{
bigdigit = (2 * (CCN % 10));
sum += (bigdigit / 10 + bigdigit % 10);
}
CCN = (CCN / 10);
//printf("\nI : %i\n", i); // test print for debugging
//printf("Sum: %li\n", sum); // test print for debugging
//printf("Number: %li\n", CCN); // test print for debugging
}
if (sum % 10 == 0)
{
printf("VALID\n");
return (true);
}
else
{
printf("INVALID\n");
return (false);
}
}

// grabs the first two numbers
long firsttwo(long CCN, long two)
{
two = CCN;
//printf("TWO CCN: %li\n", two); // debugging purposes
while (two >= 100)
{
two = two / 10;
}
return (two);
}

// finds card type and ends
void card_type(long two)
{
if (two == 34 || two == 37)
{
printf("AMEX\n");
//return (0);
}
else if (two >= 51 && two <= 55)
{
printf("MASTERCARD\n");
//return (0);
}
else if (two >= 40 && two <= 49)
{
printf("VISA\n");
//return (0);
}
else
{
printf("INVALID\n");
//return (0);
}
}

我已经根据您的建议完成了脚本的第二个版本,除了输入字符串,我将尝试在下一个版本中解决该方法,因为我还没有处理该类型。

除了使用字符串而不是b运行程序之外,还有什么我可以更有效地完成的吗?

最佳答案

让我先对您的函数 input_number 做一些说明:

long input_number()
{
// this grabs the number and verifies the correct amount of digits
int count;
[ .... ]

while (count < 13 || count == 14 || count > 16);
// Once this while loop starts, it will never terminate!
// if count starts at 10 (which is less than 13), the body says "do nothing".
// so count will continue to be unchanged, and the while-loop will continue to run.

return (0); // Code after a return statement will never be reached
// So your printf-statement below will NEVER happen.
// It also seems unlikely you want to return Zero.
// You probably want to return count, or some other value.

printf("Digits: %i\n", count); // test print for debugging
}

关于c - 在 Luhn's Alg 上制作了一个脚本。无法清楚地理解如何为修改后的脚本创建和调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61145595/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com