C语言实现日历(命令行传参)

日历的实现,需注意的问题是(1)判断某年某月的方法是先确定某一天的日期属性,其他时间则根据差值的不同来确定其属性。

(2)将命令行参数进行转换

代码如下:若读者有好的算法,希望指正赐教

//本程序需在命令行窗口执行 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//小项目之万年历的实现 
typedef struct{
	int year;
	int month;
	int day;
}Day;//定义日期的结构 
//判断某一年是否为闰年 
int isrun(int year){
		if(((year%4==0)&&(year%100!=0))||(year%400==0)){
		return 1;
	}
	else 
	return 0;
}
//求出某一年的总共天数 
int askDay(int D){
	if(isrun(D))
	return 366;
	else
	return 365;
} 
//求出某一年的某一个月的总天数 
int askday(Day D){
	switch(D.month){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
		return 31;
		case 4:
		case 6:
			case 9:
				case 11:
					return 30;
					case 2:
						if(isrun(D.year)){
							return 29;
						}
						return 28;
	}
}
/*判断某一天是星期几,主要是先确定某一个日期为星期几(本例以2000年1月1日为星期六),然后
求出欲判断的日期与给定日期所相差的天数,然后与7取余,即求出结果*/ 
int askxin(Day D){
	int sum=0,i=0;
	int year;
	if(D.year>2000){
		year=D.year;
		while(year>2000){
				year--;
			sum+=askDay(year);
		
		}
		if(D.month>1){
			while(D.month>1){
					D.month--;
				sum+=askday(D);
			
			}
			sum+=D.day-1;
			
		}
	}
	else{
		while(D.year<2000){
			D.year++;
			sum+=askDay(D.year);
		}
		while(D.month>1){
			D.month--;
			sum+=askday(D);
		}
		sum+=D.day-1;
		
	}
	i=sum%7;
	switch(i){
		case 0:
			return 6;
			case 1:
				return 7;
				case 2:
					return 1;
					case 3:
						return 2;
						case 4:
							return 3;
							case 5:
								return 4;
								case 6:
									return 5;
	}
}
//打印日历,注意的地方是每一个月开始的空白打印,可以结合星期几,然后确定打印几个\t,且数字日历而后打印,注意换行的依据 
void show1(Day D){
	int xin,m,i;
	D.day=1;
	xin=askxin(D);
	m=xin%7;
	printf("*****************************************************\n");
	printf("日	一	二	三	四	五	六\n");
	for(i=0;i<m;++i){
		printf("\t");
	}
	for(i=1;i<=askday(D);++i){
		printf("%2d\t",i);
		xin++;
		if(xin%7==0&&i!=askday(D))
		printf("\n");
	}
	printf("\n***************************************************\n");	
}

int main(int argc,char** date){
	Day D;
	char day[15];
	if(argc==1){
		printf("没有输入所需查看的日期\n");
		exit(0);
	}
	strcpy(day,date[1]);
	D.year=((int)(day[0])-48)*1000+((int)(day[1])-48)*100+((int)(day[2])-48)*10+((int)(day[3])-48);
	D.month=(int(day[5])-48)*10+(int(day[6]-48));
	D.day=(int(day[8])-48)*10+(int(day[9])-48);
	if(day[8]!='/'){
		printf("%d年%d月%d日是星期%d\n",D.year,D.month,D.day,askxin(D));
		return 0;
	}
	show1(D);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值