提问人:Abdul Raffay 提问时间:11/7/2023 最后编辑:Jonathan LefflerAbdul Raffay 更新时间:11/7/2023 访问量:42
PSET 01,CS50x(信用);我的程序没有打印卡名称
PSET 01, CS50x (Credit); My program is not printing the card names
问:
我正在运行代码,但每当我输入合法或正确的卡否时,它都会再次提示我相同的打印信息卡号,但是当我输入错误的数字时,它会给我“无效”,这是正确的。 我只是想知道为什么我的程序没有打印 VISA、MASTER、AMEX 的答案
#include <stdio.h>
#include "cs50.h"
int main(void)
{
long card;
do
{
card = get_long("Card no: ");
} while (card < 0); // Card number must be greater than 0.
int card1, card2, card3, card4, card5, card6, card7, card8;
card1 = ((card % 100) / 10) * 2;
card2 = ((card % 10000) / 1000 * 2);
card3 = ((card % 1000000) / 100000 * 2);
card4 = ((card % 100000000) / 10000000 * 2);
card5 = ((card % 10000000000) / 1000000000 * 2);
card6 = ((card % 1000000000000) / 100000000000 * 2);
card7 = ((card % 100000000000000) / 10000000000000 * 2);
card8 = ((card % 10000000000000000) / 1000000000000000 * 2);
card1 = (card1 % 100 / 10) + (card1 % 10);
card2 = (card1 % 100 / 10) + (card2 % 10);
card3 = (card1 % 100 / 10) + (card3 % 10);
card4 = (card1 % 100 / 10) + (card4 % 10);
card5 = (card1 % 100 / 10) + (card5 % 10);
card6 = (card1 % 100 / 10) + (card6 % 10);
card7 = (card1 % 100 / 10) + (card7 % 10);
card8 = (card1 % 100 / 10) + (card8 % 10);
int sum1 = card1 + card2 + card3 + card4 + card5 + card6 + card7 + card8;
// Now we need to find the digits that are not multiplied by 2.
int card9, card10, card11, card12, card13, card14, card15, card16;
card9 = (card % 10);
card10 = ((card % 1000) / 100);
card11 = ((card % 100000) / 10000);
card12 = ((card % 10000000) / 1000000);
card13 = ((card % 1000000000) / 100000000);
card14 = ((card % 100000000000) / 10000000000);
card15 = ((card % 10000000000000) / 1000000000000);
card16 = ((card % 1000000000000000) / 100000000000000);
int sum2 = card9 + card10 + card11 + card12 + card13 + card14 + card15 + card16;
int sum3 = sum1 + sum2;
int length = 0;
long visa = card;
long master = card;
long amex = card;
if ((sum3 % 10) != 0)
{
printf("%s\n", "INVALID");
return 0;
}
// Now we need to differentiate between VISA, Master and Amex.
while (card > 0)
{
card = card / 10;
length++;
}
// Identify if the card is VISA.
while (visa >= 10)
{
visa /= 10;
}
if (visa == 4 && (length == 13 || length == 16))
{
printf("%s\n ", "VISA");
return 0;
}
// Identify if the card is AMEX
while (amex >= 10000000000000)
{
amex /= 10000000000000;
}
if (length == 15 && (amex == 34 || amex == 37))
{
printf("%s\n", "AMEX");
return 0;
}
// Identify if the card is MASTER
while (master >= 100000000000000)
{
master /= 100000000000000;
}
if (length == 16 && (master == 51 || master == 52 || master == 53 ||
master == 54 || master == 55 || master == 56))
{
printf("%s\n", "MASTERCARD");
return 0;
}
else
printf("%s\n", "INVALID");
return 0;
}
我认为我的代码是每次我运行我的程序时都会打印出来的
卡号当我输入任何有效的卡号时,它会再次提示相同的内容,即卡号。但是当我输入错误的数字时,我给出 INVALID
我无法弄清楚为什么它不打印“VISA, MASTER, AMEX”
同样,当我运行调试时,它会给出这个
credit.c: In function 'main':
credit.c:17:13: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
card5 = ((card % 10000000000) / 1000000000 * 2);
^
credit.c:18:13: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
card6 = ((card % 1000000000000) / 100000000000 * 2);
^
credit.c:19:13: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
card7 = ((card % 100000000000000) / 10000000000000 * 2);
^
credit.c:20:13: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
card8 = ((card % 10000000000000000) / 1000000000000000 * 2);
^
credit.c:41:14: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
card14 = ((card % 100000000000) / 10000000000);
^
credit.c:42:14: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
card15 = ((card % 10000000000000) / 1000000000000);
^
credit.c:43:14: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
card16 = ((card % 1000000000000000) / 100000000000000);
^
credit.c:77:17: warning: comparison is always false due to limited range of data type [-Wtype-limits]
while (amex >= 10000000000000)
^~
credit.c:79:17: warning: conversion to 'long int' from 'long long int' may alter its value [-Wconversion]
amex /= 10000000000000;
^~~~~~~~~~~~~~
credit.c:88:19: warning: comparison is always false due to limited range of data type [-Wtype-limits]
while (master >= 100000000000000)
^~
credit.c:90:19: warning: conversion to 'long int' from 'long long int' may alter its value [-Wconversion]
master /= 100000000000000;
^~~~~~~~~~~~~~~
答:
0赞
Chris
11/7/2023
#1
An 不够大,无法像信用卡那样折叠 16 位数字。对于 32 位有符号整数,最大值为 2,147,483,647,而对于 64 位有符号整数,最大值为 9,223,372,036,854,775,807。一个 64 位无符号整数得到 18,446,744,073,709,551,615。int
数据类型之间的这种大小差异正是警告所告知的。如果你养成了使用编译器(或编译器中的等效工具)进行编译的习惯,你将被迫解决这些问题。-Werror
64 位的 int 足够大,但您最好承认信用卡号根本不是整数,而是数值字符串。
作为字符串,迭代数字也更容易。
评论
#include <limits.h>
和。你不需要处理数字;在这种情况下,使用字符(字符串)会更容易。printf("The largest long available is %ld\n", LONG_MAX);