花褪残红青杏小。燕子飞时,绿水人家绕。

C++的时间表达

菜鸟编程 十五楼的鸟儿 22406浏览 0评论
杂志上看到的东东,挺好玩的
题目要求:
英文口语表达时间有如下6种句型:
It is five past seven.(7点5分)
It is eleven to ten. (9点49分)
It is half past nine. (9点30分)
It is a quarter past eight. (8点15分)
It is a quarter to ten. (9点45分)
It is three o’clock. (3点)
请根据输入的时间,输出相应的英文表达。
程序从键盘输入多行数据,每一行为两个非负整数A和B,其中0<=A< 13,0<=B<60,分别表示小时数和分钟数。如果输入的某一行为0 0,则表示输入已结束。程序输出要求:对于输入的每一行(最后的0 0行除外),输出相应的时间表达字符串,要求为上面6种句型之一。输入输出样例见下:

样例输入
8 21
12 45
5 0
0 30
0 0
样例输出
It is twenty-one past eight.
It is a quarter to thirteen.
It is five o’clock.
It is half past zero.

[code=cpp]
#include
#include
using namespace std;
int main()
{
int hour, minute; // 小时数和分钟数
const string STR[] = {"zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen","fourteen",
"a quarter", "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
"twenty-one", "twenty-two", "twenty-three", "twenty-four", "twenty-five",
"twenty-six", "twenty-seven", "twenty-eight", "twenty-nine", "half"};
while (cin >> hour >> minute)
{
if (hour == 0 && minute == 0) // 0 0行,退出
break;
if (minute == 0) // 整点
cout << "It is " << STR[hour] << " o'clock." << endl;
else if (minute <= 30) // 不超过30分
cout << "It is " << STR[minute] << " past " << STR[hour] << "." << endl;
else // 超过30分
cout << "It is " << STR[60 - minute] << " to " << STR[hour + 1] << "." << endl;
}
return 0;
}
// VC++6.0调试通过
[/code]

转载请注明:鸟儿博客 » C++的时间表达

游客
发表我的评论 换个身份
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址