c语言编译测有效信用卡号,关于C语言,编写信用卡号是否正确?急求代码~ 通过Luhn算法来验证通过...

本文详细讲解了如何通过编程计算两个日期之间的年份、月份和天数差,包括闰年的处理方法,并提供了一个C/C++函数示例。遇到问题时,解答了关于代码运行结果不显示的疑问,强调了printf用于输出结果的重要性。
摘要由CSDN通过智能技术生成

匿名用户

9级

2011-12-12 回答

先判断年份差,然后按每4年一个闰年计算年份的日期差,

然后再判断月份差,这需要结合当前年份是否闰年计算

追问:

具体代码如何实现呢?

追答:

void func(int year, int month, int day)

{

int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int totalDays = 0;

int totalYeas = year - 1949;

// calculate years firstly

totalDays = totalYears/4 * (365 + 365 + 365 + 366 );

if( totalYears%4 < 3 )

totalDays += 365 * (totalYears % 4);

else

totalDays += (365 * 3 + 366);

// Month

if( month>=10 )

{

int i=0;

for( i=10; i

totalDays += monthDays[i];

}

else

{

int i=0;

for( i=10; i>=month; i-- )

totalDays -= monthDays[i];

}

// Days

totalDays += day-1;

}

追问:

你的答案没结果显示。

代码不对啊

追答:

结果你printf一下totalDays不就好了吗?

这里有一个typo:

int totalYeas = year - 1949; -> int totalYears = year - 1949;

追问:

还是没结果啊。能运行但是没结果显示出来

追答:

你会用printf吗?

这句话之后:

totalDays += day-1;

printf("%d\n", totalDays);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值