【问题标题】:C programming Luhn algorithmC 编程 Luhn 算法
【发布时间】:2021-03-29 10:07:26
【问题描述】:

我试图用 C 语言编写一个程序来检查信用卡的 Luhn 算法,但它不起作用。我想我不太清楚getchar() 的工作原理,但这个程序对我来说看起来很明智。你能告诉我它有什么问题吗?提前感谢您对此提供的任何帮助。

#include <stdio.h>

int main(void) {
    char x;
    int n, sum, i, c;
    sum = 0;
    printf("Insert the number of digits: ");
    scanf("%d",&n);
    printf("Insert the digits: ");
    for(i = n; i > 1; i = i - 1){
        x = getchar();
        if(i%2==0) 
            if(2*x < 10) sum = sum + 2*x;
            else sum = sum + 2*x - 9;
        else sum = sum + x;
        i = i - 1;
    }
    c = (9*sum)%10;
    x = getchar();
    getchar();
    if(x == c) printf("Last digit: %d,\nCheck digit: %d,\nMatching",x,c);
    else printf("Last digit: %d,\nCheck digit: %d,\nNot Matching",x,c);
}

【问题讨论】:

    标签: c getchar luhn


    【解决方案1】:

    getchar() 读取一个字符。所以循环中的x = getchar();不好是因为

    • 如果您在第一个“位数”之后输入换行符,它将首先读取一个换行符。
    • 它将读取一个字符,而不是一个整数。字符代码通常与字符所代表的整数不同,并且可能会影响校验位的计算。

    您应该在循环中执行此操作,而不是 x = getchar();

    scanf(" %c", &x); /* ignore whitespace characters (including newline character) and read one character */
    x -= '0'; /* convert the character to corresponding integer */
    

    【讨论】:

    • 感谢您的解决方案。我通常避免使用getchar() 并使用scanf,但是,在这种情况下,我放弃了它,因为我必须一次插入一位数字,然后按enter,而我想插入整数同时。有什么办法吗?
    • @Logos 这个程序不接受这样的输入吗?
    • 不,我写的是长号,程序在等我插入其他字符
    • @Logos 是不是因为你在循环之后写了getchar(); 等待另一个字符?
    • 不,我一介绍scanf 就删除了它。另外,如果我一次插入一个数字,然后是enter 它可以工作
    【解决方案2】:
    #include <stdio.h>
    #define N 16
    
    void luhn_algorithm();
    int main(){
        int a[N];
        int i;
        printf("type the card number:\n");
        for(i=1;i<=N;i++){
            scanf("%d",&a[i]);
        }
        luhn_algorithm(a);
    }
    void luhn_algorithm(int *a){
        int i,multiply=1,m,sum=0,total=0;
        for(i=1;i<=N;i++){
            if(i%2!=0){
                multiply=a[i]*2;
                if(multiply>9){
                    while(multiply>0){
                        m=multiply%10;
                        sum+=multiply;
                        multiply/=10;
                    }
                    multiply=sum;
                }
            }
            else if(i%2==0){
                multiply=a[i]*1;
                if(multiply>9){
                    while(multiply>0){
                        m=multiply%10;
                        sum+=multiply;
                        multiply/=10;
                    }
                    multiply=sum;
                }
            }
        total+=multiply;
        }
        if(total%10==0){
            printf("\nthis credit card is valid ");
        }
        else{
            printf("\nthis credit card is not valid");
        }
    }
    

    这是我用来检查信用卡号码是否有效的程序。 我把数字放在一个数组中,然后根据它们的位置相乘,如果相加总数的最后一位数字为 0,则表示卡片有效,否则将它们全部相加。 检查一下,如果有什么问题请告诉我。

    【讨论】:

      猜你喜欢
      • 2014-03-04
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 2023-04-05
      • 1970-01-01
      • 2013-08-10
      • 2015-01-01
      相关资源
      最近更新 更多