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 60 61 62 63 64 65 66 | #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 *myTimeS3( time_t seconds); int *test( void ); int main( void ) { time_t seconds = time (NULL); My_Time *nowPtr = myTimeS3(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); int *ptr = test(); printf ( "%p\n" , ptr); printf ( "%d\n" , *ptr); return 0; } My_Time *myTimeS3( time_t seconds) { struct tm *tmPtr = localtime (&seconds); My_Time mt; My_Time *mtPtr = &mt; mt.year = tmPtr->tm_year + 1900; mt.month = tmPtr->tm_mon + 1; mt.day = tmPtr->tm_mday; mt.hour = tmPtr->tm_hour; if (mt.hour < 12 ) { mt.isam = 1; mt.hourt = tmPtr->tm_hour; } else { mt.isam = 0; mt.hourt = tmPtr->tm_hour - 12; } mt.min = tmPtr->tm_min; mt.sec = tmPtr->tm_sec; return mtPtr; } /* 《程式語言教學誌》的範例程式 檔名:timetest4.c 功能:示範結構的使用 作者:張凱慶 時間:西元2010年7月 */ |
編譯後執行,結果如下

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