C語言小程式 計算第二天日期範例程式碼

來源:互聯網
上載者:User

輸入一個日期,判斷下一個日期是哪一天,如果日期輸入錯誤,則調用Linux下的cal命令顯示輸入的月份 複製代碼 代碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int year, month, day;
const int day_30 = 30;
const int day_31 = 31;
int day_month_2 = 0;
char err_flag = 0;
void compute()
{
 printf("輸入年月日(1992-7-19):");
 scanf("%d-%d-%d",&year, &month, &day);
 if((year%4==0 && year%100!=0) || (year%400==0))
 {
  day_month_2 = 29;
 }
 else
 {
  day_month_2 = 28;
 }
 if(month>=1 && month<=12)
 {
  if(month == 2)
  {
   if(day > day_month_2)
   {
    err_flag = 1;
   }
   else if(day == day_month_2)
   {
    month++;
    day = 1;
   }
   else
   {
    day++;
   }
  }
  else if(month==1 || month==3 || month==5 || month==7 ||
    month==8 || month==10 || month==12)
  {
   if(day > day_31)
   {
    err_flag = 1;
   }
   else if(day == day_31)
   {
    month++;
    day = 1;
   }
   else
   {
    day++;
   }
  }
  else
  {
   if(day > day_30)
   {
    err_flag = 1;
   }
   else if(day == day_30)
   {
    month++;
    day = 1;
   }
   else
   {
    day++;
   }
  }
  if(month > 12)
  {
   year++;
   month = 1;
  }
 }
 else
 {
  err_flag = 1;
 }
}
int main()
{
 time_t now;
 char ch, c;
 char call_cal[30];
 do
 {
  compute();
  if(err_flag)
  {
   err_flag = 0;
   printf("日期輸入錯誤,調用cal命令查詢%d年%d月:nn",year, month);
   sprintf(call_cal, "/usr/bin/cal %d %d",month, year);
   system(call_cal);
   //system("clear");
   while((c=getchar())!='n' && (c=getchar())!=EOF);
   printf("是否重新輸入(y/n):");
  }
  else
  {
   printf("第二天為%d-%d-%d。n",year, month, day);
   while((c=getchar())!='n' && (c=getchar())!=EOF);
   printf("是否再次輸入日期(y/n):");
  }
  ch = getchar();
 }
 while(ch=='y' || ch=='Y' || ch=='n');
 time(&now);
 printf("Now is %s",ctime(&now));
 puts("Having a goodday!");
 return 0;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.