c++ - 如何设计/创建日历类

标签 c++ arrays class

当我的程序启动时,我希望它立即用从 1/1/1875 到 31/12/2025 的所有年/月/日填充“日历”类。我在创建这个“日历”类时遇到了问题,我真的不知道 C++ 有什么选择。

theclass

实现这一目标的最佳方式是什么?日/月/年是否应该是 multimap ,如果是的话;我怎样才能有效地使用该 multimap 来实现我想要做的事情( multimap 甚至可以容纳 2 个以上的对象?)?或者也许这个类根本不应该是一个类,而应该把整个东西变成一个 multimap?

我以这个“日历”项目为例,但我认为我的问题实际上归结为: “我如何设计一个类(或替代类)来保存多层链接且易于控制/访问的数据”。

“控制”是指添加/删除/检查是否已经存在等。

所以也许是这样的(忽略我在图片中的例子):

calendar.addYear(1998) //Add 1998 to the year stack (array?) inside the "calendar" object.
calendar.addMonth(1,0) //Add month 1 (january) to month stack (array?) keeping year-array position 0 as a reference, so we know what year this month belongs to.
calendar.addDay(1,0) //Add day 1 to day stack (array?) keeping month-array position 0 as a reference, so we know which month this day belongs to.

我想通过这样的类以编程方式实现的示例是:

//This is a magical GUI object I just made up ;D
myGUIofChoiceCreateSomeGUIobject GUIobject;
int i;
for(i=0; i < calendar.yearCount; i++;)
{
    GUIobject.addYeartoGUIobject(calendar.year[i]); //Add current year to our GUI. The user can now select it, hooray!
    addMonthsToYear(i); //Add months belonging to this year to our GUI.
}

addMonthsToYear(int theYear)
{
    int i;
    for(i=0; i < calendar.year[theYear].monthCount; i++;)
    {
        /*
        Our made-up GUI will add the current month to the year we received as an 
        argument. As long as that year is selected in the GUI, our user can pick this month, 
        hooray!
        */
        GUIobject.addMonthToGUIobject(calendar.year[theYear].month[i])
        addDaysToMonth(i,theYear) //Add days to this month (not typing this one down, you can see where this is going)
    }
}

而且好像所有这些还不够复杂;什么是没有待办事项的日历?所以“calendar.day”需要链接到一个数组/multimap/任何对象,它可能看起来像这样:

struct stuffToDo{
    std::string message;
    float time;
}

之后应该很容易将这样的对象添加到类中:

stuffToDo newstuff;
newstuff.message = "Find a C++ tutor so you can stop bothering stackoverflow";
newstuff.time = 15.20;

calendar.year[2012].month[11].day[18].addObject(newstuff);

我不知所措,我有什么选择?

PS.对于我的示例之间的不一致,我深表歉意,但在不知道我应该做什么的情况下这是不可避免的,这就是这个问题的本质。

最佳答案

Boost 库 是您需要查看的内容..

为了帮助自己,您可以找到 你想在这里做的日历课 <强> http://sourceforge.net/projects/c-cpp-calender/

关于c++ - 如何设计/创建日历类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8418507/

相关文章:

javascript - 如何更新reducer store

来自自定义类的 Swift addChild

c++ - 一个数组来保存 C++ 中的任何对象?

c++ - 解码日期格式?

c++ - 禁用不兼容选项的 gcc 警告

c++ - QUADS 中的 OpenGL C++ 普通 segmentation ,迭代方法

javascript - 主干 View 继承

c++ - Objective-C 地址持续多长时间?

python - 操作字符串数组时为 "TypeError: only integer scalar arrays can be converted to a scalar index"

java - Java中的赋值运算符