| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2052 人关注过本帖
标题:关于打印月历的题目求教
只看楼主 加入收藏
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
关于打印月历的题目求教
打印月历,经过调试之后情况是
闰年没几个月是对的
平年的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
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
与其研究他的代码哪里有错误  还莫如自己重写一份了
他的逻辑有些乱  看不太明白

DO IT YOURSELF !
2017-03-04 16:39
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
回复 2楼 wp231957
这就是我自己写的
2017-03-04 17:42
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
回复 3楼 Jonny0201
我觉得你应该设计一个能求    X年X月X日 是星期几的函数

而你的 FIRSTDAY函数明显不对 而那个basemonth是咋来的 或者你的总体思路 我不怎了解 所以你这个代码 基本无法帮你

DO IT YOURSELF !
2017-03-04 17:46
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
你好像也应用了泰勒公式  不过我感觉有些问题
程序代码:
//利用泰勒公式计算星期几
//w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
int getweek(int year,int month,int day)
{
    int w;    //星期
    int c;    //世纪-1 YYYY的头两位
    int y;    //年份   YYYY的后两位
    int m;    //月份 >=3 1月 2月看成上年的13月 14月
    int d=day;    //
    if(month>=3)
    {
        c=year / 100;
        y=year % 100;
        m=month;
    }
    else
    {
        m=month+12;
        y=(year-1) % 100;
        c=(year-1) / 100;
    }
    w=y+y/4+c/4-2*c+(26*(m+1))/10+d-1;
    w=(w+700) %7;
    return w;
}



DO IT YOURSELF !
2017-03-04 17:48
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.021517 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved