不会中间的转换,急着用,有哪位高手会?有木有?
假定time结构包含三个成员:hour、minute和second(都是int类型)。由用户读入从午夜开始的秒数total_sec,请计算出一个等价的时间结构,等价的时间用小时(0 ~23)、分钟(0 ~59)和秒(0 ~59)表示。Enter total seconds:2590
Time:0:43:10
Enter total seconds:39802
Time:11:3:22
程序代码:#include <stdio.h>
/* 宏定义 */
#define UINT32 unsigned int
#define UINT16 unsigned short
#define UINT8 unsigned char
#define FAILURE 1
#define SUCCESS 0
#define TOTAL_SECOND_PER_MINUTE 60
#define TOTAL_SECOND_PER_HOUR 3600
/* 数据结构定义 */
typedef struct time
{
UINT8 ucHour;
UINT8 ucMinute;
UINT8 ucSecond;
}STRUCT_TIME;
STRUCT_TIME gstrTime;
/* 获取时间秒数 */
UINT32 getTotalSecond(UINT32 *pulTotalSecond)
{
if(NULL == pulTotalSecond)
{
printf("para error !\n");
return FAILURE;
}
printf("Plz input the ulTotalSecond:");
while(1)
{
scanf("%u", pulTotalSecond);
if(*pulTotalSecond < 0)
{
printf("Input error !\n");
}
else
{
break;
}
}
return SUCCESS;
}
/* 转换并输出时间 */
UINT32 getTrueTime(UINT32 ulTotalSecond, STRUCT_TIME *pstrTime)
{
if(NULL == pstrTime)
{
printf("para error !\n");
return FAILURE;
}
pstrTime->ucHour = ulTotalSecond/TOTAL_SECOND_PER_HOUR;
pstrTime->ucMinute = (ulTotalSecond -(pstrTime->ucHour * TOTAL_SECOND_PER_HOUR))/TOTAL_SECOND_PER_MINUTE;
pstrTime->ucSecond = ulTotalSecond%TOTAL_SECOND_PER_MINUTE;
printf("Time: %u:%u:%u \n", pstrTime->ucHour, pstrTime->ucMinute, pstrTime->ucSecond);
return SUCCESS;
}
int main(void)
{
UINT32 ulTmp;
UINT32 ulTotalSecond = 0;
STRUCT_TIME *pstrTime = (STRUCT_TIME *)&gstrTime;
ulTmp = getTotalSecond(&ulTotalSecond);
if(ulTmp)
{
return FAILURE;
}
ulTmp = getTrueTime(ulTotalSecond, pstrTime);
if(ulTmp)
{
return FAILURE;
}
return SUCCESS;
}