如何在此函数中除以零的算术误差?

时间:2014-09-13 01:49:20

标签: c gcc gdb cs50 msvc12

我编写了 Luhn算法的实现。当我使用无效的数字来测试程序时,我得到没有程序错误。相反,程序在main函数中执行printf()调用。但是,每当我按照算法使用有效的卡号测试程序时,我得到除以0的错误。用DBG调试给我一个算术错误,而MSVC给我一个浮点异常,它缩小到除以0.这是代码:

#include <stdio.h>
#include <math.h>
#include "cs50.h" // For MSVC, I used scanf_s() from stdio.h instead since cs50.h is not supported

long long Prompt(void); // Returns credit card number
int Validate(long long); // Returns checksum
void Report(long long); // Prints success

int main()
{
    long long CardNumber = Prompt();
    int CheckSum = Validate(CardNumber);
    if (!(CheckSum % 10)) {
        Report(CardNumber);
    } else {
        printf("INVALID\n");
    }
    return 0;
}

long long Prompt(void)
{
    printf("CARD NUMBER: ");
    long long CardNumber;
    CardNumber = GetLongLong();
    return CardNumber;
}

int Validate(long long CardNumber)
{
    int CheckSum = 0;
    int Digit = 0;
    int DigitCount = (int)(floor(log10l(CardNumber) + 1));
    for (int i = 2; i < DigitCount; i += 2) {
        Digit = 2 * ((CardNumber % (10 ^ i)) / (10 ^ (i - 1))); // BUG
        if (Digit > 9) {
            int BiDigit = Digit;
            Digit = 0;
            int SubDigit = 0;
            for (int j = 0; j < 2; i++) {
                SubDigit = (BiDigit % (10 ^ j)) / (10 ^ (j - 1)); // BUG
                Digit += SubDigit;
            }
        }
        CheckSum += Digit;
    }
    for (int i = 1; i < DigitCount; i += 2) {
        Digit = (CardNumber % (10 ^ i)) / (10 ^ (i - 1));
        CheckSum += Digit;
    }
    return CheckSum;
}

void Report(long long CardNumber)
{
    int DigitCount = (int)(floor(log10l(CardNumber) + 1));
    int Digit1 = (CardNumber % (10 ^ (DigitCount - 1))) / (10 ^ (DigitCount - 2)); // BUG
    int Digit2 = (CardNumber % (10 ^ (DigitCount - 2))) / (10 ^ (DigitCount - 3)); // BUG
    if (Digit1 == 4 && (DigitCount == 13 || DigitCount == 16))
        printf("VISA\n");
    else if (Digit1 == 3 && (Digit2 == 4 || Digit2 == 7) && DigitCount == 15)
        printf("AMERICAN EXPRESS\n");
    else if (Digit1 == 4 && (Digit2 >= 1 || Digit2 <= 5) && DigitCount == 16)
        printf("MASTERCARD\n");
    else
        printf("INVALID\n");
    return;
}

1 个答案:

答案 0 :(得分:4)

Digit = 2 * ((CardNumber % (10 ^ i)) / (10 ^ (i - 1)));

^不是幂运算符,而是CC++中的按位xor。 i到达10后,表达式10 ^ i将变为零。


BTW,为了从整数中获取数字,最好避免浮点运算(floorlog10l)。可能的算法可能如下所示:

int DigitIndex = 0;
while (CardNumber > 0)
{
    int Digit = CardNumber % 10;
    CardNumber /= 10;

    // calculate check sum depending on parity of DigitIndex

    DigitIndex++;       
}