简体   繁体   English

c中的luhns算法

[英]luhns algorithm in c

I've looked around the internet and I've seen a lot of other luhns algorithms for the same class I'm taking but it's hard to gauge what the difference in their code is vs mine.我环顾了互联网,看到了很多其他 luhns 算法,用于我正在上的同一门课,但很难判断他们的代码与我的代码有何不同。 right now I have this:现在我有这个:

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

int main(void)
{
    long input;
    do
    {
        input = get_long("Number: ");
    }
    while (input < 0);

    long secondToLast, last;
    int i;
    long length = 0;
    long sum1 = 0;
    long sum2 = 0;
    long firstNum, secondNum, totalSum;

    //Finds the length of the input
    while (input > 0)
    {
        input/=10;
        length++;
    }
    printf("%li\n", length);
    //For loop goes through every 2 digits in
    //input and takes out the ones we want.
    for (i = 0; i <= length; i += 2)
    {
        secondToLast = input / (long)pow(10, i+1);
        secondToLast = secondToLast % 10;
        last = input / (long)pow(10, i);
        last = last % 10;
        printf("Second to last: %li\n", secondToLast);
        printf("Last: %li\n", last);

        //This is to separate the digits into individual digits.
        if (secondToLast * 2 > 10)
        {
            firstNum = secondToLast * 2 / 10;
            secondNum = secondToLast * 2 % 10;
            sum1 = sum1 + firstNum + secondNum - (secondToLast * 2);
        }
        //Add the digits we get
        sum1 = sum1 + secondToLast *  2;
        sum2 = sum2 + last;
        printf("Sum1: %li\n", sum1);
        printf("Sum2: %li\n", sum2);
    }

    totalSum = sum1 + sum2;
    printf("Total Sum: %li\n", totalSum);
    //Final step of checksum to see if the last digit is 0 or not.
    if (totalSum % 10 == 0)
    {
        printf("Valid \n");
    }
        else
    {
        printf("Invalid \n";)
    }

my code seems to be incorrect as when i print out the sums to check what the code is doing, they are all 0 and never get changed.我的代码似乎不正确,因为当我打印出总和以检查代码在做什么时,它们都是 0 并且永远不会改变。 it worked before, but i don't recall ever changing the code to the point where it doesn't work.它以前工作过,但我不记得曾经将代码更改到不起作用的程度。 if anyone could please take a look and show me where i could be messing up would be appreciated.如果有人可以请看一看并向我展示我可能会搞砸的地方,我们将不胜感激。

In the first part of your algorithm, where you are trying to get the length of your number:在算法的第一部分,您尝试获取数字的长度:

    //Finds the length of the input (and destroy input forever)
    while (input > 0)
    {
        input/=10;
        length++;
    }
    /* here you can ensure that input is always zero,
     * because of the two clauses stated below, continue
     * reading... */

You get out of the loop only when your input variable reaches 0, so after that all your algorithm deals with a 0 in input , and no way of knowing the digits anymore.只有当您的input变量达到 0 时,您才能退出循环,因此之后您的所有算法都会处理input0 ,并且无法再知道数字。 You have destroyed your input.你已经破坏了你的输入。

Before that, you did在此之前,你做了

while (input < 0);

so in case your input was negative, your program would block in a loop (with an empty body) in which nothing is done to input so you will never get out of there.因此,如果您的输入为负,您的程序将阻塞在一个循环中(带有一个空的主体),在该循环中不对input进行任何操作,因此您将永远无法离开那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM