如何在“C”中生成时钟的特定时间编程

发布于 2024-11-14 14:38:19 字数 671 浏览 4 评论 0原文

例如,以下是程序的示例运行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-11-21 14:38:20

我将通过回答作为序言,指出使用内置的 C 时间函数对于这个问题来说太过分了,不幸的是,这是一种只能从经验中获得的直觉。


从问题陈述中,您可以推断出您需要一个可以以一分钟粒度表示 24 小时时间的“时间”值。对这个时间值需要的操作是:

  • 将值设置为用户提供的时间,如“小时”值、“分钟”值和“AM/PM”值;
  • 以“11:58 PM”格式输出值;
  • 在时间值上添加一分钟。

现在您必须决定如何在 C 中表示“时间”值。一种选择是使用两个整数(表示小时和分钟)和一个布尔值(表示 AM 或 PM)。当您使用多个值来表示单个逻辑值时,通常将它们包装到 struct 中,因此我们的“时间”类型可能如下所示:(

struct time_of_day {
    int hour; /* From 1 to 12 */
    int minute; /* From 0 to 60 */
    int is_pm; /* 0 or 1 */
};

在本例中,我们遵循以下约定: int 用于存储布尔值)。

现在,您必须弄清楚如何使用这种时间表示来实现上述三个操作。通过这种表示,第一个操作将值设置为用户提供的时间变得非常简单:您只需检查提供的小时和分钟值是否在正确的范围内,然后直接存储它们在 struct time_of_dayhourmin 成员中。然后,如果用户输入“AM”,您需要将 is_pm 值设置为 0;如果用户输入“PM”,则需要将 is_pm 值设置为 1

第二个操作输出值也很简单:你可以直接使用printf(),如果你知道这些提示:

  • printf格式说明符 %.2d 将打印一个填充到两个位置的整数;
  • 表达式 is_pm ? "PM" : "AM" 如果 is_pm 为 true,则计算结果为 "PM;如果不是,则计算结果为 "AM" 第三个操作,

向时间值添加分钟,可以这样分解:

  • 向时间的分钟部分添加 1;
  • 如果分钟部分现在小于 60,则停止
  • ;分钟部分为 0,小时部分加 1时间;
  • 如果小时部分现在小于 12,则停止;
  • 更改为 AM 并停止。
  • ,则从 AM 更改为 PM 或将小时部分

部分现在为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:

  • Set the value to a time provided by the user, as an "hour" value, a "minute" value, and an "AM/PM" value;
  • Output the value in the format "11:58 PM";
  • Add a minute to the time value.

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:

struct time_of_day {
    int hour; /* From 1 to 12 */
    int minute; /* From 0 to 60 */
    int is_pm; /* 0 or 1 */
};

(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 and minute members of the struct time_of_day. You then need to set the is_pm value to 0 if the user entered "AM", or 1 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:

  • The printf format specifier %.2d will print an integer padded to two places;
  • The expression is_pm ? "PM" : "AM" will evaluate to "PM if is_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:

  • Add 1 to the minute part of the time;
  • If the minute part is now less than 60, stop;
  • Set the minute part to 0 and add 1 to the hour part of the time;
  • If the hour part is now less than 12, stop;
  • If the hour part is now 12, change from AM to PM or PM to AM and stop;
  • Set the hour part to 1.

An additional hint for this part:

  • The expression is_pm = !is_pm will change is_pm from 0 to 1 and 1 to 0.
羁〃客ぐ 2024-11-21 14:38:20

我想说最简单的方法是使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文