簡體   English   中英

為什么以下C代碼不起作用? 我無法弄清楚錯誤。

[英]why is the following C code not working? I can't figure out the error.

我不知道問題出在哪里。 代碼編譯時沒有任何錯誤,但是在運行它時,它會問我一個數字,但是當我在輸入后按回車鍵時,它便崩潰了。 誰能告訴我問題出在哪里? 我認為存在一些內存問題,但我不知道在哪里。 提前致謝。

#include <stdio.h>
#include <stdlib.h>

int main(void)
{ 
    int n[16];
    long long a;
    printf("enter your credit card number \n");
    scanf(" %lld", &a);
    int i;
    for ( i = 0; i < 15; i++)
    {
        n[i] = (a % (10^(15-i))) / 10^(14-i);
    }
    int m = (n[1] + n[3] + n[5] + n[7] + n[9] + n[11] + n[13]);
int w = 2 * m;
int k = w % 10;
int l = n[0] + n[2] + n[4] + n[6] + n[8] + n[10] + n[12] + n[14];
int z = k + l;
if (z % 10 == 0)
{
    printf("you card is valid");
}
else
{
    printf("go get a new card");
}
return 0;
}

^表示按位XOR。 如果要利用某些功能,請使用pow()函數。

盡管Lundin提到了所需的代碼,但是您的運行時錯誤的實際問題是:

  • i = 5(10^(15-i))給出0 10^10還記得XOR規則嗎?)

  • 因此(a % (10^(15-i)))變為a%0 ,這是經典的除以零運行時誤差。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM