尝试将字符串转换为整数数组的粗略时间

时间:2016-11-16 21:36:08

标签: c arrays string integer cs50

基本上,程序会询问用户信用卡号,在字符串中捕获它,将字符串转换为整数数组(因此我可以验证信用卡号,以便以后的数字和乘法)。

我需要一个带整数数组的变量,下面的代码存储ASCII值。

尝试阅读很多帖子,但没有得到它。

我很感激任何帮助,所以我可以再次入睡=) 亲切,

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

int main (void)
{

string cc_string;

printf("Please enter a credit card number:\n");

//Captures credit card string
cc_string = GetString();

// Array of credit card digits integers 
int cc_digits[16];

    for (int i = 0; i<= 15; i++)
        {   
            cc_digits[i] = (int) cc_string[i];
            //Just checking what value has been stored
            printf("position %d with %d \n", i, cc_digits[i]);
        }

//to be continued

}

4 个答案:

答案 0 :(得分:1)

使用atoi()

而不是使用(int)转换为int

答案 1 :(得分:1)

而不是cc_digits[i] = (int) cc_string[i]尝试cc_digits[i] = cc_string[i] - '0'如果您只是转换为int,您将获得该字符的ASCII代码,但是如果您减去字符{{1}的代码那么你应该得到实际的数字。

答案 2 :(得分:0)

找到解决方案,想与大家分享。感谢大家的帮助,时间和学习。

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

int main (void)

{
    long long cc_num;
    int array_dig[15];
    int static sum_evens, sum_odds, sum_tot, valid = 0;
    printf("Enter your credit card number for validation: \n");
    cc_num = GetLongLong();

    for (int i=15; i >= 0; i--)
        {
            array_dig[i] = cc_num % 10;
            cc_num = cc_num / 10;

            //Sanity check
            printf("pos %d = %d\n", i, array_dig[i]);
        }

    //Summing digits
    for (int j=0; j < 16; j++)
        {
            if (j % 2 == 0)
            {
            sum_evens += array_dig[j];

            //Sanity check
            printf("sum_evens = %d/n", sum_evens);
            }

            else if (j % 2 == 1)
                {
                    if (array_dig[j] < 5)
                    {
                        sum_odds += 2 * array_dig[j];

                        //Sanity check
                        printf("sum_odds for char < 5 = %d\n", sum_odds);
                    }
                    else
                    {
                        sum_odds += (2 * array_dig[j]) % 10 + 1;

                        //Sanity check
                        printf("sum_odds for char >= 5 = %d\n", sum_odds);
                    }
                }
        }

         sum_tot = sum_evens + sum_odds;

         //Validation (if sum % 10 = 0 it is valid)
         valid = sum_tot % 10;
         printf("sum_tot = %d, valid = %d \n", sum_tot, valid);
            if (valid % 10 == 0)
                {
                    printf("Valid credit card \n");
                }
            else
                {
                    printf("Invalid credit card\n");
                }

}

答案 3 :(得分:0)

不是那么快。 =( 将值(数字)传递给long long变量cc_num时,程序会失去精度。

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

int main (void)

{
    long long cc_num;
    char cc_num_str[15];

    int static sum_evens, sum_odds, sum_tot, valid = 0;
    int static size;
    do
        {
            printf("Enter your credit card number for validation: \n");
            cc_num = GetLongLong();
            sprintf(cc_num_str, "%lld", cc_num);
            size = strlen (cc_num_str);
        }
    while (cc_num < 1 || cc_num > 9999999999999999 || size < 13 || size > 16);

    int array_dig[size];

    for (int i = size - 1; i >= 0; i--)
        {
            array_dig[i] = cc_num % 10;
            cc_num = cc_num / 10;

            //Sanity check
            printf("pos %d = %d\n", i, array_dig[i]);
        }

    //Summing digits
    for (int j=0; j < size; j++)
        {
            if (j % 2 == 0)
            {
            sum_evens += array_dig[j];

            //Sanity check
            printf("sum_evens = %d/n", sum_evens);
            }

            else if (j % 2 == 1)
                {
                    if (array_dig[j] < 5)
                    {
                        sum_odds += 2 * array_dig[j];

                        //Sanity check
                        printf("sum_odds for char < 5 = %d\n", sum_odds);
                    }
                    else
                    {
                        sum_odds += (2 * array_dig[j]) % 10 + 1;

                        //Sanity check
                        printf("sum_odds for char >= 5 = %d\n", sum_odds);
                    }
                }
        }

         sum_tot = sum_evens + sum_odds;

         //Validation (if sum % 10 = 0 it is valid)
         valid = sum_tot % 10;
         printf("sum_tot = %d, valid = %d \n", sum_tot, valid);
            if (valid % 10 == 0)
                {
                    //code that will verifies card type (to be written)

                    printf("Valid credit card \n");
                }

            else
                {
                    printf("Invalid. \n");
                }
    return 0;    
}

这是输出:

 Enter your credit card number for validation: 
    1234567809874366
    pos 15 = 8
    pos 14 = 3
    pos 13 = 6
    pos 12 = 9
    pos 11 = 5
    pos 10 = 8
    pos 9 = 9
    pos 8 = 0
    pos 7 = 8
    pos 6 = 7
    pos 5 = 6
    pos 4 = 5
    pos 3 = 4
    pos 2 = 3
    pos 1 = 2
    pos 0 = 1
    sum_evens = 1/nsum_odds for char < 5 = 4
    sum_evens = 4/nsum_odds for char < 5 = 12
    sum_evens = 9/nsum_odds for char >= 5 = 15
    sum_evens = 16/nsum_odds for char >= 5 = 22
    sum_evens = 16/nsum_odds for char >= 5 = 31
    sum_evens = 24/nsum_odds for char >= 5 = 32
    sum_evens = 33/nsum_odds for char >= 5 = 35
    sum_evens = 36/nsum_odds for char >= 5 = 42
    sum_tot = 78, valid = 8 
    Invalid.