各位高手帮帮忙,新手求问 (100分求助)

direren 2009-12-13 07:05:33
关于时间的一个(C/C++)程序:
假设一天的时间是24小时的模式,-hh:mm:ss ,当问““现在是几点了?”产生一个具有人性化的回答。
例子:
00:01:12 => it's twelve at night
13:38:49=>it's one-forty pm

更高的期待:
00:01:12 => it's midnight
13:38:49=>it's about twenty till two in the afternoon (还有二十分钟到下午俩点)
还可以这么回答 “the little hand is on the one, the big hand is almost on the eight" (时针在1那,分针快到8了)


各位高手谢谢了,如果回答的好,我还可以在加分的。





...全文
259 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattycat 2009-12-13
  • 打赏
  • 举报
回复
定义与12小时对应的字符数组"one","two",...,"twelve",然后三个区间属性的字符数组"morning", "afternoon","night". 对分也定义模糊的区间,10,20,30,40,50对应的字符串也差不多了。

然后根据hh-mm取得的值,对应到数组下标,用一个格式化输出的函数就行了,这个函数根据这些数组的下标格式化输出It's about xx(h) xx(m) in the xx(morning,afternoon,evening)。
类似这个样子
a4626458 2009-12-13
  • 打赏
  • 举报
回复
switch 语句啊
晴天1999 2009-12-13
  • 打赏
  • 举报
回复
新手哈!多多指教哟!
direren 2009-12-13
  • 打赏
  • 举报
回复
有没有更简单的方法呢?就是不要把全部的时间和语句都switch case 一遍, 这样就没有意义了。能不能给个具体的例子呢,像是怎么录入之类的
小魔菇 2009-12-13
  • 打赏
  • 举报
回复
switch
case
liuchaotao 2009-12-13
  • 打赏
  • 举报
回复
用模糊数学的方法先把一天分成几个段(早上、上午、中午、下午、晚上、午夜等),并把每个小时也用模糊的方法进行分段。然后根据具体的时间通过查表取得回答的词语,按照格式回答就可以了。
yjqidai 2009-12-13
  • 打赏
  • 举报
回复
PS: 我的程序完全是:汇编脑袋.上面那程序要写24*60条以上的程序.
yjqidai 2009-12-13
  • 打赏
  • 举报
回复
例如时间为 13:38:49

if(时针==?)
{
Switch(60-分钟)
{
case ...;
break;
.
.
default:....;
}
else
.
.
.
if(时针==13)
{
Switch(60-38)
{
case 1:printf("....");
break;
.
.
.
.
case 12:printf("it's about twenty till two in the afternoon ");
.
.
.
default:printf("...");
}
.
.
.


---------------------
先声明,我是个新手.可能有错,望高手指错.谢谢
MasterLuo 2009-12-13
  • 打赏
  • 举报
回复
我觉得应该就是一个把时间转换为数字的过程。当然你要回答的话是你先保存起来的,然后用时间来替换原来的时间,回答方式的不同,就使数字的表达方式不同。
00:01:12 => it's midnight
如果你想选择这种回答的话,那么,it's是不变的,那么你只需要判断时间是哪个时间段,再用相应的单词来替代(预先定制)。
这个还是很简单的,你可以对每一个转换方式写一个函数,还可以随机选机回答方式,我觉得这样做比较好。
daidodo 2009-12-13
  • 打赏
  • 举报
回复
mark
selooloo 2009-12-13
  • 打赏
  • 举报
回复
LZ可以根据需要再添加些answer


#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;
}
hbvanguard 2009-12-13
  • 打赏
  • 举报
回复
分析:
1.不需要考虑秒数,所以忽略秒,只考虑时钟和分钟。
2.以24时计时法 hh(时钟):0--23, mm(分钟):0--59 为取值范围
3.假设18:00---00:00为晚上(night) ,1--11为AM,12为Midnoon,13--17为PM
4.为了输出更精确,规定分钟 >=55 的时候为下一时刻,否则为50分,例08:57 输出为9:00 ,08:52输出为8:50
以下为程序:

#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
direren 2009-12-13
  • 打赏
  • 举报
回复
有人会LISP语言吗?这道题好像是用LISP解得,我就说没那么简单。。。。呜呜。。。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧