1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include <stdio.h> #include <stdlib.h> #include <time.h> struct my_tm { int year; // 現在年份 int month; // 月份 int day; // 日期 int hour; // 二十四小時制的時 int isam; // 上午為非 0 值,下午為 0 int hourt; // 十二小時制的時 int min; // 分 int sec; // 秒 }; typedef struct my_tm My_Time; My_Time *myTimeS4( time_t seconds); int main( void ) { time_t seconds = time (NULL); My_Time *nowPtr = myTimeS4(seconds); printf ( "\n\n現在時間是西元 %d 年 %d 月 %d 日" , nowPtr->year, nowPtr->month, nowPtr->day); printf ( "%s %d 點 %d 分 %d 秒\n\n" , nowPtr->isam ? "上午" : "下午" , nowPtr->hourt, nowPtr->min, nowPtr->sec); return 0; } My_Time *myTimeS4( time_t seconds) { struct tm *tmPtr = localtime (&seconds); My_Time *mtPtr = malloc ( sizeof (My_Time)); mtPtr->year = tmPtr->tm_year + 1900; mtPtr->month = tmPtr->tm_mon + 1; mtPtr->day = tmPtr->tm_mday; mtPtr->hour = tmPtr->tm_hour; if (mtPtr->hour < 12 ) { mtPtr->isam = 1; mtPtr->hourt = tmPtr->tm_hour; } else { mtPtr->isam = 0; mtPtr->hourt = tmPtr->tm_hour - 12; } mtPtr->min = tmPtr->tm_min; mtPtr->sec = tmPtr->tm_sec; return mtPtr; } /* 《程式語言教學誌》的範例程式 檔名:timetest5.c 功能:示範結構的使用 作者:張凱慶 時間:西元2010年7月 */ |
編譯後執行,結果如下

程式說明,請繼續參考
沒有留言:
張貼留言