69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
typedef struct mytime{
int hour;
int minute;
}MT;
void answer1(MT n)
{
char hour[12][12]={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"};
char minute[60][12]={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve",
"thirtheen","fourtheen","fifteen","sixtheen","seventheen","eighteen","nintheen"};
char m[10][12]={"twenty","thirty","fourty","fifty","fifty","sixty"};
printf("it's ");
if(n.hour>12)
{
printf("%s ",hour[n.hour-12-1]);
if(n.minute>=20)
{
if(n.minute%10==0)
printf("%s-%s pm.\n",m[n.minute/10-2]);
else
printf("%s-%s pm.\n",m[n.minute/10-2],minute[n.minute%10-1]);
}
else
printf("%s pm.\n",minute[n.minute]);
}
else
{
printf("%s ",hour[n.hour-1]);
if(n.minute>20)
printf("%s-%s am.\n",m[n.minute/10-2],minute[n.minute%10-1]);
else
printf("%s am.\n",minute[n.minute]);
}
}
void answer2(MT n)
{
char big[12][12]={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"};
char little[12][12]={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"};
int min;
printf("the little hand is on the %s,",little[n.hour%12-1]);
printf("the big hand is almost on the %s\n",big[n.minute/5]);
}
void asktime(void)
{
char time[10]={};
MT now;
puts("现在是几点了?");
sprintf(time,"%s",__TIME__);
puts(time);
sscanf(time,"%d:%d",&now.hour,&now.minute);
answer1(now);
answer2(now);
}
int main(void)
{
asktime();
getchar();
return 0;
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef enum TimeKind
{
None = -1,
Night,
AM,
PM,
MidNoon
}Kind;
//得到时钟对应的英文时间
void GetHourStr(int hour,char *getHour,Kind *timeKind)
{
memset(getHour,0,sizeof(getHour));
switch(hour)
{
case 0: strcpy(getHour," twelve");*timeKind = Night; break;
case 1: strcpy(getHour," one"); *timeKind = AM; break;
case 2: strcpy(getHour," two"); *timeKind = AM; break;
case 3: strcpy(getHour," three");*timeKind = AM; break;
case 4: strcpy(getHour," four");*timeKind = AM; break;
case 5: strcpy(getHour," five");*timeKind = AM; break;
case 6: strcpy(getHour," six");*timeKind = AM; break;
case 7: strcpy(getHour," seven");*timeKind = AM; break;
case 8: strcpy(getHour," eight");*timeKind = AM; break;
case 9: strcpy(getHour," nine");*timeKind = AM; break;
case 10: strcpy(getHour, " ten");*timeKind = AM; break;
case 11: strcpy(getHour," eleven");*timeKind = AM;break;
case 12: strcpy(getHour," twelve");*timeKind = MidNoon;break;
case 13: strcpy(getHour," one");*timeKind = PM; break;
case 14: strcpy(getHour," two");*timeKind = PM; break;
case 15: strcpy(getHour," three");*timeKind = PM;break;
case 16: strcpy(getHour," four");*timeKind = PM; break;
case 17: strcpy(getHour," five");*timeKind = PM; break;
case 18: strcpy(getHour," six");*timeKind = Night; break;
case 19: strcpy(getHour," seven");*timeKind = Night;break;
case 20: strcpy(getHour," eight");*timeKind = Night;break;
case 21: strcpy(getHour," nine");*timeKind = Night; break;
case 22: strcpy(getHour," ten");*timeKind = Night; break;
case 23: strcpy(getHour," eleven");*timeKind = Night;break;
default:
break;
}
return;
}
char * GetTime(const char *strTime )
{
char pHour[3] = {0};
char pMinute[3] = {0};
char hourBuff[16] = {0};
char *DesStr = NULL;
int iHour = 0;
int iMinute = 0;
int tmp = 0;
Kind timeKind = None;
DesStr = (char *)malloc(sizeof(char)*64);
strncpy(pHour,strTime,2); //时钟
strncpy(pMinute,strTime + 3,2);//分钟
iHour = atoi(pHour);
iMinute = atoi(pMinute);
strcpy(DesStr,"it's");
//得到时钟对应的英文时间
GetHourStr(iHour,hourBuff,&timeKind);
strcat(DesStr,hourBuff);
tmp = iMinute%10;
//得到模糊分钟
switch(iMinute/10)
{
case 0: break;
case 1: (tmp>=5)?strcat(DesStr,"-twenty "):strcat(DesStr,"-ten ");break;
case 2: (tmp>=5)?strcat(DesStr,"-thirty "):strcat(DesStr,"-twenty ");break;
case 3: (tmp>=5)?strcat(DesStr,"-forty "):strcat(DesStr,"-thirty ");break;
case 4:(tmp>=5)?strcat(DesStr,"-fifty "):strcat(DesStr,"-forty ");break;
case 5:
if(tmp>5)
{
GetHourStr(iHour + 1,hourBuff,&timeKind);
memset(DesStr,0,sizeof(DesStr));
strcpy(DesStr,"it's");
strcat(DesStr,hourBuff);
break;
}
else
{strcat(DesStr,"-fifty ");break;}
}
switch(timeKind)
{
case Night:strcat(DesStr," at night "); break;
case AM:strcat(DesStr," am "); break;
case PM:strcat(DesStr," pm "); break;
case MidNoon:strcat(DesStr," at Midnoon "); break;
default:
break;
}
return DesStr;
}
int main(int argc, char* argv[])
{
char *p = "20:46:46";
char *pTimeStr = GetTime(p);
printf("%s",pTimeStr);
if(pTimeStr != NULL)
{
free(pTimeStr);
}
getchar();
return 0;
}
//vs2005 运行结果:
//it's eight-fifty at night