c語言算兩個日期相差多少天,兩個日期之間差多少天C語言

本文目錄一覽:

c語言如何計算兩個時間相差多少

/**

time.c

定義一個結構體實現兩個時間的加減

*/

#includestdio.h

#includestring.h

typedef struct

{

int seconds;

int minutes;

int hours;

}Time;

int checkTime(Time time);

void printTime(Time time);

void swap(Time *time1,Time *time2);//大的時間放在前面

Time subtract1(Time *first,Time *second);

Time subtract(Time *first,Time *second);//默認第一個時間比第二個大

int main()

{

Time time1;

Time time2;

Time time3;

char againch[5]=”y”;

while(strcmp(againch,”y”)==0||strcmp(againch,”Y”)==0)

{

int again=1;

while(again)

{

printf(“輸入時間1:”);

scanf(“%d:%d:%d”,time1.hours,time1.minutes,time1.seconds);

if(checkTime(time1))

{

printf(“—–輸入時間格式錯誤!請重新輸入\n”);

again=1;

}

else

again=0;

}

again=1;

while(again)

{

printf(“輸入時間2:”);

scanf(“%d:%d:%d”,time2.hours,time2.minutes,time2.seconds);

if(checkTime(time2))

{

printf(“—–輸入時間格式錯誤!請重新輸入\n”);

again=1;

}

else

again=0;

}

swap(time1,time2);

printf(”           “);

printTime(time1);

printf(” – “);

printTime(time2);

time3=subtract(time1,time2);

printf(” = “);

printTime(time3);

printf(“\n”);

printf(“繼續[y/n]?:”);

scanf(“%s”,againch);

}

return 0;

}

//檢查時間的格式

int checkTime(Time time)

{

//    printf(“小時格式錯誤:%d\n”,(time.hours=24||time.hours0));

//    printf(“分鐘格式錯誤:%d\n”,(time.minutes=60||time.minutes0));

//    printf(“秒格式錯誤  :%d\n”,(time.seconds=60||time.minutes0));

return ((time.hours24||time.hours0)||(time.minutes=60||time.minutes0)||(time.seconds=60||time.minutes0));

}

//輸出按個數輸出時間

void printTime(Time time)

{

printf(“%d:%d:%d”,time.hours,time.minutes,time.seconds);

}

//大的時間放到第一個變數,小的時間方法哦第二個變數

void swap(Time *time1,Time *time2)

{

//保證第一個時間永遠大於第二個時間

if(time2-hourstime1-hours)//如果有time

{

//交換兩個時間的小時

time2-hours^=time1-hours;

time1-hours^=time2-hours;

time2-hours^=time1-hours;

//交換兩個時間的分鐘:

time1-minutes^=time2-minutes;

time2-minutes^=time1-minutes;

time1-minutes^=time2-minutes;

//交換兩個時間的秒:

time1-seconds^=time2-seconds;

time2-seconds^=time1-seconds;

time1-seconds^=time2-seconds;

}

else if(time2-minutestime1-minutestime1-hours==time2-hours)

{

//交換兩個時間的分鐘:

time1-minutes^=time2-minutes;

time2-minutes^=time1-minutes;

time1-minutes^=time2-minutes;

//交換兩個時間的秒:

time1-seconds^=time2-seconds;

time2-seconds^=time1-seconds;

time1-seconds^=time2-seconds;

}

else if(time2-secondstime1-secondstime1-minutes==time2-minutes)

{

//交換兩個時間的秒:

time1-seconds^=time2-seconds;

time2-seconds^=time1-seconds;

time1-seconds^=time2-seconds;

}

}

//計算兩個時間的差

Time subtract(Time *first,Time *second)//默認第一個時間比第二個大

{

Time result;

//先對秒進行相減

if(first-seconds=second-seconds)//如果第一個秒大於或者等於

{

result.seconds=first-seconds-second-seconds;

}

else//如果第一個的秒數小的話

{

first-minutes=first-minutes-1;//借位

first-seconds=first-seconds+60;

result.seconds=first-seconds-second-seconds;

}

//接著對分鐘相減

if(first-minutes=second-minutes)//如果第一個秒大於或者等於

{

result.minutes=first-minutes-second-minutes;

}

else//如果第一個的秒數小的話

{

first-hours=first-hours-1;//借位

first-minutes=first-minutes+60;

result.minutes=first-minutes-second-minutes;

}

//交換後 默認第一個小時會大於第一個,沒有借位的情況,不用

result.hours=first-hours-second-hours;

return result;

拓展資料

C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。

儘管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標準規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。

二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標準局為C語言制定了一套完整的美國國家標準語法,稱為ANSI C,作為C語言最初的標準。目前2011年12月8日,國際標準化組織(ISO)和國際電工委員會(IEC)發布的C11標準是C語言的第三個官方標準,也是C語言的最新標準,該標準更好的支持了漢字函數名和漢字標識符,一定程度上實現了漢字編程。

c語言兩日期相差天數

#include stdio.h

#includemath.h

int main()

{

int s=0,m1,d1,y1,y2,m2,d2,i,a[12]={31,28,31,30,31,30,31,31,30,31,30,31},b[12]={31,29,31,30,31,30,31,31,30,31,30,31};

scanf(“%d %d %d”,y1,m1,d1);

scanf(“%d %d %d”,y2,m2,d2);

if(y1==y2)

{

if((y1%100!=0y1%4==0)||y1%400==0)

{

while(m1m2)

{s=b[m1-1]+s;

m1=m1+1;

}

}

else

{

while(m1m2)

{s=a[m1-1]+s;

m1=m1+1;

}

}

printf(“%d days\n”,s+d2-d1);

}

else

{

while(y1y2)

{

if(m1=2)

{

if((y1%100!=0y1%4==0)||y1%400==0)

{s=366+s;}

else

{s=365+s;}

y1=y1+1;

}

else

{

y1=y1+1;

if((y1%100!=0y1%4==0)||y1%400==0)

{s=366+s;}

else

{s=365+s;}

}

}

if(m1=m2)

{

if((y2%100!=0y2%4==0)||y2%400==0)

{for(i=0;m1m2;i++)

{s=b[m1-1]+s;

m1=m1+1;

}

}

else

{for(i=0;m1m2;i++)

{s=a[m1-1]+s;

m1=m1+1;

}

}

printf(“%d days\n”,s+d2-d1);

}

else

{

if((y2%100!=0y2%4==0)||y2%400==0)

{

while(m1m2)

{s=b[m1-1]+s;

m1=m1-1;

}

}

else

{

while(m1m2)

{s=a[m1-1]+s;

m1=m1-1;

}

}

printf(“%d days\n”,s+d2-d1);

}

}

return 0;

}

你試試看!

C語言求兩個日期相差的天數

計算兩個年月日之間的天數,思路是分別算出日期的總天數然後相減。

要考慮閏年的情況,判斷閏年的口訣:4年一閏,100年不閏,400年再閏。

((year % 4 == 0 year % 100 != 0) || year % 400 == 0)

網上找了一個(偷懶= =!),修改下如下:

#include stdio.h

int sum(int y,int m,int d)

{

unsigned char x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int i,s=0;

for(i=1;iy;i++)

if(i%4==0 i%100!=0 || i%400==0)

s+=366;//閏年

else

s+=365;//平年

if(y%4==0 y%100!=0 || y%400==0)

x[2]=29;

for(i=1;im;i++)

s+=x[i];//整月的天數

s+=d;//日的天數

return s;//返回總天數,相對公元1年

}

void main()

{

unsigned char y1,m1,d1,y2,m2,d2;

int s1,s2;

printf(“輸入第一個年 月 日:”);

scanf(“%d %d %d”,y1,m1,d1);

printf(“輸入第二個年 月 日:”);

scanf(“%d %d %d”,y2,m2,d2);

s1=sum(y1,m1,d1);

s2=sum(y2,m2,d2);

if (s1 s2)

printf(“相差天數:%ld\n”,s1-s2);

else

printf(“相差天數:%ld\n”,s2-s1);

}

以上代碼VC6編譯測試通過。

雖然這個思路顯得有些笨,但是其它演算法,代碼太長太複雜,要考慮多種情況,不如直接算兩個日期距離公元元年1月1日的天數,然後相減

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/286307.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-23 03:47
下一篇 2024-12-23 03:47

相關推薦

  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 利用Python實現兩個鏈表合併為一個有序鏈表

    對於開發工程師來說,實現兩個鏈表合併為一個有序鏈表是必須掌握的技能之一。Python語言在鏈表處理上非常便利,本文將從多個方面詳細闡述如何利用Python實現兩個鏈表合併為一個有序…

    編程 2025-04-29
  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python定義兩個列表的多面探索

    Python是一種強大的編程語言,開放源代碼,易於學習和使用。通過Python語言,我們可以定義各種數據類型,如列表(list)。在Python中,列表(list)在處理數據方面起…

    編程 2025-04-29
  • Python獲取當前日期的多種方法

    本文介紹如何使用Python獲取當前日期,並提供了多種方法,包括使用datetime模塊、time模塊以及第三方庫dateutil等。讓我們一步一步來看。 一、使用datetime…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28

發表回復

登錄後才能評論