C初学者帮助:给定日期的星期几 [英] C beginner help : day of week for any given date

查看:35
本文介绍了C初学者帮助:给定日期的星期几的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提示是:实现一个函数,该函数读取包含日历日期文本说明的字符串,并打印出一周中的相应日期(周一至周日).此功能的两种有效输入格式是:

mm/dd/yyyy

示例:2014年3月4日
输出:星期二

dd,yyyy

示例:2014年3月4日
输出:星期二

其中dd是数字日,mm是数字月,yyyy是年,Month是月的名称.所有的日期和月份都用两位数字指定(例如,对于3月,请使用03而不是3).在第二种有效格式中,Month和dd之间以及dd和yyyy之间只有一个空格.为了获得此任务的全部功劳,您的程序应以正确的格式打印出每周正确的日期以进行任何输入.

因此,到目前为止,我能够获得每一天的正确日期,但2005、2009、2013、2017等年份除外……它们总是落后一天,我注意到它的发展趋势每4年的趋势最终会落后1天.我不知道怎么了.是因为我每年使用365.25的方法不正确吗?

我的代码:

 #include< stdio.h>int main(){int month,day1,day2,totdays,year,dm,dn,leap,rmd;printf(");scanf(%d/%d/%d",& month,& day1,& year);if((((year%4 == 0)&&(year%100!= 0))||(year%400 == 0)){如果(月== 1)dm = 0;如果(月== 2)dm = 31;如果(月== 3)dm = 60;如果(月== 4)dm = 91;如果(月== 5)dm = 121;如果(月== 6)dm = 152;如果(月== 7)dm = 182;如果(月== 8)dm = 213;如果(月== 9)dm = 244;如果(月== 10)dm = 274;如果(月== 11)dm = 305;如果(月== 12)dm = 335;}别的{如果(月== 1)dm = 0;如果(月== 2)dm = 31;如果(月== 3)dm = 59;如果(月== 4)dm = 90;如果(月== 5)dm = 120;如果(月== 6)dm = 151;如果(月== 7)dm = 181;如果(月== 8)dm = 212;如果(月== 9)dm = 243;如果(月== 10)dm = 273;如果(月== 11)dm = 304;如果(月== 12)dm = 334;}day2 =(1970年)*(365.25);dn = dm + day1;totdays = day2 + dn;rmd = totdays%7;如果(rmd == 5){printf(星期一\ n");}如果(rmd == 6){printf(星期二\ n");}如果(rmd == 0){printf(星期三\ n");}如果(rmd == 1){printf(星期四\ n");}如果(rmd == 2){printf(星期五\ n");}如果(rmd == 3){printf("Saturday \ n");}如果(rmd == 4){printf("Sunday \ n");}返回0;} 

解决方案

1969年不是a年,1972年是leap年.当你做

  day2 =(year-1970)*(365.25); 

要找出年份 year 的1月1日是多少天,您会计数

  • '70'0天
  • '71的365.25天
  • '72的730.5天
  • '73'的
  • 1095.75天
  • '74的
  • 1461天

浮点计算的小数部分被截断了,因此day2不会计入从02/29/1972到01/01/1974的额外天数,而不是应该计入的01/01/1973./p>

换一种说法,您假设1970年是after年之后的第一年,因此a年直到4年后才算在内.

The prompt is: Implement a function that reads in a string containing a textual description of a cal- endar date and that prints out the corresponding day of the week (Monday–Sunday). The two valid input formats for this function are:

mm/dd/yyyy

Example: 03/04/2014
Output: Tuesday

Month dd, yyyy

Example: March 04, 2014
Output: Tuesday

where dd is the numeric day, mm is the numeric month, yyyy is the year and Month is the name of the month. All days and months are specified using two digits (i.e. for March, use 03 instead of 3). In the second valid format, there is a single space between Month and dd and between dd, and yyyy. In order to receive full credit on this task, your program should print out the correct day of the week for any input in a correct format.

So as of right now i am able to get the correct days for every single day except in the years 2005 2009 2013 2017 etc etc... they are always a day behind, i notice that its going by a trend of every 4 years the days end up 1 day behind. Im not sure whats wrong. is it cause my method of using 365.25 as each year is wrong?

My code:

#include<stdio.h>

int main()
{
int month,day1,day2,totdays,year,dm,dn,leap,rmd;


    printf(" ");
    scanf("%d/%d/%d",&month,&day1,&year);


    if(((year%4==0) && (year%100!=0)) || (year%400==0))
      {
         if(month==1)
            dm=0;

         if(month==2)
            dm=31;

         if(month==3)
            dm=60;

         if(month==4)
            dm=91;

         if(month==5)
            dm=121;

         if(month==6)
            dm=152;

         if(month==7)
            dm=182;

         if(month==8)
            dm=213;

         if(month==9)
            dm=244;

         if(month==10)
            dm=274;

         if(month==11)
            dm=305;

         if(month==12)
            dm=335;
       }
    else
       {
         if(month==1)
            dm=0;

         if(month==2)
            dm=31;

         if(month==3)
            dm=59;

         if(month==4)
            dm=90;

         if(month==5)
            dm=120;

         if(month==6)
            dm=151;

         if(month==7)
            dm=181;

         if(month==8)
            dm=212;

         if(month==9)
            dm=243;

         if(month==10)
            dm=273;

         if(month==11)
            dm=304;

         if(month==12)
            dm=334;
       }


      day2=(year-1970)*(365.25);
      dn=dm+day1;
      totdays=day2+dn;

      rmd=totdays%7;

      if(rmd==5)
        {
           printf("Monday \n");
        }

      if(rmd==6)
        {
           printf("Tuesday \n");
        }

      if(rmd==0)
        {
            printf("Wednesday \n");
        }

      if(rmd==1)
        {
            printf("Thursday \n");
        }

      if(rmd==2)
        {
            printf("Friday \n");
        }

      if(rmd==3)
        {
            printf("Saturday \n");
        }

      if(rmd==4)
        {
            printf("Sunday \n");
        }

      return 0;

}

解决方案

1969 wasn't a leap year, 1972 was. When you do

day2=(year-1970)*(365.25);

to discover how many days off January 1st of year year is, you'll count

  • 0 days for '70
  • 365.25 days for '71
  • 730.5 days for '72
  • 1095.75 days for '73
  • 1461 days for '74

The fractional portion of the floating point calculation is truncated, so day2 isn't going to count the extra day from 02/29/1972 until 01/01/1974, instead of 01/01/1973 as it should.

Put another way, you are making the assumption that 1970 was the first year after a leap year, so a leap day won't be counted until four years later.

这篇关于C初学者帮助:给定日期的星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆