| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1284 人关注过本帖
标题:C语言日历调了好久格式
只看楼主 加入收藏
烟雨晨曦
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:150
专家分:599
注 册:2017-3-5
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
C语言日历调了好久格式
#include<stdio.h>
#include<windows.h>
#include<conio.h>

//以1900年1月1日 星期一 为起点计算当前日期到1900年天数
int Day(int year,int month)
{
        int  sum = 0;
        int  day[12]={31,28,31,30,31,30,31,31,30,31,30,31};

        //判断平年闰年
        if((year % 400 == 0 )||((year % 4 ==0) && (year % 100 != 0)))
        {
            day[1] = 29;
        }

        
        if(year==1900)
        {
            for(int i=0;i<month-1;i++)
            {
                sum += day[i];
            }
            return sum;
        }
        else
        {
            //计算年的天数
            for(int j=1900;j<year;j++)
            {
                if((j % 400 == 0 )||((j % 4 ==0) && (j % 100 != 0)))
                {
                    sum += 366;
                }
                else
                {
                    sum += 365;
                }
            }
            
            //计算月的天数
            for(j = 0; j < month-1; j++)
            {
                sum += day[j];
            }
            return sum;
        }
}

//显示日期
void Date(int year,int month)
{
        int day[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
        if((year % 400 == 0 )||((year % 4 ==0) && (year % 100 != 0)))
        {
            day[1] = 29;
        }

        //计算星期
        int week = (Day(year,month)+1)%7;
        int j = 0;
        int i = 0;
        int count = 1;

        //输出格式调整
        for(i = 0; i < week; i++)
        {
            printf("    ");
            j = week;
        }

        //输出日期
        for(i = 1; i <= day[month-1]; i++,j++)
        {
            //星期六换行
            if(j==6)
            {
                 printf("%-3d ",i);
                 printf("\n");
                 j = 0;
                 i++;
                 count++;
            }
            //正常循环输出
            printf("%-3d ",i);
        }

        //格式调整
        if(count==5)
        {
            printf("\n\n");
        }
        else
        {
            printf("\n");
        }
}

int main()
{
        SYSTEMTIME time;
        GetSystemTime(&time);
        int year = time.wYear;
        int month = time.wMonth;
        int day = time.wDay;
        char date[255];
        sprintf(date,"      %i年%2i月%2i日",year,month,day);
        printf("%s\n\n",date);
        printf("日            六\n");
        Date(year,month);
        printf("        月增加\n");
        printf("         ↑ \n");
        printf(" 年减小←  →年增加\n");
        printf("         ↓ \n");
        printf("       月减小\n\n");
        printf("       ESC退出\n");
        char c;
        bool q=true;
        while(q)
        {
            c = getch();
            switch(c)
            {
                //月份增加
               case 'H':
                   month++;
                   if(month>12)
                   {
                      month=1;
                      year++;
                   }
                   break;
                //月份减小
               case 'P':
                   month--;
                   if(month<1)
                   {
                     month=12;
                     year--;
                     if(year<1900)
                     {
                        year = 1900;
                        month = 1;
                     }
                   }
                   break;
                //年分减小
               case 'K':
                   year--;
                   if(year<1900)
                   {
                     year = 1900;
                     month = 1;
                   }
                   break;
                //年份增加
               case 'M':
                   year++;
                   break;
                //退出
               case 27:
                   q=false;
               default:
                   break;
            }
            system("cls");
            sprintf(date,"      %i年%2i月%2i日",year,month,day);
            printf("%s\n\n",date);
            printf("日            六\n");
            Date(year,month);
            printf("        月增加\n");
            printf("         ↑ \n");
            printf(" 年减小←  →年增加\n");
            printf("         ↓ \n");
            printf("       月减小\n\n");
            printf("       ESC退出\n");
        }
        return 0;
}

搜索更多相关主题的帖子: 日历 起点 C语言 include 
2017-03-16 22:44
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
咋的 差在哪  是日期不对啊 还是星期不对啊 还是???

DO IT YOURSELF !
2017-03-16 22:55
烟雨晨曦
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:150
专家分:599
注 册:2017-3-5
收藏
得分:0 
回复 2楼 wp231957
大体上没什么问题,就是不知道有没有bug

[此贴子已经被作者于2017-3-16 23:33编辑过]

2017-03-16 23:26
快速回复:C语言日历调了好久格式
数据加载中...
 
   



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

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