❶ c語言 輸入一個時間(年、月、日、時、分、秒),判斷時間是否合法,輸出下一秒的時間
#include<stdio.h>
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
int main()
{
void inputDate(); /*輸入年-月-日 時:分:秒*/
void nextSceond(); /*計算下一秒的時間*/
int leapYear(int year); /*判斷是否為閏年*/
int dayMonth(int month); /*返回每個月份對應的天數*/
inputDate();
leapYear(year);
dayMonth(month);
nextSceond();
system("PAUSE");
return 0;
}
/*函數inputDate()輸入年-月-日 時:分:秒*/
void inputDate()
{
int loop;
for(loop = 0; loop < 3; loop++)
{
printf("請輸入年-月-日 時:分:秒:");
scanf("%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
if(month < 1 || month > 12)
{
printf(" 月份輸入錯誤! ");
continue;
}
else if(day < 1 || day > dayMonth(month))
{
printf(" 日期輸入錯誤! ");
continue;
}
else if(hour < 0 || hour > 23)
{
printf(" 小時輸入錯誤! ");
continue;
}
else if(minute < 0 || minute > 59)
{
printf(" 分鍾輸入錯誤! ");
continue;
}
else if(second < 0 || second > 59)
{
printf(" 秒數輸入錯誤! ");
continue;
}
else
{
break;
}
}
}
/*函數nextSecond()計算下一秒的時間*/
void nextSceond()
{
if(59 == second)
{
minute += 1;
second = 0;
if(60 == minute)
{
hour += 1;
minute = 0;
if(24 == hour)
{
day += 1;
hour = 0;
if(day > dayMonth(month))
{
month += 1;
day = 1;
if(13 == month)
{
year += 1;
month = 1;
}
}
}
}
}
else
{
second += 1;
}
printf("%d-%d-%d %d:%d:%d ",year, month, day, hour, minute, second);
}
/*函數leapYear(int year)判斷是否為閏年*/
int leapYear(int year)
{
if(0 == (year % 4 && 0 != year % 100) || 0 == year % 400)
{
return 1;
}
else
{
return 0;
}
}
/*函數名dayMonth(int month)返回每個月份對應的天數*/
int dayMonth(int month)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if(0 == (year % 4 && 0 != year % 100) || 0 == year %400)
{
return 29;
}
else
{
return 28;
}
case 4:
case 6:
case 9:
case 11:
return 30;
}
}
請放心使用
有問題的話請追問
滿意請及時採納,謝謝
❷ 在C語言中獲取到當前時間,然後與1900年比較,獲取兩個差,在轉換為秒數,這個要怎麼做
#include <stdio.h>
#include <time.h>
void main()
{
time_t lt;
lt = time(NULL);
printf("%ld\n",lt);
}
函數原型:time_t time(time_t * timer)
typedef long time_t;
time函數的原型也可以理解為 long time(long *tloc),即返回一個long型整數。
用法是你先自己定義一個time_t變數,回讓後把變數的地答址傳給它。函數會返回自1970年1月1日0點走過的秒數,同時把這個返回值保存在你傳進來的那個time_t*指向的變數裡面。如果你傳進來NULL(也就是0)的話,就不保存。
❸ c語言怎麼求兩個日期相差的秒數,日期格式20140325150630和20140324150000
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm time_cha;
time_t t1,t2;
long int d;
int year, month, day,hour,min,sec;
scanf("%4d%2d%2d%2d%2d%2d",&year,&month,&day,&hour,&min,&sec);
time_cha.tm_year = year - 1900; //tm結構記錄年為實際-1900
time_cha.tm_mon = month - 1;
time_cha.tm_mday = day;
time_cha.tm_hour = hour;
time_cha.tm_min = min;
time_cha.tm_sec = sec;
t1 = mktime(&time_cha);//獲得從1970年1月1日0時0分0秒以來過去的時間,秒
scanf("%4d%2d%2d%2d%2d%2d",&year,&month,&day,&hour,&min,&sec);
time_cha.tm_year = year - 1900; //tm結構記錄年為實際-1900
time_cha.tm_mon = month - 1;
time_cha.tm_mday = day;
time_cha.tm_hour = hour;
time_cha.tm_min = min;
time_cha.tm_sec = sec;
t2 = mktime(&time_cha);
d=t1-t2;//經過的時間差(秒)
printf("%ld\n", d);
return 0;
}
❹ c語言怎麼將一個年月日轉換成秒數
用mktime()函數。
表頭文件:#include <time.h>
定義函數:time_tmktime(structtm*timeptr);
函數說明:mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0秒算起至今的UTC時間所經過的秒數。
返回值:返回經過的秒數。
(4)c計算年限間秒數擴展閱讀:
C語言參考函數
C語言isgraph()函數:判斷一個字元是否是圖形字元
C語言isdigit()函數:判斷一個字元是否為數字
C語言iscntrl()函數:判斷一個字元是否為控制字元
C語言isalpha()函數:判斷一個字元是否是字母
C語言isalnum()函數:判斷一個字元是否是字母或者數字
C語言pow()函數:求x的y次方的值
C語言frexp()函數:提取浮點數的尾數和指數部分
❺ C語言計算時間函數
標准庫的time.h里有時間函數
time_t time (time_t *timer)
計算從1970年1月1日到當前系統時間,並把結果返回給timer變數,
函數本身返專回的也屬是這個結果.time_t這個類型其實就是一個int.
另有:
double difftime ( time_t timer2, time_t timer1 )
把返回time2和time1所儲存的時間的差.
❻ C語言計算兩個時間之間的時間差,精確到秒。 現在我用的是time_t來計算的,但是我發現這個tim
第一個時間相對於1900-01-01 00:00:00是多少秒
第二個時間相對於1900-01-01 00:00:00是多少秒
再相減就OK
❼ C語言中如何計算時間差
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
unsigned char time1[] = {10, 8, 31, 9, 26 };
unsigned char time2[] = { 10, 8, 31, 9, 50 };
struct tm t1 = {0};
struct tm t2 = {0};
time_t _t1;
time_t _t2;
double diff;
t1.tm_year = time1[0] + 100;
t1.tm_mon = time1[1];
t1.tm_mday = time1[2];
t1.tm_hour = time1[3];
t1.tm_min = time1[4];
t2.tm_year = time2[0] + 100;
t2.tm_mon = time2[1];
t2.tm_mday = time2[2];
t2.tm_hour = time2[3];
t2.tm_min = time2[4];
_t1 = _mkgmtime( &t1 );
_t2 = _mkgmtime( &t2 );
diff = difftime(_t2, _t1 );
printf( "相差 %.0f 分鍾
", diff / 60 );
}
C語言中有兩個相關的函數用來計算時間差,分別是:
time_t time( time_t *t) 與 clock_t clock(void)
頭文件: time.h
計算的時間單位分別為: s , ms
time_t 和 clock_t 是函數庫time.h 中定義的用來保存時間的數據結構
返回值:
1、time : 返回從公元1970年1月1號的UTC時間從0時0分0秒算起到現在所經過的秒數。如果參數 t 非空指針的話,返回的時間會保存在 t 所指向的內存。
2、clock:返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數。 1單元 = 1 ms。
所以我們可以根據具體情況需求,判斷採用哪一個函數。
具體用法如下例子:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t c_start, t_start, c_end, t_end;
c_start = clock(); //!< 單位為ms
t_start = time(NULL); //!< 單位為s
system("pause");
c_end = clock();
t_end = time(NULL);
//!<difftime(time_t, time_t)返回兩個time_t變數間的時間間隔,即時間差
printf("The pause used %f ms by clock() ",difftime(c_end,c_start));
printf("The pause used %f s by time() ",difftime(t_end,t_start));
system("pause");
return 0;
}
因此,要計算某一函數塊的佔用時間時,只需要在執行該函數塊之前和執行完該函數塊之後調用同一個時間計算函數。再調用函數difftime()計算兩者的差,即可得到耗費時間。
❽ C語言 統計秒數
題目意思不清晰,沒沒明白
❾ C語言計算時間
在C語言中計算時間,可以使用標准庫中的計時函數——clock()。
函數原型:
clock_tclock(void);
其中clock_t是用來保存時間的數據類型,在time.h文件中,可以找到對它的定義:
#ifndef_CLOCK_T_DEFINED
typedeflongclock_t;
#define_CLOCK_T_DEFINED
#endif
很明顯,clock_t是一個長整形數。在time.h文件中,還定義了一個常量CLOCKS_PER_SEC,它用來表示一秒鍾會有多少個時鍾計時單元,其定義如下:
#defineCLOCKS_PER_SEC((clock_t)1000)
可以看到每過千分之一秒(1毫秒),調用clock()函數返回的值就加1。下面舉個例子,可以使用公式clock()/CLOCKS_PER_SEC來計算一個進程自身的運行時間:
voidelapsed_time()
{
printf("Elapsedtime:%usecs. ",clock()/CLOCKS_PER_SEC);
}
當然,也可以用clock函數來計算的機器運行一個循環或者處理其它事件到底花了多少時間:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain(void)
{
longi=10000000L;
clock_tstart,finish;
doubleration;
printf("Timetodo%ldemptyloopsis",i);
start=clock();
while(i--);
finish=clock();
ration=(double)(finish-start)/CLOCKS_PER_SEC;
printf("%fseconds ",ration);
system("pause");
}