2

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

这是我到目前为止所得到的。我可能离得很远。

#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;
}
4

5 回答 5

2

解决方案是使用一个新索引,它将在您的日历行中显示一个位置。那是:

int startDayPostion = (monthStartDay + 1) % 7;

因为您从星期一开始计数为零,但您的打印是从星期日开始的。因此,需要“向右移动”。阅读 a 后添加上面的行monthStartDay

然后您需要添加一个循环,它将打印您需要的所有空格,并将上述位置移动到所需的位置startDayPostion

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

for(在你的循环之前插入这个x

现在,当你有一个班次时,你可以简单地填充其余的单元格,记住你在结束之前(Sat)。

cout << x << setw(6);

不断移动帮助索引:

++p;

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

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

我不知道你为什么在这里放一个for (int i=1; i == 6; i++)循环......你可以简单地删除那些代码行。

于 2013-11-10T00:45:00.730 回答
1

这是我整理的东西-希望对您有所帮助!

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;


int main()
{
    int i, days, startday;

    cout << "Please enter the number of days in the month:" << endl;
    cin >> days;

    cout << "Please enter the starting date of the month (1 = Monday, 7 = Sunday):" << endl;
    cin >> startday;

    for (i = 1; i < startday; i++)
    {
        cout << "   ";
    }

    for (i = 1; i <= days; i++)
    {
        cout << setw(3) << i;

        if ((startday + i - 1) % 7 == 0)
        {
            cout << "\n";
        }
    }

    cout << endl;

    system("pause");

    return 0;
}

让我知道是否需要澄清。

于 2016-05-25T00:28:24.157 回答
0

我认为,这是您正在寻找的东西:

    #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);


        int offset = monthStartDay;          // offset for the first date
        for (int i = 0; i < offset; ++i)
            cout << "" << setw(6);             // output blank space

        for (int x=1 ; x <= daysInMonth; x++)
        {
            cout << x << setw(6);        
            if ((x+offset)%7 == 0)         // after each 7th output we have to
                cout << "\n ";             //  make a new line
        }
        return 0;
    }

我还假设,如果是您的日历标题,它将是 Sunday=0, Monday=1,...

于 2013-11-10T00:35:09.980 回答
0

您拥有的内部循环检查 i == 6,这永远不会发生。也许你为什么会觉得卡住了?

这里有两个问题需要解决:如何将新行放在正确的位置,以及如何填充到第一天。

我会把第一个问题留给你。

如果不给出答案,很难就这样的事情提供具体的帮助。让我开始吧。如果我说第一天是第五天,还有 30 天,那么你要打印出 35 件东西。前五个是空白空间,其余是当天的数字。

于 2013-11-10T00:32:59.037 回答
0

这是我的项目(包括叶年)。你可以参考这个。规则 1.必须使用 En 月份。2.必须使用1 -> 01~ 9-> 09 3.下线时必须使用“------------------”。4.必须使用“输入值”到“输出值”。

        #include <iostream>
    #include <ostream>
    #include <iomanip>

    using namespace std;

    void leafCalendar(int inputYear, int inputMonth);
    bool leafYear(int inputYear);
    int startToYear(int inputYear);
    int startToMonth(int inputYear, int inputMonth);
    int finishToDay[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    char display_year[5];
    char display_month[3];

    int main()
    {
        int inputYear, inputMonth, i;
        char response;
        char dayofWeek[7][5] = { {"Sun"},{"Mon"},{"Tue"},{"Wed"},{"Thu"},{"Fri"},{"Sat"} };
    do
    {
        cout << "enter the year and month. (exam: 2003 5) " << endl;   
        cin >> inputYear >> inputMonth;
        if (leafYear(inputYear))
            finishToDay[1] = 29;
        else finishToDay[1] = 28;
        cout << ("Input Month ");
        cout << (display_year, "$4d", inputYear);
        cout << ("Year");
        cout << (" ");
        cout << (display_month, "$2d", inputMonth);
        cout << ("Month");
        cout << ("is....") << endl;
        cout << ("\t< ");

        cout << (display_month, "$4d", inputYear);
        cout << (" ");
        if (inputMonth == 1) {
            cout << "January";
        };
        if (inputMonth == 2) {
            cout << "February";
        };
        if (inputMonth == 3) {
            cout << "March";
        };
        if (inputMonth == 4) {
            cout << "April";
        };
        if (inputMonth == 5) {
            cout << "May";
        };
        if (inputMonth == 6) {
            cout << "June";
        };
        if (inputMonth == 7) {
            cout << "July";
        };
        if (inputMonth == 8) {
            cout << "August";
        };
        if (inputMonth == 9) {
            cout << "September";
        };
        if (inputMonth == 10) {
            cout << "October";
        };
        if (inputMonth == 11) {
            cout << "November";
        };
        if (inputMonth == 12) {
            cout << "December";
        };
        cout << (" >");

        cout << "\n============================\n";
        if (inputMonth >= 1 && inputMonth <= 12)
        {
            for (i = 0; i < 7; i++)
                cout << " " << dayofWeek[i];
            cout << "\n----------------------------";
        }

        cout << endl;
        leafCalendar(inputYear, inputMonth);        
        cout << ("\n----------------------------\n");
        cout << "\n============================\n" << endl;
        cout << "Repeat?(Y/N): ";
        cin >> response;
    } while (response != 'N' || response != 'n');

    return 0;
}



    void leafCalendar(int inputYear, int inputMonth)
    {
        int StartToDay, LineBreak;

        int TermToLine = (startToYear(inputYear) + startToMonth(inputYear, inputMonth)) % 7;
        LineBreak = TermToLine;
        for (StartToDay = 0; StartToDay < TermToLine; StartToDay++)
            cout << "    ";

        for (StartToDay = 1; StartToDay <= finishToDay[inputMonth - 1]; StartToDay++)
        {   
            std::cout << "  " << std::setw(2) << std::setfill('0') << StartToDay;

            if (LineBreak == 6) {
                cout << "\n----------------------------" <<endl;
                LineBreak = 0;
            }
            else
                LineBreak++;

        }
    }

    bool leafYear(int b)
{
    if ((b % 4 == 0 && !(b % 100 == 0)) || (b % 400 == 0))
        return true;
    else {
        return false;
    }
}

int startToMonth(int inputYear, int inputMonth)
{
    int CheckToLeaf = 0;
    for (int i = 1; i < inputMonth; i++)
        CheckToLeaf += finishToDay[i - 1] % 7;
    if (inputMonth > 2 && leafYear(inputYear))
        CheckToLeaf++;

    return CheckToLeaf % 7;
}



int startToYear(int inputYear)
{
    int CheckToLeaf = 4;

    for (int a = 1980; a >= inputYear; a--)
    {
        CheckToLeaf += 6;
        if (leafYear(a))
            CheckToLeaf += 6;
    }    CheckToLeaf %= 7;

    return CheckToLeaf;
}
于 2018-11-06T02:35:48.333 回答