用 C 语言构建日历程序的开始步骤的建议 [英] Suggestions for beginning steps to build a calendar program in C

查看:8
本文介绍了用 C 语言构建日历程序的开始步骤的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做我学校的一个实践项目,即在 linux 或 Mac 命令行中构建一个类似于日历的 C 程序.该程序不必实现日历所具有的所有方法,但它应该为这些参数产生相同的输出

I am working on a practice project of my school, which is to build a C program that resembles a calendar in linux or Mac command lines. The program doesn't have to implement all the methods that a calendar has, but it should produce the same output for these arguments

$ cal -m 11 2010
$ cal -m 13 2011
$ cal -m mar 2012
$ cal -m maRc 2013
$ cal -m MARCH 2014
$ cal -m MARCHY 2015
$ cal -m 5
$ cal 18
$ cal 2018
$ cal

我将如何处理这个问题?对开始步骤有什么建议吗?

How would I approach this? Is there any suggestion for beginning steps?

非常感谢任何帮助.

推荐答案

这是一个很好的挑战,如果你从第一原则着手处理它会非常困难.您要求提供建议,所以我很乐意为您提供一些建议.

This is a good challenge, and quite difficult if you approach it from first principles. You asked for suggestions, so I'll be happy to give you a few.

首先,编写代码来显示日历.您可能希望它看起来像这样:

First, write code to display a calendar at all. You probably want it to look something like this:

Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

对于您的第一次实施,假设每月的第一天是星期日.对于您的第一个实现,要么将月份的长度固定为 30 天,要么将其作为命令行参数.确保代码在 28、29、30 或 31 天的月份内有效.

For your first implementation, assume that the first of the month is a Sunday. For your first implementation, either have the length of the month be hardwired at 30 days, or have it be a command-line argument. Make sure the code works for months of length 28, 29, 30, or 31 days.

第一个子问题显然只涉及一些精心挑选的循环和 printf 调用.但这会让你忙一阵子,尤其是如果你是初学者.

This first subproblem obviously just involves some well-chosen loops and printf calls. But this will keep you busy for a while, especially if you're a beginner.

接下来,弄清楚如何让它在每个月的第一天不是星期天的月份都能正常工作.你可能想让它看起来像这样:

Next, figure out how to make it work for months where the first day of the month isn't Sunday. You'll probably want to make it look something like this:

Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

同样,这显然只涉及循环和 printf 调用.再次(现在),硬连线或从命令行传入第一天指示.确保它适用于所有七种可能性.根据您编写第一个程序的方式(假设为周日),您可能能够进行一些增量修改,但如果没有干净的方法来添加这种可变性,请不要害怕将所有东西都扔掉,重新开始.

Again, this obviously just involves loops and printf calls. Again (for now), have the first-day indication hardwired, or passed in from the command line. Make sure it works for all seven possibilities. Depending on how you wrote your first program (the one that assumed Sunday), you might be able to make a few incremental modifications, but if there's no clean way to add this variability, don't be afraid to throw just about everything away and start over.

最后,我们来到了最难的部分.(但是,如果您认为前面的工作非常困难,请不要太担心——下一步的难度不同.)显然,要制作一个真正的日历程序,我们将必须停止硬连线,并开始计算实际月份的实际长度,以及它开始的星期几.

Finally, we come to the hard part. (But don't worry too much if you thought the preceding work was plenty hard -- this next step is hard in a different way.) Obviously to make a real calendar program, we're going to have to stop hardwiring things, and start computing the actual length of an actual month, and the day of the week on which it starts.

没有函数(至少在 C 和 Unix 中)可以直接告诉您给定月份有多少天.没有任何函数可以直接告诉您每月第一天是星期几.

There's no function (in C and Unix, at least) that directly tells you how many days there are in a given month. There's no function that directly tells you the day of the week for the first of the month.

硬连线一个包含每个月天数的 12 元素数组很简单,尽管您显然必须为闰年的二月做一些特别的事情.(请参阅此处了解准确计算哪些年份是闰年的技巧.)但是如何弄清楚一个月从一周中的哪一天开始?这与如何根据日期找到星期几?"的问题相同,其中日期是 1 月 1 日或 2 月 1 日,或者您正在处理的任何月份.我认为解决这个问题有两种通用方法:

It's straightforward to hardwire a 12-element array containing the number of days in each month, although you obviously have to do something special for February in leap years. (See here for tips on accurately computing which years are leap years.) But how to figure out which day of the week a month starts on? This is the same problem as "How can I find the day of the week given the date?", where the date is January 1, or February 1, or whatever month it is you're working on. I think there are two general approaches for solving this problem:

  1. 从首要原则出发.我解决这个问题的首选算法是一个叫做Zeller's congruence"的老栗子,尽管也有一些替代栗子.旧的 C FAQ list问题 20.31.

让机器做脏活.要查找给定日期的星期几,请在 tm_yeartm_mon 中构造一个包含所需日期的 struct tmtm_mday 字段.(记住tm_mon是从0开始的,tm_year是从1900开始的.)填写tm_hourtm_min, 和 tm_sec 与 12:00:00.用 0 或 -1 填写 tm_isdst.调用 mktime.在输出时,tm_wday 将包含该日期的星期几.

Let the machine do the dirty work. To find the day of the week given the date, construct a struct tm containing the desired date in the tm_year, tm_mon, and tm_mday fields. (Remember that tm_mon is 0-based, and tm_year is 1900-based.) Fill in tm_hour, tm_min, and tm_sec with 12:00:00. Fill in tm_isdst with 0 or -1. Call mktime. On output, tm_wday will contain the day of the week for that date.

最后,如果您已经走到这一步,您可以担心命令行输入格式.如果是我,我会满足于解析

Lastly, if you've gotten this far, you can worry about the command line input format. If it was me, I'd settle for just parsing

cal 7 2018

以整数形式接受月份和年份.如果你想接受你提到的所有其他变体,那将是一些有点乏味、繁琐的工作.有很多方法可以做到这一点;我会让你发现一些适合你的技术.

to accept the month and year number as integers. If you want to accept all the other variants you mentioned, that's going to be a certain amount of somewhat tedious, fussy work. There are lots of ways to do it; I'll let you discover some techniques that work for you.

最后,作为一个额外的挑战(这是一个真正的挑战!),正如 Bathsheba 在评论中建议的那样,尝试让你的程序做与标准 Unix/Linux 相同的事情 cal 命令会在你调用它时执行

Finally, as an extra-credit challenge (and this is a real challenge!), as Bathsheba suggested in a comment, try making your program do the same thing as the standard Unix/Linux cal command does if you invoke it as

cal 9 1752

这篇关于用 C 语言构建日历程序的开始步骤的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆