文章目录
1.给定出生年月日及现在年月日,计算天数
思想:
计算由两部分组成
1.现在年到出生年是几年,并算出这些年的天数之和
2.现在月日距出生月日的天数
完整代码:
#include <stdio.h>
int main()
{
int sum_day(int, int); //?
int leap(int year);
int year1, month1, day1, days1;
int year, month, day, days, j, dayss = 0;
printf("请输入出生年月日: \n");
scanf("%d, %d, %d", &year1, &month1, &day1);
printf("请输入现在的年月日: \n");
scanf("%d, %d, %d", &year, &month, &day);
days1 = sum_day(month1, day1);
//是闰年则月份大于3即包括了2月,闰年2月多一天,所以+1
if(leap(year1) && (month1>=3)) //出生月日总天数
days1 = days1 + 1;
//月+日
days = sum_day(month, day);
if(leap(year)&&month>=3) //现在月日总天数
{
days = days + 1;
}
/*年 -> 天 */
if(year1>year)
printf("输入有误");
else if(year1==year)
{
printf("\n");
printf("总天数是%天\n", days-days1);
printf("总小时数是%d时\n", 24*(days-days1));
}
else
{
for(j=year1+1; j<=year-1; j++)
{
if(leap(j)==1)
{
dayss = dayss + 366 - days1;
}
else
{
dayss = dayss + days + 365 - days1;
}
}
printf("总天数是%d天\n", dayss);
printf("总小时数是%d时\n", 24*dayss);
}
return 0;
}
/*月+日 -> 天数 */
int sum_day(int month, int day)
{
int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i;
for(i=1; i<=month-1; i++)
day += day_tab[i];
return (day);
}
/*判断是否为闰年 */
int leap(int year)
{
int leap;
leap = (year%4==0 && year%100!=0) || year%400==0;
//如果是闰年leap=1,反之为0
return(leap);
}
2.指定年月日距离1990年1月1日的天数及小时
思想同上
/*
指定年月日距离1990年1月1日的天数及小时
*/
#include <stdio.h>
//存储年月日
typedef struct date{
int year;
int month;
int day;
}DATE;
int countday(DATE);
int runyear(int);
int main()
{
DATE today;
int totalday;
printf("请输入指定年月日: ");
scanf("%d, %d, %d", &today.year, &today.month, &today.day);
totalday = countday(today) - 1;
printf("总天数是%d天\n", totalday);
printf("总小时是%d时\n", 24*totalday);
return 0;
}
//计算总天数+1
int countday(DATE currentday)
{
//存储每个月天数 ,2月默认值28天
int permonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int totalday = 0, year, i;
//年->天数
for(year=1990; year<currentday.year; year++)
{
if(runyear(year)) //是闰年的话,一年就是366天
totalday = totalday + 366;
else
totalday = totalday +365;
}
//如果是闰年的话,2月就是29天
if(runyear(currentday.year))
permonth[2] += 1;
//月->天数
for(i=0; i<currentday.month; i++)
{
totalday += permonth[i];
}
//(年+月)+日
totalday += currentday.day;
return(totalday);
}
//判断是否为闰年
int runyear(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else
return 0;
}
3.计算某年某月某日是本年度第几天
/*
计算某年某月某日是本年度第几天
*/
#include <stdio.h>
int main()
{
int sum_day(int, int);
int leap(int year);
int year, month, day, days;
printf("\n");
printf("请输入所求日期: \n");
scanf("%d, %d, %d", &year, &month, &day);
printf("%d, %d, %d", year, month, day);
days = sum_day(month, day);
if(leap(year)&&month>=3)
{
days = days + 1;
}
printf("是本年度第%d天\n", days);
return 0;
}
int sum_day(int month, int day)
{
int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i;
for(i=1; i<=month-1; i++)
{
day += day_tab[i];
}
return(day);
}
int leap(int year)
{
int leap;
leap = (year%4==0 && year%100!=0) || year%400 == 0;
return(leap);
}
4.给出年月日,计算该日是星期几
/*
计算某年某月某日是星期几
*/
#include <stdio.h>
int main()
{
int a, b, c, d, s, z;
printf("请输入年月日: \n");
scanf("%d, %d, %d", &a, &b, &c);
if((a%4==0&&a%100!=0) || (a%400==0))
{
if(b==1)
d = c;
if(b==2)
d = c + 31;
if(b==3)
d = c + 60;
if(b==4)
d = c + 91;
if(b==5)
d = c + 121;
if(b==6)
d = c + 152;
if(b==7)
d = c + 182;
if(b==8)
d = c + 213;
if(b==9)
d = c + 244;
if(b==10)
d = c + 274;
if(b==11)
d = c + 305;
if(b==12)
d = c + 335;
}
else
{
if(b==1)
d = c;
if(b==2)
d = c + 31;
if(b==3)
d = c + 59;
if(b==4)
d = c + 90;
if(b==5)
d = c + 120;
if(b==6)
d = c + 151;
if(b==7)
d = c + 181;
if(b==8)
d = c + 212;
if(b==9)
d = c + 243;
if(b==10)
d = c + 273;
if(b==11)
d = c + 304;
if(b==12)
d = c + 334;
}
s = a - 1 + (a - 1) / 4 - (a - 1) / 100+ (a - 1) / 400 + d;
z = s % 7;
if(z==0)
printf("%d年%d月%d日这天是星期天\n", a, b, c);
if(z==1)
printf("%d年%d月%d日这天是星期一\n", a, b, c);
if(z==2)
printf("%d年%d月%d日这天是星期二\n", a, b, c);
if(z==3)
printf("%d年%d月%d日这天是星期三\n", a, b, c);
if(z==4)
printf("%d年%d月%d日这天是星期四\n", a, b, c);
if(z==5)
printf("%d年%d月%d日这天是星期五\n", a, b, c);
if(z==6)
printf("%d年%d月%d日这天是星期六\n", a, b, c);
return 0;
}
5.给出年份,计算元旦那天是星期几
/*
给出年份,计算元旦那天是星期几
*/
#include <stdio.h>
int main()
{
int a, s, z;
printf("请输入年份:\n");
scanf("%d", &a);
s = a + (a - 1) / 4 - (a - 1) / 100+ (a - 1) / 400;
z = s % 7;
if(z==0)
printf("元旦这一天是星期天\n");
if(z==1)
printf("元旦这一天是星期一\n");
if(z==2)
printf("元旦这一天是星期二\n");
if(z==3)
printf("元旦这一天是星期三\n");
if(z==4)
printf("元旦这一天是星期四\n");
if(z==5)
printf("元旦这一天是星期五\n");
if(z==6)
printf("元旦这一天是星期六\n");
return 0;
}
总结
主要模块
1.判断是否为闰年
int leap(int year)
{
int leap;
leap = (year%4==0 && year%100!=0) || year%400==0;
//如果是闰年leap=1,反之为0
return(leap);
}
2.计算从现在月日距出生月日的天数
/*月+日 -> 天数 */
int sum_day(int month, int day)
{
int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i;
for(i=1; i<=month-1; i++)
day += day_tab[i];
return (day);
}
3.现在年到出生年是几年,并算出这些年的天数之和
/*年 -> 天 */
if(year1>year)
printf("输入有误");
else if(year1==year)
{
printf("\n");
printf("总天数是%天\n", days-days1);
printf("总小时数是%d时\n", 24*(days-days1));
}
else
{
for(j=year1+1; j<=year-1; j++)
{
if(leap(j)==1)
{
dayss = dayss + 366 - days1;
}
else
{
dayss = dayss + days + 365 - days1;
}
}
printf("总天数是%d天\n", dayss);
printf("总小时数是%d时\n", 24*dayss);
}
return 0;
}
4、对于闰年、平年的总天数处理
方式一:先给二月份一个默认值,最后在计算的总天数中+1即可
//是闰年则月份大于3即包括了2月,闰年2月多一天,所以+1
if(leap(year1) && (month1>=3)) //出生月日总天数
days1 = days1 + 1;
//月+日
days = sum_day(month, day);
if(leap(year)&&month>=3) //现在月日总天数
{
days = days + 1;
}
方式二:修改月份
//计算总天数+1
int countday(DATE currentday)
{
//存储每个月天数 ,2月默认值28天
int permonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int totalday = 0, year, i;
//年->天数
for(year=1990; year<currentday.year; year++)
{
if(runyear(year)) //是闰年的话,一年就是366天
totalday = totalday + 366;
else
totalday = totalday +365;
}
//如果是闰年的话,2月就是29天
if(runyear(currentday.year))
permonth[2] += 1;
//月->天数
for(i=0; i<currentday.month; i++)
{
totalday += permonth[i];
}
//(年+月)+日
totalday += currentday.day;
return(totalday);
}