Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CS50学分分配代码中的问题_C_Cs50 - Fatal编程技术网

CS50学分分配代码中的问题

CS50学分分配代码中的问题,c,cs50,C,Cs50,我正试图为CS50 Credit中的信用问题集编写代码,我才刚刚开始,但我遇到了一个问题。现在,我正在尝试提取卡片数字,以便稍后使用它们,并检查提取是否顺利。代码似乎适用于卡号中的每个数字,除了从末尾开始的第12个数字 e、 g。 卡号: 输入:1111111111 输出:11111111 01111 虽然在某些情况下不会出现此问题: 输入: 1234567890123456 输出: 1234567890123456 这是我的代码,请不要太苛刻,我是新手: 我知道还有其他方法可以得到这些数字,但

我正试图为CS50 Credit中的信用问题集编写代码,我才刚刚开始,但我遇到了一个问题。现在,我正在尝试提取卡片数字,以便稍后使用它们,并检查提取是否顺利。代码似乎适用于卡号中的每个数字,除了从末尾开始的第12个数字

e、 g。 卡号: 输入:1111111111 输出:11111111 01111

虽然在某些情况下不会出现此问题:

输入: 1234567890123456 输出: 1234567890123456

这是我的代码,请不要太苛刻,我是新手: 我知道还有其他方法可以得到这些数字,但我现在真的想知道为什么它不起作用。提前谢谢

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

long card_no(void);
int main(void)
{
    long card = card_no();
    int one = (card % 10);
    int two = (card % 100 - card % 10) * 0.1;
    int tree = (card % 1000 - card % 100) * 0.01;
    int four = (card % 10000 - card % 1000) * 0.001;
    int five = (card % 100000 - card % 10000) * 0.0001;
    int six = (card % 1000000 - card % 100000) * 0.00001;
    int seven = (card % 10000000 - card % 1000000) * 0.000001;
    int eight = (card % 100000000 - card % 10000000) * 0.0000001;
    int nine = (card % 1000000000 - card % 100000000) * 0.00000001;
    int ten = (card % 10000000000 - card % 1000000000) * 0.000000001;
    int eleven = (card % 100000000000 - card % 10000000000) * 0.0000000001;
    int twelve = (card % 1000000000000 - card % 100000000000) * 0.00000000001;
    int thirteen = (card % 10000000000000 - card % 1000000000000) * 0.000000000001;
    int forteen = (card % 100000000000000 - card % 10000000000000) * 0.0000000000001;
    int fifteen = (card % 1000000000000000 - card % 100000000000000) * 0.00000000000001;
    int sixteen = (card % 10000000000000000 - card % 1000000000000000) * 0.000000000000001;

    printf("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i\n", sixteen, fifteen, forteen, thirteen, twelve, eleven, ten, nine, eight, seven, six, five, four, tree, two, one);

}

//Prompt user for 13 to 16 digits card number
long card_no(void)
{
    long n;
    do
    {
       n = get_long("Card number:\n"); 
    }
    while (n < 1000000000000 || n > 9999999999999999);
    return n;
}

这段代码可能会向您展示一种更简单的方法。 它一次提取一个数字的所有数字,而不使用像10000000这样越来越庞大的数字


浮点数学不是精确的-像0.1这样的值不能用有限的位数精确表示,所以只能得到像0.099这样的近似值。。。或者0.1000…1或者类似的。因此,在对第五个数字进行计算后,得到的值为0.9…,当转换为int时,得到0

另一个不依赖浮点数学的选项是,通过增加数值大小并取结果的%10来除以数字。瞧

(123 /   1) % 10  == 123 % 10 == 3
(123 /  10) % 10  ==  12 % 10 == 2
(123 / 100) % 10  ==   1 % 10 == 1
整数除法产生整数结果-任何小数部分都被丢弃

方便提示,当您发现自己创建了一组具有顺序名称的相同类型的变量时,第一个、第二个、第三个或一个、两个、三个,这是一个非常强烈的提示,表明您想要使用数组:


也许卡号在很长一段时间内都放不下。为什么不作为字符串处理,分别获取每个数字呢?或者,当您与浮点数相乘时,可能会存在不精确性。为什么不使用整数除法,/?程序到底应该做什么?不管怎么说,它看起来很奇怪。
Digit 1 = 4
Digit 2 = 6
Digit 3 = 4
Digit 4 = 5
Digit 5 = 6
Digit 6 = 1
Digit 7 = 9
Digit 8 = 7
Digit 9 = 8
Digit 10 = 4
Digit 11 = 6
Digit 12 = 5
(123 /   1) % 10  == 123 % 10 == 3
(123 /  10) % 10  ==  12 % 10 == 2
(123 / 100) % 10  ==   1 % 10 == 1
#define  NUM_DIGITS 16
int digits[NUM_DIGITS];

/** 
 * get card number as before
 */

long magnitude = 1;
/**
 * Arrays are indexed from 0 to N-1
 */
for ( size_t i = 0; i < NUM_DIGITS; i++ ) 
  digits[i] = (card / magnitude) % 10;
  magnitude *= 10;
}

/**
 * I typically use size_t for array indices, which is an unsigned type.
 * This means it cannot represent negative values.  If we loop while "i >= 0",
 * then the loop will never exit, since subtracting 1 from 0 will "wrap
 * around" to the largest value.  So even though the array is indexed from
 * NUM_DIGITS-1 to 0, we loop over NUM_DIGITS to 1 and subtract 1 from 
 * i in the subscript operation.
 */
for ( size_t i = NUM_DIGITS; i > 0; i-- ) 
  printf( "%d", digits[i-1] );