C ++日历问题 [英] C++ Calendar problems

查看:64
本文介绍了C ++日历问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个输出日历的程序.用户必须输入该月的开始日期(星期一-0,星期二-1等),以及该月中有多少天.根据月份的开始日期,日历日期将在该特定日期开始.我遇到的问题是,我不确定如何让日历在特定的一天开始,并且我不确定如何使日期在7天后转到新的行.任何帮助,将不胜感激.到目前为止,我们还没有学到很多东西,所以实际上只允许我使用基础知识,而不能使用任何函数或类似的东西.

I'm trying to write a program that outputs a calendar. The user has to input the day in which the month starts (Monday-0, Tuesday-1, etc.), and how many days are in the month. Depending on what day the month starts, the calendar dates will start under that specific day. The issues I'm having is, I'm not sure how to get the calendar to start under a specific day, and I'm not sure how to get the dates to go to a new line after the 7 days. Any help would be appreciated. We haven't learnt much so far, so I'm really only allowed to use the basics, no functions or things like that.

这是我到目前为止所掌握的.我可能会很遥远.

Here's what I've got so far. I could be far out.

#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

int main()
{
    int monthStartDay, daysInMonth;

    cout << "Enter the first day of the month: ";
    cin >> monthStartDay;
    cout << "Enter how many days are in the month: ";
    cin >> daysInMonth;

    cout<<"\nSun   Mon   Tue   Wed   Thu   Fri   Sat";
    cout<<"\n\n"<<setw(2);

    for (int x=1; x <= daysInMonth; x++){
        cout << x << setw(6);

        for (int i=1; i == 6; i++) {
            cout << "\n";
        }
}
    return 0;
}

推荐答案

该解决方案正在使用一个新索引,该索引将在您的日历行中显示一个 position .那就是:

The solution is using a new index, that will show a position in your calendar row. That is:

int startDayPostion = (monthStartDay + 1) % 7;

因为您从星期一开始算零,但打印从星期日开始.因此,需要向右移动".阅读 monthStartDay 后添加以上行.

becouse you are counting zero from Monday, but your print is starting from Sunday. Hence a "shift to the right" is need. Add above line after reading a monthStartDay.

然后您需要添加一个循环,该循环将打印所需的所有空格,并将上述 position 移至所需的 startDayPostion :

You need then to add a loop, which will print all spaces you need, and will shift above mentioned position to the desired startDayPostion:

int p = 0;
for (; p < startDayPostion; ++p) {
    cout << "" << setw(6);
}

(在您的 for 循环之前用 x 插入此代码)

(Insert this before your for loop with x)

现在,当您有班次时,您只需填充其余的单元格即可,请记住您已经排在末尾( Sat ).

Now, when you have a shift, you can simply fill the rest of the cells, keeping in mind that you are before the end (Sat).

之后

cout << x << setw(6);

继续移动帮助索引:

++p;

,然后,如果您已经完成了该行,请转到新行并重置p:

and then, if you are done with the line, go to the new line and reset p:

if (p > 6) {
    cout << '\n';
    p = 0;
}

我不知道您为什么要在这里为循环(int i = 1; i == 6; i ++) ......您只需删除这些代码行即可.

I dont know why do you put here a for (int i=1; i == 6; i++) loop... You can simply delete those lines of code.

这篇关于C ++日历问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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