c ++日历循环设计 [英] c++ calendar loop design

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

问题描述

我需要帮助写一个函数 displayTable()在屏幕上显示日历。该函数将采用两个参数:

I need help to write a function displayTable() to display a calendar on the screen. The function will take two parameters:

numDays :一个月的天数。

:与星期一的偏移量。如果偏移量为零,则月份从星期一开始。如果偏移量为2,则月份从星期三开始。如果偏移量为6,那么月份从星期日开始。

numDays: The number of days in a month.
offset: The offset from Monday. If the offset is zero, then the month starts on Monday. If the offset is 2, the month starts on Wednesday. If the offset is 6, the month starts on Sunday.

我的程序必须使用循环;我不能用几个IF语句和COUT硬编码程序。接下来我必须写 main(),这样它会提示用户输入月份的天数和偏移量。

My program has to work with loops; I can't "hard code" the program with a couple of IF statements and COUTs. Next I have to write main() so that it prompts the user for the number of days in the month and the offset.

它需要如下所示:

It needs to look like this:

我很难把它放在一起。任何想法?

I'm having a hard time putting it all together. Any ideas?

推荐答案

我将假设你已经照顾了如何计算offset和numDays是基于特定年份计算的。所有剩下的是一个接受numDays和offset的函数。我要显示表格,每个数字用空格分隔。我会离开你如何使用这种方法,但算法如下:

I'm going to assume that you have already took care of how to figure out how the offset and numDays is computed based on a particular year. All that is left is a function that accepts numDays and offset. I'm going to display the table where each number is separated by spaces. I'll leave to you how to make use of this method but the algorithm goes like this:

displayTable(int numDays, int offset)
{
    // go to the correct location based on offset
    for (int i=0; i<offset; i++) cout << "     ";

    // start writing the callendar
    for (int i=1; i<=numDays; i++)
    {
        // print the date
        cout << i;

        // update the value of offset
        if (offset<7) {
            offset++;
            cout << "     ";
        else {
            offset = 0;
            cout << "\n";
        }
    }
}



Usage:

void main()
{
    int numDays, offset;

    // input Year and Month

    // compute the value of numDays and offset

    displayTable(numDays, offset);
}

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

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