【C】“HH:MM:SS”格式的时间的计算
这个和那个VB的差不多。这是个控制台程序,用法如下。D:\C_Proj\TimeCalc\Release>TimeCalcUSAGE:
TimeCalc <HH:MM:SS> <operator> <HH:MM:SS>
Operator can be + - * / % & | ^ << >>BIN下载:
SRC下载:
代码在此。VC6编译通过。//=============================================================================
//TimeCalc:
//进行HH:MM:SS格式的时间的加减乘除等运算。
//作者:0xAA55
//论坛:http://www.0xaa55.com/
//版权所有(C) 2013-2014 技术宅的结界
//请保留原作者信息,否则视为侵权
//-----------------------------------------------------------------------------
#include<stdio.h>
#include<time.h>
#define TC_SecondsPerMinute 60
#define TC_MinutesPerHour 60
#define TC_HoursPerDay 24
#define TC_SecondsPerHour (TC_SecondsPerMinute*TC_MinutesPerHour)
#define TC_SecondsPerDay (TC_SecondsPerMinute*TC_MinutesPerHour*TC_HoursPerDay)
//=============================================================================
//TimeToSeconds:
//将H:M:S表示时间转换为秒
//-----------------------------------------------------------------------------
#define TimeToSeconds(h,m,s) s+m*TC_SecondsPerMinute+h*TC_SecondsPerHour
//=============================================================================
//SecondsToTime:
//将秒转换为H:M:S表示时间
//-----------------------------------------------------------------------------
void SecondsToTime(time_t Time,unsigned*puHours,unsigned*puMinutes,unsigned*puSeconds)
{
*puHours=(Time / TC_SecondsPerHour),
*puMinutes=(Time / TC_SecondsPerMinute) % TC_MinutesPerHour,
*puSeconds=(Time % TC_SecondsPerMinute);
}
//=============================================================================
//ParseTimeString:
//从H:M:S格式的字符串提取H、M、S的值
//返回解析的数值个数
//-----------------------------------------------------------------------------
#define ParseTimeString(str,ph,pm,ps) \
sscanf(str,"%u:%u:%u",ph,pm,ps)
void Usage()
{
fputs(
"USAGE:\n"
"TimeCalc <HH:MM:SS> <operator> <HH:MM:SS>\n"
"Operator can be + - * / % & | ^ << >>\n",stderr);
}
int main(int argc,char**argv)
{
time_t
tTime1,
tTime2,
tTimeResult;
unsigned
uHours1,
uMinutes1,
uSeconds1;
unsigned
uHours2,
uMinutes2,
uSeconds2;
unsigned
uHoursResult,
uMinutesResult,
uSecondsResult;
if(argc<4)
{
Usage();
return 1;
}
//分析输入的字符串
if(ParseTimeString(argv,&uHours1,&uMinutes1,&uSeconds1)!=3)
{
Usage();
return 1;
}
if(ParseTimeString(argv,&uHours2,&uMinutes2,&uSeconds2)!=3)
{
Usage();
return 1;
}
//换算成秒
tTime1=TimeToSeconds(uHours1,uMinutes1,uSeconds1);
tTime2=TimeToSeconds(uHours2,uMinutes2,uSeconds2);
//判断运算符
if(!strcmp(argv,"+"))
{
tTimeResult=tTime1+tTime2;
}else
if(!strcmp(argv,"-"))
{
tTimeResult=tTime1-tTime2;
}else
if(!strcmp(argv,"*"))
{
tTimeResult=tTime1*tTime2;
}else
if(!strcmp(argv,"/"))
{
tTimeResult=tTime1/tTime2;
}else
if(!strcmp(argv,"%"))
{
tTimeResult=tTime1%tTime2;
}else
if(!strcmp(argv,"&"))
{
tTimeResult=tTime1&tTime2;
}else
if(!strcmp(argv,"|"))
{
tTimeResult=tTime1|tTime2;
}else
if(!strcmp(argv,"^"))
{
tTimeResult=tTime1^tTime2;
}else
if(!strcmp(argv,"<<"))
{
tTimeResult=tTime1<<tTime2;
}else
if(!strcmp(argv,">>"))
{
tTimeResult=tTime1>>tTime2;
}
//转换成H:M:S的格式
SecondsToTime(tTimeResult,&uHoursResult,&uMinutesResult,&uSecondsResult);
//打印
printf("%u:%u:%u\n",uHoursResult,uMinutesResult,uSecondsResult);
//结束
return 0;
}
页:
[1]