用C语言来编程,具体如下

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

#include <stdio.h>
int main()
{
    int year, month, day;    //输入的年月日
    int sum_1 = 0, sum_2 = 0, sum_3, week_day; //分别代表要求的天数与星期
    int i, j;
    int common_year[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; //平年每个月的天数
    
    //获取年月日格式 2015/4/16,2015 4 16, 2015$4&l6等等中间只要有两个分隔符就行。
    scanf("%d",&year);
    getchar();
    scanf("%d",&month);
    getchar();
    scanf("%d",&day);
    
    //计算到前一年的年末的天数
    for (i = 1; i < year; i++) {
        if( (i%4 == 0 && i %100 != 0) || i%400 == 0) {
            //闰年
            sum_1 += 366;
        } else {
            //平年
            sum_1 += 365;
        }
    }
    printf("前一年的年末:%d\n",sum_1);
    
    //计算前一个月末的天数
    if(month <= 1) {
        //一月份,相当于去年年末
        sum_2 = sum_1;
    } else if (month <= 2) {
        //2月份
        sum_2 = sum_1 + 31;
    } else if (month > 2) {
        //大于2月份须判断,闰年还是平年。
        if( (i%4 == 0 && i %100 != 0) || i%400 == 0) {
            //平年2月29天。
            common_year[1]  = 29;
        }
        //本年月的天数
        for (j = 0; j < month-1; j++) {
            sum_2 += common_year[j];
        }
        
        sum_2 += sum_1;
        
    }
    printf("前一月末天数:%d\n", sum_2);
    //月末加本月天数,就是总天数。
    sum_3 = sum_2 + day;
    printf("到这一天总共有:%d\n", sum_3);
    //求星期几,公元一月一日是星期一(虽然公元元年一月一日是星期几的问题有争论。但这个题不是考你们历法的问题,当星期一用就行,或者把问题摔给出题的人,让他证明一下历法是连续的,一天没落的)。
    week_day = sum_3 % 7;
    if(week_day == 0) {
        printf("这一天为星期日\n");
    } else {
        printf("这一天为星期%d\n", week_day);
    }
    
  return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-04-16
//程序代码如下,如对你有帮助 请采纳
#include<stdio.h>
struct Day
{
int year;
int month;
int day;
int xingqi;
};
void main()
{
Day first,input;
first.year=1;
first.month=1;
first.day=1;
first.xingqi=1;
printf("请输入 年 月 日:\n");
scanf("%d %d %d",&input.year,&input.month,&input.day);
int totalday;
int totalyear;
int totalmonth;
totalyear=(input.year-1)*365;
totalmonth=(input.month-1)*30+totalyear;
totalday=totalmonth+input.day;
input.xingqi=totalday%7;
if(input.xingqi==0)
input.xingqi=7;
printf("\n1年1月1日到该日期前一年的年末总共有%d天",totalyear);
printf("\n到该日期前一个月的月末总共有%d天",totalmonth);
printf("\n到这一天总共有%d天",totalday);
printf("\n这一天是星期%d\n",input.xingqi);
}

本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网