C语言编程100题——第四题

2022/4/10 9:13:13

本文主要是介绍C语言编程100题——第四题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

输入某年某月某日,判断这一天是某年的第几天?

本程序评价,本人觉得本程序过于繁杂。这是在自学 C Primer Plus过程写的,等自学完了之后,会用其他方法来优化。

  1 /*程序4:输入某年某月某日,判断这一天是这一年的第几天? */
  2 
  3 #include<stdio.h>
  4 #include<conio.h>
  5 
  6 void main()
  7 {
  8     int year,month,day,number;
  9     REDO: 
 10     printf("输入年月日\"年-月-日\",然后按enter下一步\n");
 11     scanf("%d-%d-%d",&year,&month,&day);
 12     if(month<=12&&month>=1&&day>=1&&day<=31)
 13     {
 14          if(year%400==0||(year%4==0&&year%100!=0))       
 15         {
 16             if(month==1)
 17             {
 18                 printf("这一天是%d年中的第%d天\n",year,day);
 19             }
 20             else if(month==2)
 21             {
 22                 printf("这一天是%d年中的第%d天\n",year,day+31);
 23             }
 24             else if(month>2)
 25             {
 26                 if(month<=7&&month%2==0)
 27                 {
 28                     day=month/2*31+day+(month/2-1)*30-1;
 29                     printf("这一天是%d年中的第%d天\n",year,day);
 30                 }
 31                 else if (month<=7&&month%2==1)
 32                 {
 33                     day=(month-1)/2*31+(month-1)/2*30+day-1;
 34                     printf("这一天是%d年中的第%d天\n",year,day);
 35                 }
 36                 if (month>7&&month%2==0)
 37                 {
 38                     day=(month-8)/2*31+(month-8)/2*30+day;
 39                     day=day+31+29+31+30+31+30+31;
 40                     printf("这一天是%d年中的第%d天\n",year,day);
 41                 }
 42                 else if(month>7&&month%2==1)
 43                 {
 44                     if(month==9)
 45                     {
 46                         day=31+day;
 47                     }
 48                     else if(month==11)
 49                     {
 50                         day=31+30+31+day;
 51                     }
 52                     day=day+31+29+31+30+31+30+31;
 53                     printf("这一天是%d年中的第%d天\n",year,day);
 54                 }
 55             }
 56         }
 57         else
 58         {
 59             if(month==1)
 60             {
 61                 printf("这一天是%d年中的第%d天\n",year,day);
 62             }
 63             else if(month==2)
 64             {
 65                  printf("这一天是%d年中的第%d天\n",year,day+31);
 66             }
 67             else if(month>2)
 68             {
 69                 if(month<=7&&month%2==0)
 70                 {
 71                     day=month/2*31+day+(month/2-1)*30-2;
 72                     printf("这一天是%d年中的第%d天\n",year,day);
 73                 }
 74                 else if (month<=7&&month%2==1)
 75                 {
 76                     day=(month-1)/2*31+(month-1)/2*30+day-2;
 77                     printf("这一天是%d年中的第%d天\n",year,day);
 78                 }
 79                 if (month>7&&month%2==0)
 80                 {
 81                     day=(month-8)/2*31+(month-8)/2*30+day;
 82                     day=day+31+28+31+30+31+30+31;
 83                     printf("这一天是%d年中的第%d天\n",year,day);
 84                 }
 85                 else if(month>7&&month%2==1)
 86                 {
 87                     if(month==9)
 88                     {
 89                         day=31+day;
 90                     }
 91                     else if(month==11)
 92                     {
 93                         day=31+30+31+day;
 94                     }
 95                     day=day+31+28+31+30+31+30+31;
 96                     printf("这一天是%d年中的第%d天\n",year,day);
 97                 }
 98             }
 99         }
100     }
101     else
102     {
103         printf("输入年月日有误或者输入的格式不对,请重新运行输入");
104         goto REDO;
105     }
106     getch();
107 }

 



这篇关于C语言编程100题——第四题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程