| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3069 人关注过本帖
标题:帮一MM写的一个日历小程序
取消只看楼主 加入收藏
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
结帖率:100%
收藏
 问题点数:0 回复次数:3 
帮一MM写的一个日历小程序
帮一MM写的一个日历小程序 。程序从1980年1月1日开始可以计算到9999年12月31日
程序代码:
#include <stdio.h>
#include <stdlib.h>

#define  LEAP(x) ( !(x % 4) && ( !(x % 400) || (x %100) ) )

char *w[] = { "Sun" , "Mon", "Tue" , "Wed" , "Thu" , "Fri" ,"Sat" }  ;
enum {Sun , Mon ,Tue , Wed , Thu , Fri,Sat} days ;

void setsp(int n) {

           int i ;
           for ( i = 1 ; i <= n ; i++)
                putchar(' ') ;
  return  ;
}

void Print_Week(char *w[]) {

                int  i ;
                for (i = 0 ; i < 7 ; i++)
                    printf("%+5s" , w[i]) ;
                printf("\n") ;


 return  ;
} 

void Print_date(int year ,int month , int date) {

                int  i , j = date ,month_days ;
                
                if (month == 2)
                    month_days = 28 + LEAP(year) ;

                else
                if (month == 4 || month == 6 || month == 9 || month == 11)
                    month_days = 30 ;
                else
                    month_days = 31 ;
                    
                for (i = 1 ; i <= month_days ; i++,j++) {
                    if (j-1 == Sat){
                       putchar('\n') ;
                       j = Sun ;
                    }
                    printf("%5d" , i) ;
                 }
   return  ;
}

int Get_day(int current_year , int current_month){

         int  year=1980 ,month = 1 ,day = 2 , month_days = 31 ; //1981-1-1 Tuesday
         
         while (year < current_year) {

                for (month = 1 ; month <= 12 ; month++) {
                    if (month == 2)
                        month_days = 28 + LEAP(year) ;
                    else if (month == 4 || month == 6 || month == 9 || month == 11)
                             month_days = 30 ;
                    else
                         month_days = 31 ;
                    day = (day + (month_days % 7) ) % 7 ;
                }
                year++ ;
          }
          for (month = 1 ; month <= current_month ; month++) {
                if (month == 2)
                    month_days = 28 + LEAP(year) ;

                else
                if (month == 4 || month == 6 || month == 9 || month == 11)
                    month_days = 30 ;
                else
                    month_days = 31 ;
                    day = (day + (month_days % 7) ) % 7 ;
          }
   return day ;

  }

int main(void) {

         int year , month , day ;
         printf("Input the year and month like  \"2008-10\":") ;
         scanf("%d-%d", &year , &month) ;
         
         if(year < 1980 || month > 12) {
            fprintf(stderr,"The year can not less than 1980 and the month can not more than 12\n") ;
            exit(1) ;
         }
         day = Get_day(year , month - 1) ;

         Print_Week(w) ;
         switch (day) {
                 case Sun: setsp(0) ; Print_date(year,month,day) ; break ;
                 case Mon: setsp(5) ; Print_date(year,month,day) ; break ;
                 case Tue: setsp(10); Print_date(year,month,day) ; break ;
                 case Wed: setsp(15); Print_date(year,month,day) ; break ;
                 case Thu: setsp(20); Print_date(year,month,day) ; break ;
                 case Fri: setsp(25); Print_date(year,month,day) ; break ;
                 case Sat: setsp(30); Print_date(year,month,day) ; break ;
          }
          putchar('\n') ;
  return 0 ;
}


[[it] 本帖最后由 iFreeBSD 于 2008-10-30 10:26 编辑 [/it]]
搜索更多相关主题的帖子: 日历 
2008-10-30 10:06
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
直接贴出来的格式杂这么难看。

without further ado, let’s get started
2008-10-30 10:07
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
to 风居住的街道
我用gcc,cc 等编译均无问题,况且"+"属printf格式输出修饰符,哪来warnning之说,如果有更简单的代码不妨发上来,函数命名属个人喜好,样例输入我也已经给出,你不按说明输入得到了错误结果,我想那不是我的错吧。

[[it] 本帖最后由 iFreeBSD 于 2008-10-30 13:35 编辑 [/it]]

date.jpg (7.24 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

without further ado, let’s get started
2008-10-30 13:32
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
[bo][un]风居住的街道[/un] 在 2008-10-30 19:26 的发言:[/bo]

1 首先这程序写得的确不错,编译也没有问题的确,但是编译开-Wall选项是个好习惯,希望LZ能考虑一下~~~

呵呵,我是先编译通过了,运行后有问题再gcc-Wall,再gdb.
你的程序运行不正确。我看不出你从哪一年为起点开始算的。

without further ado, let’s get started
2008-10-31 21:32
快速回复:帮一MM写的一个日历小程序
数据加载中...
 
   



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

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