如何在“C”中生成时钟的特定时间编程
例如,以下是程序的示例运行:
Enter the hour: 5
Enter the minute: 23
Enter A (for AM) or P (for PM): A
Enter how many minutes to display: 5
The new time is
5:24 AM
5:25 AM
5:26 AM
5:27 AM
5:28 AM
Enter the hour: 11
Enter the minute: 57
Enter A (for AM) or P (for PM): P
Enter how many minutes to display: 4
The new time is
11:58 PM
11:59 PM
12:00 AM
12:01 AM
Enter the hour: 12
Enter the minute: 55
Enter A (for AM) or P (for PM): P
Enter how many minutes to display: 7
The new time is
12:56 PM
12:57 PM
12:58 PM
12:59 PM
1:00 PM
1:01 PM
1:02 PM
另外,我不允许使用以下语句
: (除非在 switch() 语句中使用) • 继续; • 出口(); 中止(); • 转到
For example, the following are sample runs of the program:
Enter the hour: 5
Enter the minute: 23
Enter A (for AM) or P (for PM): A
Enter how many minutes to display: 5
The new time is
5:24 AM
5:25 AM
5:26 AM
5:27 AM
5:28 AM
Enter the hour: 11
Enter the minute: 57
Enter A (for AM) or P (for PM): P
Enter how many minutes to display: 4
The new time is
11:58 PM
11:59 PM
12:00 AM
12:01 AM
Enter the hour: 12
Enter the minute: 55
Enter A (for AM) or P (for PM): P
Enter how many minutes to display: 7
The new time is
12:56 PM
12:57 PM
12:58 PM
12:59 PM
1:00 PM
1:01 PM
1:02 PM
Also, I'm not allowed to use the folling statements:
• break; (except when used in a switch() statement)
• continue;
• exit();
• abort();
• goto
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将通过回答作为序言,指出使用内置的 C 时间函数对于这个问题来说太过分了,不幸的是,这是一种只能从经验中获得的直觉。
从问题陈述中,您可以推断出您需要一个可以以一分钟粒度表示 24 小时时间的“时间”值。对这个时间值需要的操作是:
现在您必须决定如何在 C 中表示“时间”值。一种选择是使用两个整数(表示小时和分钟)和一个布尔值(表示 AM 或 PM)。当您使用多个值来表示单个逻辑值时,通常将它们包装到
struct
中,因此我们的“时间”类型可能如下所示:(在本例中,我们遵循以下约定:
int
用于存储布尔值)。现在,您必须弄清楚如何使用这种时间表示来实现上述三个操作。通过这种表示,第一个操作将值设置为用户提供的时间变得非常简单:您只需检查提供的小时和分钟值是否在正确的范围内,然后直接存储它们在
struct time_of_day
的hour
和min
成员中。然后,如果用户输入“AM”,您需要将is_pm
值设置为0
;如果用户输入“PM”,则需要将is_pm
值设置为1
。第二个操作输出值也很简单:你可以直接使用
printf()
,如果你知道这些提示:printf
格式说明符%.2d
将打印一个填充到两个位置的整数;is_pm ? "PM" : "AM"
如果is_pm
为 true,则计算结果为"PM
;如果不是,则计算结果为"AM"
第三个操作,向时间值添加分钟,可以这样分解:
部分现在为12
is_pm = !is_pm
会将is_pm
从 0 更改为 1,将 1 更改为 0。I will preface this by answer by stating that using the in-built C time functions is overkill for this problem, which is unfortunately the kind of intuition that you only gain from experience.
From the problem statement, you can deduce that you require a "time" value that can represent 24 hours worth of time, at one minute granularity. The operations required on this time value are:
You now have to decide how you're going to represent the "time" value in C. One option is to use two integers (representing the hour and the minute) and a boolean (representing AM or PM). When you use multiple values to represent a single logical value, it's conventional to wrap those into a
struct
, so our "time" type might look like:(In this case we've followed the convention that an
int
is used to store a boolean value).You now have to figure out how to implement the three operations above using this representation of a time. With this representation, the first operation Set the value to a time provided by the user becomes very easy: you simply need to check that the hour and minute values provided are in the correct range, then store them directly in the
hour
andminute
members of thestruct time_of_day
. You then need to set theis_pm
value to0
if the user entered "AM", or1
if the user entered "PM".The second operation Output the value is also quite simple: you can directly use the
printf()
, if you know these hints:printf
format specifier%.2d
will print an integer padded to two places;is_pm ? "PM" : "AM"
will evaluate to"PM
ifis_pm
is true, and"AM"
if it is not.The third operation, Adding a minute to the time value, can be broken down like this:
An additional hint for this part:
is_pm = !is_pm
will changeis_pm
from 0 to 1 and 1 to 0.我想说最简单的方法是使用
strftime()
,请参阅MSDN 详细信息。您必须手动执行多次输出,但一旦您填写了 tm 结构,这就是一个足够简单的过程。I'd say the simplest method is to use
strftime()
, see MSDN details. You'll have to manually do the multiple time outputs, but it's a simple enough process once you've filled in the tm struct.