计算某年中的一天是这年的第几天C程序

           /*計算某年某日為當年的第幾天*/
           /*作者:石輔寸*/
           /*2019,2,3*/
           #include<stdio.h>
           int year,month,day;
           int monthAyear[] = {31,28,31,30,31,30,31,31,30,31,30,31};
           void writeDate();
           void daySumout(int year,int month,int day);
           int checkmonth(int month);
           int checkday(int day);
           int checkFeb(int year,int month,int day);
           int checkMinmonth(int month);
           void repeat();
           int main()
           {
           	writeDate();
           	daySumout(year,month,day);
           	repeat();
           	return 0;
           }
           /*檢查日期是否輸入正確*/
           /*檢查月份*/
           int checkmonth(int month)
           {
           	if(month <= 0 || month > 12)
           	{
           		printf("月份輸入錯誤,請重新輸入。\n");
           		return 1;
           	}
           	else
               {
                   return 0;
           	}
           }
           /*粗略檢查日期*/
           int checkday(int day)
           {
               if(day > 31 || day <= 0)//大於31天,小於1天必定錯誤
           	{
           		printf("日期輸入錯誤,請重新輸入。\n");
           		return 1;
           	}
           	else
               {
                   return 0;
               }
           }
           
           /*單獨檢查二月天數*/
           int checkFeb(int year,int month,int day)
           {
               if(month == 2)
               {
                   if((year%4 == 0 && year%100 != 0) || (year%400 == 0))//檢查閏年二月天數
                   {
                       if(day >29)
                       {
                           printf("日期輸入錯誤,請重新輸入。\n");
                           return 1;
                       }
                       else
                       {
                           return 0;
                       }
                   }
                   else//檢查平年二月天數
                   {
                       if(day >28)
                       {
                           printf("日期輸入錯誤,請重新輸入。\n");
                           return 1;
                       }
                       else
                       {
                           return 0;
                       }
                   }
           
           	}
           }
           
           /*檢查小月天數*/
           int checkMinmonth(int month)
           {
               if(monthAyear[month-1] > 30)
           	{
           		switch(month)
           		{
           			case 4: printf("日期輸入錯誤,請重新輸入。\n");return 1;
           			break;
           			case 6: printf("日期輸入錯誤,請重新輸入。\n");return 1;
           			break;
           			case 9: printf("日期輸入錯誤,請重新輸入。\n");return 1;
           			break;
           			case 11:printf("日期輸入錯誤,請重新輸入。\n");return 1;
           			break;
           			default:return 0;
           		}
           	}
           	else
               {
                   return 0;
               }
           }
           /*寫函數*/
           void writeDate()
           {
           	printf("請輸入日期:");
           	scanf("%d,%d,%d",&year,&month,&day);
           	//規定返回值為1時為輸入錯誤
           	if(checkday(day) == 1)
           	{
           		writeDate();
           	}
           	if(checkmonth(month) == 1)
           	{
           		writeDate();
           	}
           	if(checkFeb(year,month,day) == 1)
           	{
           		writeDate();
           	}
           	if(checkMinmonth(month) == 1)
           	{
           		writeDate();
           	}
           }
           
           void daySumout(int year,int month,int day)
           {
           	int daySum = 0,i;
           	/*month == 1;
           	daySum == day;
           	month == 2;
           	daySum ==day +monthAyaer[0];*/
           	if(month == 1)
           	{
           		daySum = day;
           	}
           	if(month > 1)
           	{
           		if((year%4 == 0 && year%100 != 0) || (year%400 == 0))
           		{
           			monthAyear[1] = 29;
           
           		}
           		for(i = 0; i < month-1; i++)
           		{
           			daySum += monthAyear[i];
           		}
           		daySum += day;
           	}
           	printf("這是當年的第%d天。\n",daySum);
           }
           /*重复输入*/
           void repeat()
           {
           	printf("請繼續輸入。\n");
           	writeDate();
           	daySumout(year,month,day);
           	repeat();
           }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值