[ create a new paste ] login | about

Link: http://codepad.org/bL5K1TJa    [ raw code | output | fork ]

C, pasted on Dec 1:
#include <stdio.h>
#include <stdlib.h>

int main(int argc , char *argv[]) {
	FILE *fp;
	const char *wd[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
	int md[]={31,28,31,30,31,30,31,31,30,31,30,31};  // 1~12月までの日数
	const int md_sz=sizeof(md)/sizeof(md[0]);
	int zel;
	int y,m,d,zy,zm,zd,i,j;
	const int wd_sz=sizeof(wd)/sizeof(wd[0]);
	const int wd_wid=4;
	
	if( argc<3 ) {
		printf("出力するファイル名と年、月の指定をして下さい。\n");
		printf("例:実行プログラム名  cal1012.txt 2010 12\n");
		return 1;
	}
	
	y=atoi(argv[2]);
	m=atoi(argv[3]);
	
	if(m<1 || m>md_sz) {
		printf("月の指定が不適切です。\n");
		return 1;
	}
	if(y<1583) {
		printf("1583年以降を入力して下さい。\n");
		return 1;
	}
	
	fp=fopen(argv[1],"w");
	if( fp==NULL ) {
		perror("fopen ");
		return 1;
	}
	
	
	if(y%4==0) {
		if(y%100!=0 || y%400==0)
		md[1]=29;  // 閏年の2月を29日に
	}
	
	zy=y; zm=m; zd=1;
	if( zm<=2 ) { zy--; zm+=12; }
	zel=(zy+zy/4-zy/100+zy/400+((13*zm+8)/5) + zd)%7;
	
	fprintf(fp,"%d/%d\n",y,m);
	
	for(i=0; i<wd_sz; i++) {
		fprintf(fp,"%*s",wd_wid,wd[i]);
	}
	fprintf(fp,"\n");
	
	for(i=1; i<=zel; i++) {
		fprintf(fp,"%*c",wd_wid,0x20);
	}
	
	for(d=1;d<=md[m-1]; i++,d++) {
		fprintf(fp,"%*d",wd_wid,d);
		if(i%wd_sz==0) fprintf(fp,"\n");
	}
	fprintf(fp,"\n");
	
	fclose(fp);
	
	return 0;
}


Output:
1
2
出力するファイル名と年、月の指定をして下さい。
例:実行プログラム名  cal1012.txt 2010 12


Create a new paste based on this one


Comments: