如何在C中添加产品数字而不是产品本身? [英] How to add product digits rather than products themselves in C?

查看:77
本文介绍了如何在C中添加产品数字而不是产品本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用C完成CS50课程的作业,在该课程中,我必须实现卢恩算法来验证信用卡号。下面是一个简单的示例:

I am trying to finish an assignment in C for the CS50 course in which I must implement Luhn's algorithm to validate a credit card number. Here is a quick example to elaborate:

信用卡号:4003600000000014.

现在每其他数字,从数字的倒数第二位开始:

credit card number: 4003600000000014.
Now for every other digit, starting with the number’s second-to-last digit:

1-0-0-0-0-6-6-4

1-0-0-0-0-6-0-4

让每个数字乘以2:

1•2 + 0•2 + 0•2 + 0•2 + 0 •2 + 6•2 + 0•2 + 4•2

1•2 + 0•2 + 0•2 + 0•2 + 0•2 + 6•2 + 0•2 + 4•2

这使我们:

2 + 0 + 0 + 0 + 0 + 12 + 0 + 8

2 + 0 + 0 + 0 + 0 + 12 + 0 + 8

现在让我们将这些产品的数字(即不是产品本身)加在一起:

Now let’s add those products’ digits (i.e., not the products themselves) together:

2 + 0 + 0 + 0 + 0 + 1 + 2 + 0 + 8 = 13

2 + 0 + 0 + 0 + 0 + 1 + 2 + 0 + 8 = 13

现在让我们加总和( 13)到未乘以2的数字总和(从末尾开始):

Now let’s add that sum (13) to the sum of the digits that weren’t multiplied by 2 (starting from the end):

13 + 4 + 0 + 0 + 0 + 0 + 0 + 3 + 0 = 20

13 + 4 + 0 + 0 + 0 + 0 + 0 + 3 + 0 = 20

是的,该总和(20)的最后一位为0,因此该数字有效。

Yup, the last digit in that sum (20) is a 0, so the number is valid.

我想出了如何分别提取信用卡中的每个数字(我知道我的方式很无聊,可能不实际),因此下一步是将其他数字乘以2并相加(产品的数字,而不是数字本身),这是我需要如何做的帮助?
我的代码:

I figured out how to extract each number in the credit card individually (I know my way is boring and probably not practical), so the next step is to multiply every other number by two and add (the products' digits, not the digits themselves) and this is what I need help of how to do it? MY code:

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

int main(void)
{
    long credit_card_number;
    do
    {
        credit_card_number = get_long("Enter your credit card number: ");

    }
    while (credit_card_number < 1 || credit_card_number > 9999999999999999);

    //American Express uses 15-digit numbers. American Express numbers start with 34 or 37
    //MasterCard uses 16-digit numbers. MasterCard numbers start with 51, 52, 53, 54, or 55.
    //Visa uses 13- and 16-digit numbers. Visa numbers start with 4.
    // checksum

    long last_number;
    long credit_card_without_last_number;
    long second_to_last_number;
    long credit_card_without_second_number;
    long third_number;
    long credit_card_without_third_number;
    long fourth_number;
    long credit_card_without_fourth_number;
    long fifth_number;
    long credit_card_without_fifth_number;
    long sixth_number;
    long credit_card_without_sixth_number;
    long seventh_number;
    long credit_card_without_seventh_number;
    long eighth_number;
    long credit_card_without_eighth_number;
    long ninth_number;
    long credit_card_without_ninth_number;
    long tenth_number;
    long credit_card_without_tenth_number;
    long eleventh_number;
    long credit_card_without_eleventh_number;
    long twelfth_number;
    long credit_card_without_twelfth_number;
    long thirteenth_number;
    long credit_card_without_thirteenth_number;
    long fourteenth_number;
    long credit_card_without_fourteenth_number;
    long fifteenth_number;
    long credit_card_without_fifteenth_number;
    long sixteenth_number;

    long multiply_digits;

    //separating each number starting from the last (right)in its own variable.
    last_number = credit_card_number % 10;
    credit_card_without_last_number = credit_card_number / 10;

    second_to_last_number = credit_card_without_last_number % 10;
    credit_card_without_second_number = credit_card_without_last_number / 10;

    third_number = credit_card_without_second_number % 10;
    credit_card_without_third_number = credit_card_without_second_number / 10;

    fourth_number = credit_card_without_third_number % 10;
    credit_card_without_fourth_number = credit_card_without_third_number / 10;

    fifth_number = credit_card_without_fourth_number % 10;
    credit_card_without_fifth_number = credit_card_without_fourth_number / 10;

    sixth_number = credit_card_without_fifth_number % 10;
    credit_card_without_sixth_number = credit_card_without_fifth_number / 10;

    seventh_number = credit_card_without_sixth_number % 10;
    credit_card_without_seventh_number = credit_card_without_sixth_number / 10;

    eighth_number = credit_card_without_seventh_number % 10;
    credit_card_without_eighth_number = credit_card_without_seventh_number / 10;

    ninth_number = credit_card_without_eighth_number % 10;
    credit_card_without_ninth_number = credit_card_without_eighth_number / 10;

    tenth_number = credit_card_without_ninth_number % 10;
    credit_card_without_tenth_number = credit_card_without_ninth_number / 10;

    eleventh_number = credit_card_without_tenth_number % 10;
    credit_card_without_eleventh_number = credit_card_without_tenth_number / 10;

    twelfth_number = credit_card_without_eleventh_number % 10;
    credit_card_without_twelfth_number = credit_card_without_eleventh_number / 10;

    thirteenth_number = credit_card_without_twelfth_number % 10;
    credit_card_without_thirteenth_number = credit_card_without_twelfth_number / 10;

    fourteenth_number = credit_card_without_thirteenth_number % 10;
    credit_card_without_fourteenth_number = credit_card_without_thirteenth_number / 10;

    fifteenth_number = credit_card_without_fourteenth_number % 10;
    credit_card_without_fifteenth_number = credit_card_without_fourteenth_number / 10;

    sixteenth_number = credit_card_without_fifteenth_number % 10;

    //Here I need the help to multiply these numbers by two and then add each product's
    //digits to the rest of the unused numbers.
    multiply_digits = (second_to_last_number*2)+(fourth_number*2)+(sixth_number*2)+(eighth_number*2)+(tenth_number*2)+(twelfth_number*2)+(fourteenth_number*2)+(sixteenth_number*2);

}


推荐答案

一般用于添加数字(假定为整数类型)的算法:

A general algorithm for adding digits (assuming an integer type):


  1. 将您的总和初始化为0: sum = 0

  2. 使用模运算符从数字中提取最低位数: digit =数字%10

  3. 将该数字的值添加到总和中: sum + =数字(简写对于 sum = sum +数字

  4. 将数字除以10:数字/ = 10 数字的缩写=数字/ 10

  5. 如果数字除以10后为非零,请返回到2

  6. End

  1. Initialize your sum to 0: sum = 0
  2. Extract the lowest digit from the number using the % modulus operator: digit = number % 10
  3. Add the value of that digit to the sum: sum += digit (shorthand for sum = sum + digit)
  4. Divide the number by 10: number /= 10 (shorthand for number = number / 10
  5. If the number is non-zero after dividing by 10, go back to 2
  6. End

模运算符返回整数除法的整数余数- 123/10 == 12 rem 3 。因此,将数字除以10的余数是最低有效十进制数字注意整数除法为您提供整数结果- 123/10 == 12 ,而不是 12.3

The modulus operator % returns the integer remainder of an integer division - 123 / 10 == 12 rem 3. So the remainder of dividing the number by 10 is the least significant decimal digit of the number. Notice that integer division gives you an integer result - 123 / 10 == 12, not 12.3.

您需要将其放在单独的函数中,因此可以编写类似

You'll want to put this in a separate function, so you can write something like

int sumdig( int v )
{
  ...
}

int main( void )
{
   int value = 123;
   int sum = sumdig( value ); // sumdig will return 1 + 2 + 3, or 6
   ...
}

当您发现自己创建了一堆具有相同名称的相同类型的独立变量时,除了一些固定的序数( var1 var2 var3 第一件事 second_thing third_thing ),这确实是您要使用 array 的强烈提示。您可以使用数组存储卡号的单个数字:

When you find yourself creating a bunch of separate variables of the same type with the same name except for some tacked-on ordinal (var1, var2, var3 or first_thing, second_thing, third_thing), that's a real strong hint you want to use an array. You can use an array to store the individual digits of your card number:

int number[16];

并使用%10 方法上面提取单个数字:

and use the % 10 method as described above to extract the individual digits:

long tmp = credit_card_number; // use a temporary so we preserve the original card number
for ( int i = 0; i < 16; i++ )
{
  number[i] = tmp % 10;
  tmp /= 10;
}

这意味着最低有效(最右边)卡号数字将存储在 number [0] 中,而位有效(最左侧)卡号将存储在 number [15] ,因此请注意。出于验证数字的目的,这无关紧要,但是,如果要显示数组的内容,则必须考虑到这一点。

This means that the least significant (rightmost) card number digit will be stored in number[0] and the most significant (leftmost) card number digit will be stored in number[15], so be aware of that. For the purposes of validating the number it doesn't matter, but if you want to display the contents of the array you'll have to take that into account.

使用数组可以更轻松地提取数字子集:

Using an array makes it easier to extract subsets of digits:

for ( int i = 1; i < 16; i += 2 ) // hit every other element starting at element 1
{
  number[i] *= 2; // multiply these digits by 2
}

上面的循环执行 1•2 + 0•2 + 0•2 + 0•2 + 0•2 + 6•2 + 0•2 + 4•2部分。

That loop above executes the "1•2 + 0•2 + 0•2 + 0•2 + 0•2 + 6•2 + 0•2 + 4•2" portion of your algorithm.

您应该能够从那里找出其余的内容。希望这可以帮助。

You should be able to figure out the rest from there. Hope this helps.

这篇关于如何在C中添加产品数字而不是产品本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆