有关开始使用C构建日历程序的建议 [英] Suggestions for beginning steps to build a calendar program in C

查看:61
本文介绍了有关开始使用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?

我将非常感谢您的帮助.

I would really appreciate any help.

推荐答案

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

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.

最后,我们进入困难的部分.(但是,如果您认为前面的工作很辛苦,请不要担心太多-下一步很难以其他方式完成.)显然,要制作一个 real 日历程序,我们将必须停止繁琐的工作,并开始计算实际月份的实际长度以及该月份的起始日期.

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个元素的数组(包含每个月的天数)很简单,尽管您显然必须在years年中对2月进行一些特殊的操作.(有关准确计算哪些年份是leap年的提示,请参见此处.)弄清楚一个月的星期几开始?这与如何在给定日期的情况下找到星期几?"是相同的问题,其中日期是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常见问题列表让机器进行肮脏的工作.要查找给定日期的星期几,请构造一个 struct tm ,在 tm_year tm_mon 中包含所需的日期tm_mday 字段.(请记住, tm_mon 是基于0的,而 tm_year 是基于1900的.)填写 tm_hour tm_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相同的操作<如果您以以下方式调用code> 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天全站免登陆