C++课程:基督教节日计算器
基本上,我正在尝试完成这个考试式的问题以进行考试练习。我知道如何做所有事情,除了最后一个问题,即与课程有关的问题。我有点明白它们,但不知道如何将其应用到问题中。有人可以给我一些关于如何创建 C++ 类的起点或技巧(甚至可能的答案)吗?非常感谢任何帮助。问题如下。对于时间有多长表示歉意。
1800 年,数学家 Carl Friedrich Gauss 提出了一种计算 新公历中复活节假期的日期(大英帝国于 1752 年)。为此,需要评估许多表达式: 两项 M 和 N,对于给定年份 y 保存以下值:
year M N
1700-1799 23 3
1800-1899 23 4
1900-2099 24 5
2100-2199 24 6
The expressions are:
a = y mod 19
b = y mod 4
c = y mod 7
d = (19a + M) mod 30
e = (2b + 4c + 6d + N) mod 7
如果 22+d+e 小于 32,则该总和是 3 月的复活节假期。 否则 d+e-9 是四月的复活节,除非: - 结果是 4 月 26 日,这种情况下复活节是 4 月 19 日 - 结果是 4 月 25 日,在这种情况下,如果 d 为 28,则复活节为 4 月 18 日 a 大于 10
a) 实现一个以年份作为参数的 C/C++ 函数 打印出复活节假期的日期。 (10 分) 其他基督教节日可以从复活节之日开始计算: 耶稣受难日是复活节周日之前的星期五。 棕枝主日是复活节前的星期日。 圣灵降临节周日是复活节周日后的第 7 周。 耶稣升天节是圣灵降临节前 10 天。 注:四月和六月长 30 天,三月和五月长 31 天。
b) 设计/描述用于识别基督徒日期的算法 假期“耶稣受难节”、“棕枝主日”、“耶稣升天”和“圣灵降临节” Sunday”。(此答案不需要源代码)(15分)
高斯描述的复活节计算算法中的M和N项也可以 使用以下表达式进行计算:
k = floor(y/100)
p = floor((13 + 8k)/25)
q = floor(k/4)
M = (15 − p + k − q) mod 30
N = (4 + k − q) mod 7
下限函数向下舍入一个值 - 在数学库 math.h 中,这是 可用作带有原型 double Floor(double) 的 C 函数;
c) 生成一个 C++ 类来实现基督教节日计算器。它 应该有私有属性(成员变量)代表 日期的日、月、年以及私有方法 计算项 M 和 N。默认构造函数应设置 所有属性都设置为 0。应该可以使用 a 来更改年份 set() 方法并使用 get() 方法检索日期和月份。 该类还应该包含不同的计算方法 假期 – 示例:computeEaster()。
Basically, I'm trying to complete this exam-style question for exam practice. I know how to do everything but the last question, to do with classes. I kinda get them, but don't know how to apply it to the question. Could anybody give me some starting points or tips (or even possible answers) to how I would go about creating a C++ class? Any help is much appreciated. The question is below. Apologies about how long it is.
In 1800 the mathematician Carl Friedrich Gauss presented an algorithm for calculating the
date of the Easter holiday in the new Gregorian calendar (introduced in the British Empire in
the year 1752). For this a number of expressions need to be evaluated:
Two terms M and N, which for a given year y hold the values:
year M N
1700-1799 23 3
1800-1899 23 4
1900-2099 24 5
2100-2199 24 6
The expressions are:
a = y mod 19
b = y mod 4
c = y mod 7
d = (19a + M) mod 30
e = (2b + 4c + 6d + N) mod 7
If 22+d+e is smaller than 32 than this sum is the day of the Easter holiday in March.
Otherwise d+e-9 is the day of Easter in April unless:
- the result is the 26th April, in which case Easter is on the 19th April instead
- the result is the 25th April, in which case Easter is on the 18th of April if d is 28 and
a is greater than 10
a) Implement a C/C++ function that given a year as parameter will
print out the date for the Easter holiday. (10 marks)
Other Christian holidays can be calculated from the date of Easter:
Good Friday is the Friday before Easter Sunday.
Palm Sunday is the Sunday before Easter Sunday.
Whit Sunday is 7 weeks after Easter Sunday.
Ascension day is 10 days before Whit Sunday.
Note: April and June are 30 days long, whereas March and May are 31 days long.
b) Design/describe algorithms for identifying the date for the Christian
holidays "Good Friday", "Palm Sunday", "Ascension" and "Whit
Sunday". (no source code is required for this answer) (15 Marks)
The terms M and N in the Easter calculation algorithm described by Gauss can also
be calculated using the following expressions:
k = floor(y/100)
p = floor((13 + 8k)/25)
q = floor(k/4)
M = (15 − p + k − q) mod 30
N = (4 + k − q) mod 7
The floor function rounds down a value – in the maths library math.h this is
available as a C function with the prototype double floor(double);
c) Produce a C++ class to implement a Christian Holiday calculator. It
should have private attributes (member variables) representing the
day, month and year of the date and a private method for
calculating the terms M and N. The default constructor should set
all attributes to 0. It should be possible to change the year using a
set() method and to retrieve day and month using get() methods.
The class should also contain compute methods for the different
holidays – example: computeEaster().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个类很简单:
好了!你现在有一个 foobar 类。该类可以是您想要的任何类。根据需要填写公共和私人部分。
至于如何使用类来解决给定问题,以下是大致按顺序排列的一般步骤:
最后一点,正如大卫的评论已经指出的那样,将原始问题逐字复制并粘贴到您的问题中是一个坏主意。人们(基本上)不太愿意通读它。如果您准确地澄清了自己遇到的问题并删除了所有不相关的部分,则可以增加获得答复的可能性。
Creating a class is simple:
There! you now have a class foobar. That class can be whatever you want it to be. Fill in the public and private sections as appropriate.
As for how to use classes on how to solve a given problem, here are the general steps in roughly sequential order:
One last note, as David's comment already pointed out, copying and pasting the original problem verbatim into your question is a bad idea. People will be (substantially) less inclined to read it through. You can increase the likelihood for a response if you clarified exactly what trouble you're having and cutting out all the irrelevant parts.
所以,你只能坚持创建一个类。要么您根本不知道如何创建一个类,要么您不知道如何创建这个特定的类。
要创建一个类,请使用
class
关键字:class { int i; };
。如果您尚未在 C++ 中创建任何课程,那么您可能需要重修本课程或其他 C++ 编程课程。相反,如果您的问题是您不了解如何将教授的课程设计转换为 C++ 代码,那么您并不像您感觉的那么孤独。从模棱两可的英语设计语句转换为具体的 C++ 程序是很困难的,这也是我们获得高薪的原因。
让我们一步一步地看一下。
这听起来很简单,不是吗?
到这里,这一步就完成了!
你知道属性或成员变量是什么吗?您需要检查教科书的索引吗?继续吧,我会等。 ……哦,你回来了!您一定已经看到,成员变量是变量的集合,这些变量的范围由类声明限定,并且每次创建该类的对象时都会出现(作为一个组)。
哦,你以为我会把它们命名为“年”、“月”和“日”?不,那是你的作业,不是我的。既然您告诉我们您知道如何进行数学计算,那么我将把具体细节留给您。
还有两个词汇:方法和私有。去复习一下你的课程材料。
您肯定发现方法是
class
范围内的函数。方法的特殊之处在于它们仅在与当前类的对象相关时被调用。至于“私人”,好吧,我把它留给你。今年你肯定学到了一些关于
class
的知识吗?由于此方法必须计算
M
和N
,因此您需要某个地方来存储结果。嗯,也许您需要更多成员变量?最后,你的老师提到了set()、get()和compute()方法。这是他告诉您要创建哪些公共方法的简写方式。他可能并不是字面意思
int get() { ... }
。他可能意味着您需要创建一组名称和设计都相似的方法:这就是您开始创建类的方式。我希望这能为您提供入门所需的动力。祝你考试顺利。
So, you are stuck on creating a class. Either you don't know how to create a class at all, or you don't know how to create this specific class.
To create a class at all, use the
class
keyword:class { int i; };
. If you have not yet created any classes in C++, then you may need to retake this or another C++ programming class.If your problem, instead, is that you don't understand how to translate your professor's class design into C++ code, then you are not nearly as alone as you feel. Translating from ambiguous English design statements to concrete C++ programs is difficult, and is the reason we get paid the big bucks.
Let's go through it step by step.
That sounds easy enough, doesn't it?
There, that step is done!.
Do you know what attributes or member variables are? Do you need to check the index of your textbook? Go ahead, I'll wait. ... Oh, you're back! You must have seen that member variables are a collection of variables which are scoped by the class declaration and which spring into existence (as a group) everytime an object of that class is created.
Oh, you thought I would name them "year", "month", and "day"? No, that's your homework, not mine. Since you told us that you know how to do the math, I'll leave the specifics to you.
There are two more vocabulary words: method and private. Go review your course material for those.
You surely found that a method is a function inside the
class
scope. Methods are special in that they are invoked only in relation to objects of the class at hand.As for "private", well, I'll leave that to you. Surely you learned something about
class
es this year?Since this method has to compute
M
andN
, you'll need someplace to store the results. Hmm, maybe you need more member variables?Finally, your teacher mentioned set(), get() and compute() methods. This is his shorthand way of telling you which public methods to create. He probably doesn't mean literally
int get() { ... }
. He probably means that you need to create a group of methods, all similar in name and in design:There is how you start to create the class. I hope this gives the push you need to get started. Good luck on your exams.