C++课程:基督教节日计算器

发布于 2024-11-07 18:44:43 字数 1321 浏览 0 评论 0原文

基本上,我正在尝试完成这个考试式的问题以进行考试练习。我知道如何做所有事情,除了最后一个问题,即与课程有关的问题。我有点明白它们,但不知道如何将其应用到问题中。有人可以给我一些关于如何创建 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 技术交流群。

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

发布评论

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

评论(2

遥远的她 2024-11-14 18:44:43

...我将如何创建一个 C++ 类?

创建一个类很简单:

class foobar
{
public:
private:
};

好了!你现在有一个 foobar 类。该类可以是您想要的任何类。根据需要填写公共和私人部分。

有人可以给我一些起点或提示吗...

至于如何使用类来解决给定问题,以下是大致按顺序排列的一般步骤:

  1. 确定问题的要求。需要解决什么问题?
  2. 将这些要求改革为责任清单。您的程序必须做什么才能满足这些要求?
  3. 将这些职责组合在一起形成一个有凝聚力的类对象。这个类对象如何有助于问题的整体解决?这个对象实际上将如何使用?
  4. 在每个对象的单元测试中表达并捕获该用法。在执行此操作时,您可以假装该类已经实现,并且您需要的任何成员函数都可用。
  5. 最后,实现该类以便单元测试通过。

最后一点,正如大卫的评论已经指出的那样,将原始问题逐字复制并粘贴到您的问题中是一个坏主意。人们(基本上)不太愿意通读它。如果您准确地澄清了自己遇到的问题并删除了所有不相关的部分,则可以增加获得答复的可能性。

...how I would go about creating a C++ class?

Creating a class is simple:

class foobar
{
public:
private:
};

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.

Could anybody give me some starting points or tips...

As for how to use classes on how to solve a given problem, here are the general steps in roughly sequential order:

  1. Identify the requirements of the problem. What needs solving?
  2. Reform those requirements as a list of responsibilities. What does your program have to do to fulfill those requirements?
  3. Group those responsibilities together to form a cohesive class object. How does this class object contribute to the overall solution of the problem? How will this object actually be used?
  4. Express and capture that usage in a unit test for each of those objects. While doing this, you can pretend that that class is already implemented and whatever member functions you need from it is available.
  5. Finally, implement that class so the unit test passes.

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.

厌倦 2024-11-14 18:44:43

所以,你只能坚持创建一个类。要么您根本不知道如何创建一个类,要么您不知道如何创建这个特定的类。

要创建一个类,请使用 class 关键字: class { int i; };。如果您尚未在 C++ 中创建任何课程,那么您可能需要重修本课程或其他 C++ 编程课程。

相反,如果您的问题是您不了解如何将教授的课程设计转换为 C++ 代码,那么您并不像您感觉的那么孤独。从模棱两可的英语设计语句转换为具体的 C++ 程序是很困难的,这也是我们获得高薪的原因。

让我们一步一步地看一下。

生成一个 C++ 类来实现基督教节日计算器。

这听起来很简单,不是吗?

class ChristianHolidayCalculator {
};

到这里,这一步就完成了!

它应该有私有属性(成员变量),表示日期的日、月、年

你知道属性成员变量是什么吗?您需要检查教科书的索引吗?继续吧,我会等。 ……哦,你回来了!您一定已经看到,成员变量是变量的集合,这些变量的范围由类声明限定,并且每次创建该类的对象时都会出现(作为一个组)。

class ChristianHolidayCalculator {
    double width;
    double height;
    double depth;
};

哦,你以为我会把它们命名为“年”、“月”和“日”?不,那是你的作业,不是我的。既然您告诉我们您知道如何进行数学计算,那么我将把具体细节留给您。

以及计算 M 和 N 项的私有方法。

还有两个词汇:方法私有。去复习一下你的课程材料。

您肯定发现方法是 class 范围内的函数。方法的特殊之处在于它们仅在与当前类的对象相关时被调用。

至于“私人”,好吧,我把它留给你。今年你肯定学到了一些关于class的知识吗?

class ChristianHolidayCalculator {
    double width;
    double height;
    double depth;
    void GuitarTune() {
      // Code to tighten strings goes here.
    }
};

由于此方法必须计算 MN,因此您需要某个地方来存储结果。嗯,也许您需要更多成员变量?

最后,你的老师提到了set()get()compute()方法。这是他告诉您要创建哪些公共方法的简写方式。他可能并不是字面意思 int get() { ... }。他可能意味着您需要创建一组名称和设计都相似的方法:

class ChristianHolidayCalculator {
    double width;
    double height;
    double depth;
    void GuitarTune() {
      // Code to tighten strings goes here.
    }
    int getRed() { /* return red value */ }
    int getGreen() { /* return green value */ }
    int getBlue() { /* return blue value */ }
    void setRed(int newRed) { /* assign red value */ }
    void setGreen(int newGreen) { /* guess */ }
    ... calculateEaster() ... { ... }
};

这就是您开始创建类的方式。我希望这能为您提供入门所需的动力。祝你考试顺利。

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.

Produce a C++ class to implement a Christian Holiday calculator.

That sounds easy enough, doesn't it?

class ChristianHolidayCalculator {
};

There, that step is done!.

It should have private attributes (member variables) representing the day, month and year of the date

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.

class ChristianHolidayCalculator {
    double width;
    double height;
    double depth;
};

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.

and a private method for calculating the terms M and N.

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 classes this year?

class ChristianHolidayCalculator {
    double width;
    double height;
    double depth;
    void GuitarTune() {
      // Code to tighten strings goes here.
    }
};

Since this method has to compute M and N, 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:

class ChristianHolidayCalculator {
    double width;
    double height;
    double depth;
    void GuitarTune() {
      // Code to tighten strings goes here.
    }
    int getRed() { /* return red value */ }
    int getGreen() { /* return green value */ }
    int getBlue() { /* return blue value */ }
    void setRed(int newRed) { /* assign red value */ }
    void setGreen(int newGreen) { /* guess */ }
    ... calculateEaster() ... { ... }
};

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.

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