课程实践----万年历(三种输入三种显示)

问题及代码:

/*
*copyright (t) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:张晴晴
*完成日期:2016年6月30日
*版本号:v1.0
*问题描述:万年历(3)
*输入描述:
三种输入方法,
1.输入年份。
2.输入年份月份
3.输入年月日
*程序输出:
1.输出一年的月份
2.输出某年某月的月历
3.输出某年某月某日所在的月历,具体日期标注
*/

#include 
#include 
#include 

char	months[13][10] = {"","Janurary","Feberury","March","April","May","June","July","Augest","September","October","November","December"};
//判断每个月的第一天是星期几,
int judgeFirstDate(int year, int m, int q)
{
    if(m == 1 || m == 2)
    {
        m += 12;//如果月份为1,2,则表示为去年的13,14月,年份也改为去年
        --year;
    }
    int j = year / 100;//j表示世纪
    int k = year % 100;//k表示本世纪的第几年
    int h = (q + 26*(m+1)/10 + k + k/4 + j/4 + 5*j) % 7;//h=0为周六,h=1为周日,依次类推
    return h;
}

//处理周几,把h=0为周六改为h=0为周日,把h=1为周日改为h=1为周一,依次类推
int judgeWeek(int week)
{
    if(!week)
        return 6;
    else
        return --week;
}

//把输入的字符串转换为数字年份
void input(char* cTime, int* year, int* month, int* day)
{
    int len = strlen(cTime);
    if(cTime[0] == '+')
    {
        ++(*month);
        return;
    }
    else if(cTime[0] == '-')
    {
        --(*month);
        return ;
    }
    *year = *month = *day = 0;
    *year = atoi(cTime);
    if(len >= 6)
        *month = atoi(cTime+5);
    if(len >= 8)
        *day = atoi(cTime+7);
}

//判断是不是闰年
int isLeapYear(int year)
{
    if((year%400 == 0) || (year%4 == 0 && year%100 != 0))
        return 1;
    return 0;
}

//输出月份最上边的哪个导航
void outputWeek()
{
    printf("Sun  Mon  Tues Wed  Thu  Fri  Sat\n");
}

//判断某年某个月份有几天
int monthDay(int year, int month)
{
    int judge = isLeapYear(year);
    switch(month)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        return 31; break;
    case 4: case 6: case 9: case 11:
        return 30; break;
    case 2:
        return judge == 1 ? 29 : 28;
        break;
    }
    return 0;
}

//输出某年的某个月
void outputOneMonth(int year, int month, int day)
{
    int i;
    int dayNum = monthDay(year,month);
    int week = judgeFirstDate(year,month,1);
    week = judgeWeek(week);

    printf("==================================\n");
    printf("%s\n",months[month]);
    outputWeek();
    for(i = 0; i < week; ++i)
        printf("     ");
    for(i = 1; i <= dayNum; ++i)
    {
        if(day == i) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                                             FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        printf("%-5d",i);
        if(day == i) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                        FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
        if((week+1)%7 == 0)
            printf("\n");
        ++week;
    }
    if(week %7 != 0)
        printf("\n");
    printf("==================================\n");
}

//输出一年的所有月份
void outputAllMonth(int year)
{
    int i;
    for(i = 1; i <= 12; ++i)
    {
        outputOneMonth(year,i,0);
        printf("\n");
    }
}

int main()
{
//	console();
    int year,month,day;
    char cTime[11];
    while(fgets(cTime,11,stdin))
    {
        //读入数据
        input(cTime,&year,&month,&day);
        printf("%d",year);
        if(month == 0)
        {
            printf("\n");
            //输出一年的所有月份
            outputAllMonth(year);
        }
        else
        {
            printf(".%d",month);
            if(day != 0)
                printf(".%d",day);
            printf("\n");
            //输出某个月份
            outputOneMonth(year,month,day);
        }
        printf("+:last month\n-:next month\n");
    }
    return 0;
}




运行结果:

课程实践----万年历(三种输入三种显示)_第1张图片

课程实践----万年历(三种输入三种显示)_第2张图片

课程实践----万年历(三种输入三种显示)_第3张图片

课程实践----万年历(三种输入三种显示)_第4张图片

你可能感兴趣的:(技术支持实训项目)