| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2028 人关注过本帖
标题:关于打印月历的题目求教
取消只看楼主 加入收藏
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
关于打印月历的题目求教
打印月历,经过调试之后情况是
闰年没几个月是对的
平年的7月和8月有错误,其他都是对的
有那么几种情况:
1.firstday函数里面的公式有问题
2.basicMouth函数里的月基数有问题
(参考:http://wenku.baidu.com/link?url=HHFg4LY8ygZbTQVbyA5HhZAem1oGJeSuXpBaL6ALdLtd_tyQASSOb3th6gRu45r48DqXKXLSqDaI741QnkP7jObWjv7neE9I-_PQ3WoOOni)
3.display1函数有问题
最大可能是3,但是找不出来哪里有问题,请教各位
代码如下:
#include <iostream>
#include <string>

void display0(std::string enmouth[], int year, int mouth);
int firstday(int mouth, int year);
int days(int mouth, int year);
void display1(int W, int day, int mouth, int year);
int basicMouth(int mouth, int baseYear);

using namespace std;
int main(int argc, char *argv[]) {
    cout << "Enter full year (e.g. 2010) : ";
    int year;
    cin >> year;
    cout << "Enter mouth in number between 1 and 12 : ";
    int mouth;
    cin >> mouth;
    string enmouth[12] = {"January" ,"February" ,"March" ,"April" ,"May" ,"June" ,"July" ,"August" ,"September" ,"October" ,"November" ,"December"};
    display0(enmouth, year, mouth);
    int W;
    W = firstday(mouth, year);
    int day;
    day = days(mouth, year);
    display1(W, day, mouth, year);
}
void display0(std::string enmouth[], int year, int mouth)
{
    for (int i = 1; i <= 43; i++)
    {
        cout << "-";
    }
    cout << endl;
    for (int i = 1; i <= 2; i++)
    {
        cout << "\t";
    }
    cout << enmouth[mouth - 1] << "  " << year << endl;
    for (int i = 1; i <= 43; i++)
    {
        cout << "-";
    }
    cout << endl;
    cout << "Sun  Mon  Tue  Wed  Thu  Fri  Sat" << endl;
}
int firstday(int mouth, int year)
{
    int baseYear, baseMouth;
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) baseYear = 2;
    else baseYear = 1;
    baseMouth = basicMouth(mouth, baseYear);
    int W;
    W = (year + year / 4 + year / 400 - year / 100 - baseYear + baseMouth + 1) % 7;
    return W;
}
int basicMouth(int mouth, int baseYear)
{
    int baseMouth;
    if (baseYear == 1)
    {
        switch(mouth){
            case 1:
                baseMouth = 0;
                break;
            case 2:
                baseMouth = 3;
                break;
            case 3:
                baseMouth = 3;
                break;
            case 4:
                baseMouth = 6;
                break;
            case 5:
                baseMouth = 1;
                break;
            case 6:
                baseMouth = 4;
                break;
            case 7:
                baseMouth = 0;
                break;
            case 8:
                baseMouth = 3;
                break;
            case 9:
                baseMouth = 5;
                break;
            case 10:
                baseMouth = 0;
                break;
            case 11:
                baseMouth = 3;
                break;
            case 12:
                baseMouth = 5;
                break;
        }
    }
    if (baseMouth == 2)
    {
        switch(mouth){
            case 1:
                baseMouth = 0;
                break;
            case 2:
                baseMouth = 3;
                break;
            case 3:
                baseMouth = 4;
                break;
            case 4:
                baseMouth = 0;
                break;
            case 5:
                baseMouth = 2;
                break;
            case 6:
                baseMouth = 5;
                break;
            case 7:
                baseMouth = 0;
                break;
            case 8:
                baseMouth = 3;
                break;
            case 9:
                baseMouth = 6;
                break;
            case 10:
                baseMouth = 1;
                break;
            case 11:
                baseMouth = 4;
                break;
            case 12:
                baseMouth = 6;
                break;
        }
    }
    return baseMouth;
}
int days(int mouth, int year)
{
    int day;
    switch (mouth){
        case 1:
            day = 31;
            break;
        case 2:
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) day = 29;
            else day = 28;
            break;
        case 3:
            day = 31;
            break;
        case 4:
            day = 30;
            break;
        case 5:
            day = 31;
            break;
        case 6:
            day = 30;
            break;
        case 7:
            day = 31;
            break;
        case 8:
            day = 31;
            break;
        case 9:
            day = 30;
            break;
        case 10:
            day = 31;
            break;
        case 11:
            day = 30;
            break;
        case 12:
            day = 31;
            break;
    }
    return day;
}
void display1(int W, int day, int mouth, int year)
{
    cout << "  ";
    for (int i = 1; i <= W; i++)
    {
        cout << "     ";
    }
    int i;
    i = 1 + W;
    for (int j = 1; j <= day; j++)
    {
        cout << j;
        if (j <= 8) cout << "    ";
        else cout << "   ";
        if (i % 7 == 0)
        {
            cout << endl;
            if (j < 9) cout << "  ";
            else cout << " ";
        }
        i++;
    }
}

用的是C++,但是除了cin cout还有字符串数组和必要的头文件描述之外,其余都是C语言的内容
搜索更多相关主题的帖子: include 
2017-03-04 15:52
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
回复 2楼 wp231957
这就是我自己写的
2017-03-04 17:42
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
回复 5楼 wp231957
用你的公式OK了
应该是那个文档里的公式有问题
谢谢
2017-03-06 18:48
快速回复:关于打印月历的题目求教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018068 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved