0

求大神幫忙小弟抓出程式的問題點(C語言)

#include <stdio.h>
#include <time.h>
int main ()
{
int seconds=time((time_t*)NULL);//讀取現在時間

``````int sec;
int Y,M,D,H,MIN,SEC;
printf("請輸入年分、月份、日期、幾時、幾分、幾秒=");
scanf("%d%d%d%d%d%d",&Y,&M,&D,&H,&MIN,&SEC);

int year;//多少年
int a=0;
int b=0;
for(year=1970;year<Y;year++)
{

if( year%400==0 || ( year%4==0 && year%100!= 0 ) )
{
a=a+1;

}
else
b=b+1;
}
``````

int month2;//月份對應天數
scanf("%d",&M);
switch(M)
{
case 1:
month2=0;
break;
case 2:
month2=31;
break;
case 3:
month2=59;
break;
case 4:
month2=90;
break;
case 5:
month2=120;
break;
case 6:
month2=151;
break;
case 7:
month2=181;
break;
case 8:
month2=212;
break;
case 9:
month2=243;
break;
case 10:
month2=273;
break;
case 11:
month2=304;
break;
case 12:
month2=334;
break;
printf("month2=%d",month2);
}
sec=((a366+b365)(243600))+((month2+D)243600)+(H3600)+(MIN60)+SEC;
sec=sec-seconds;
printf("After %d seconds will shotdown the computer",sec);
return 0;
}

After 115501 seconds will shotdown the computer
(執行當下時間:2019 10 8 22 20 0)

4 個回答

2

iT邦大師 1 級 ‧ 2019-10-08 22:29:58

1. 首先你D的地方多加了一天,
今天是9號,
你加9天就等於多了一天,
你應該要加8天.

2. 你忘了考慮時區的問題,
系統抓的秒數是以UTC時間,
也就是會少8個小時.

3. 你是不是忘了考慮今年是平年還閏年?

ant1017 iT邦新手 4 級 ‧ 2019-10-09 08:38:14 檢舉

24*3600不是86400嗎-.-

system( "shutdown -f -t %d",sec) ;

system(printf(xxx xxx %d, rec));

p.s.

0

iT邦新手 1 級 ‧ 2019-10-09 00:49:51

(大概會用到：localtime、mktime、struct tm 這幾個)

1
dragonH
iT邦超人 6 級 ‧ 2019-10-09 01:28:42

2019-10-8 22:25:00

``````(17897) * 24 * 3600 + (273 + 8) *24 * 3600 + 3600 * 22 + 25 * 60 = 1570659900
``````

2019-10-8 22:20:00

``````(17897) * 24 * 3600 + (273 + 8) *24 * 3600 + 3600 * 22 + 20 * 60 = 1570659600
``````

2019-10-8 22:25:00

``````Number(new Date('2019-10-08 22:25:00')) / 1000 = 1570544700
``````

2019-10-8 22:20:00

``````Number(new Date('2019-10-08 22:20:00')) / 1000 = 1570544400
``````

dragonH iT邦超人 6 級 ‧ 2019-10-09 09:08:28 檢舉

1

iT邦大神 1 級 ‧ 2019-10-09 09:45:28

``````time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\nCurrent local time and date: %s\n", asctime (timeinfo) );
``````