You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法让if语句正确评估两函数返回值之和变量及条件判断

信用卡号码验证代码的条件判断问题

编译器无法正确评估totalSum % 10 == 0这个条件判断,以下是我的C语言代码:

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

int countingMachine(long n);
int oddAdd(long cNum2)
{
    int n = 0;
    long tempCred = cNum2;
    int add = 0;
    long double tempData = 0;
    while (tempCred != 0)
    {
        if (n % 2 != 0)
        {
            tempData = (tempCred % 10);
            if (tempData <= 0)
            {
                tempData = 0;
                add += (int) tempData;
            }
            add += tempData;
        }
        tempCred /= 10;
        n++;
    }
    return add;
}
int multAdd(long cNum)
{
    int n = 0;
    long tempCred = cNum;
    int evenAdd = 0;
    int tempData = 0;
    while (tempCred != 0)
    {
        tempCred /= 10;
        if(n % 2 == 0)
        {
            tempData = (tempCred % 10)*2;
            if (tempData >= 10)
            {
                evenAdd += tempData % 10;
                evenAdd += tempData / 10;
            }
            else
            {
                evenAdd += tempData;
            }
        }
        n++;
    }
    return evenAdd;
}

long divNum(int count)
{
    long long int divisor;
    int i;
    for(divisor = 10, i = 0; i <= count - 1; i++)
    {
        divisor = divisor * 10;
    }
    return divisor;
}

int mathCheck(long cardNum, long neoDiv)
{
    int primeTwo = cardNum / neoDiv;
    return primeTwo;
}


int main(void)
{
    int am1 = 34;
    int am2 = 37;
    int mc1 = 51;
    int mc2 = 52;
    int mc3 = 53;
    int mc4 = 54;
    int mc5 = 55;
    int vZA = 4;
    long n = 0;
    int tempCount = 0;
    int totalSum;
    long ccNum = 0;
    while (ccNum <= 0)
    {
        ccNum = get_long("Enter Credit Card Number\n");
    }
    tempCount = ccNum;
    totalSum = oddAdd(ccNum); + multAdd(ccNum) % 10;
    tempCount = countingMachine(tempCount);
    printf("%i\n", tempCount);
    long long int divi = divNum(tempCount);
    printf("%lld\n", divi);
    long firstTwo = ccNum / divi;
    printf("%li\n", firstTwo);
    while (firstTwo >= 40 && firstTwo <= 50)
    {
        firstTwo /= 10;
    }

    if (firstTwo == am1 || firstTwo = am2 (&& totalSum % 10 == 0))
    {
        printf("Number: %li\n", ccNum);
        printf("BANK OF AMERICA")
    }
    if (firstTwo == mc1 || mc2 || mc3 || mc4 || mc5 (&& totalSum % 10 == 0))
    {
        printf("Number: %li\n", ccNum);
        printf("MASTERCARD");
    }

}

int countingMachine(long n)
{

    int count = 0;
    while(n != 0)
    {
        count++;
        n /= 10;
    }
    return count;

}

我尝试过将函数定义为void和int两种返回类型,但都无法解决问题。使用int返回类型时,编译器会针对语句if (firstTwo == am1 || am2 && totalSum == 0)给出警告:&& within '||' place parenthesis around the && statement to silence this warning;添加括号后又出现错误:called object type 'int' is not a function or a function pointer or invalid operand to binary expression ('void *' and 'int');使用void返回类型时,也会出现类似错误(包含void *相关提示)。

内容的提问来源于stack exchange,提问作者Agatha Fordyce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 18:35:21