c语言算公元1年日历,C语言实验:输入任意一个日期的年、月、日的值,求出从公元1年1月1日到这一天总共有多少天,并求出这一天是星期几。...

要求:输入任意一个日期的年、月、日的值,求出从公元1年1月1日到这一天总共有多少天,并求出这一天是星期几。

简单的循环结构,并结合基姆拉尔森公式,注意月份转换。

下面是源码,仅供参考:

#include

using namespace std;

void month1(int i, int &count)

{

switch (i)

{

case 1:count += 31;break;

case 2:count += 29;break;

case 3:count += 31;break;

case 4:count += 30;break;

case 5:count += 31;break;

case 6:count += 30;break;

case 7:count += 31;break;

case 8:count += 31;break;

case 9:count += 30;break;

case 10:count += 31;break;

case 11:count += 30;break;

case 12:count += 31;break;

}

}

void month2(int i, int &count)

{

switch (i)

{

case 1:count += 31;break;

case 2:count += 29;break;

case 3:count += 31;break;

case 4:count += 30;break;

case 5:count += 31;break;

case 6:count += 30;break;

case 7:count += 31;break;

case 8:count += 31;break;

case 9:count += 30;break;

case 10:count += 31;break;

case 11:count += 30;break;

case 12:count += 31;break;

}

}

void day1(int a)

{

switch(a+1)

{

case 1: cout<

case 2: cout<

case 3: cout<

case 4: cout<

case 5: cout<

case 6: cout<

case 7: cout<

}

}

int main()

{

int year, month, day;

while(1)

{

int count = 0;

cout<

cin>>year>>month>>day;

for(int i = 1; i < year; i ++)

{

if((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0))

{

count += 366;

}

else

{

count += 365;

}

}

for(int i = 1; i < month; i++)

{

if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))

{

month1(i,count);

}

else

{

month2(i,count);

}

}

count += day;

cout<

if(month==1||month==2)

{

month+=12;

year--;

}

int d = (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;

if(d==0)

{

d=7;

}

cout<

day1(d);

cout<

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言实现输入任意一个日期的年、月、从这一年的1月1到这一天总共有多少天的代码: ```c #include <stdio.h> int is_leap_year(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } int days_in_month(int year, int month) { static const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month == 2 && is_leap_year(year)) { return 29; } else { return days[month - 1]; } } int days_in_year(int year) { return is_leap_year(year) ? 366 : 365; } int days_since_jan1(int year, int month, int day) { int days = 0; for (int m = 1; m < month; m++) { days += days_in_month(year, m); } days += day - 1; return days; } int main() { int year, month, day; printf("请输入年、月、:"); scanf("%d%d%d", &year, &month, &day); int days = 0; for (int y = 1; y < year; y++) { days += days_in_year(y); } days += days_since_jan1(year, month, day); printf("从 %d 年 1 月 1 到这一天总共有 %d 天\n", year, days); return 0; } ``` 在这个程序中,我们通过 `is_leap_year` 函数来判断一个年份是否为闰年,通过 `days_in_month` 函数来确定一个月份有多少天,通过 `days_in_year` 函数来确定一个年份有多少天,通过 `days_since_jan1` 函数来计从一年的 1 月 1 到这一天总共有多少天。最后,我们在 `main` 函数中读入日期的年、月、,然后根据从公元第一天开始的日历规则计从这一年的 1 月 1 到这一天总共有多少天。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值