展开全部
#include<stdio.h>
int a[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int c[6][7];
void PrintMonthData( )
{
int i, j, k=0;
printf("日 一 二 三 四 五 六\n");
for(i=0; i<6; i++)
{
for(j=0; j<7; j++)
if(c[i][j]==0) printf(" ");
else printf("%2d ", c[i][j]);
printf("\n");
}
}
int isLeapYear(int year)
{
if((year%4==0 && year%100!=0)||year%400==0)
return 1;
return 0;
}
int GetDaysinYear(int year, int month, int day)
{
int i, x=0;
for(i=1; i<=month; i++)
x = x + a[isLeapYear(year)][i-1];
return x+day;
}
int GetWeek(int y, int m, int d)
{
if(m==1||m==2) {
m+=12;
y--;
}
return (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
}
void CreatMonthData(int year, int month)
{
int i, j, k, *p;
p = c;
k = (GetWeek(year, month, 1)+1)%7;
for(i=0; i<6; i++)
{
for(j=0; j<7; j++)
c[i][j] = 0;
}
for(i=1; i<=a[isLeapYear(year)][month-1]; i++)
*(p+k++) = i;
}
int isValidDate(int year, int month)
{
if(month>=1&&month<=12)
return 1;
return 0;
}
int main()
{
int a, b, c ;
char key[100];
printf("输入日期:YYYY-MM\n");
fgets(key,10,stdin);
sscanf(key, "%d-%d", &a, &b);
if( isValidDate(a, b))
{
CreatMonthData(a, b);
PrintMonthData( );
}
else printf("日期不合法!\n");
}
展开全部
这道题稍微麻烦一点,题主可以留下邮箱,明天发你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/**
*万年历demo
*@fencaide with vs2010
*/
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
#include <conio.h>
#include<string.h>
#define TRUE 1;
#define FALSE 0;
int isValidate(char *str,int *year,int *month){
int flag = FALSE;
*year = 0;
*month = 0;
if(NULL != str && 0 < strlen(str) ){
sscanf(str,"%d-%d", year, month);
if(*month > 0 && *year > 0){
flag = TRUE;
}
}
return flag;
}
int isLeapYear(int year){
if((year%4==0 && year%100!=0) || year%400==0)
return TRUE;
return FALSE;
}
int GetDaysInYear(int year,int months,int day){
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int sum = 0;
if(isLeapYear(year)){
days[1] = 29;
}
for(int i = 0; i < months - 1; i++){
sum += days[i];
}
return sum + day;
}
int GetWeek(int year,int month, int day){
int sum = 0;
for(int i = 1 ; i < year; i ++){
if(isLeapYear(i)){
sum += 366;
}
else{
sum += 365;
}
}
sum += GetDaysInYear(year, month, day) - 1;
return (sum % 7) + 1;
}
void GetMonthData(int array[][7],int year,int month){
int start = GetWeek(year, month , 1);
int end = 0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
end = 31;
break;
case 2:
if(isLeapYear(year))
end = 29;
else
end = 28;
break;
default:
end = 30;
}
for(int i = start + 1; i < end + start + 1 ; i ++){
array[i/7][i%7 - 1] = i - start;
}
}
void Output(int array[][7]){
printf("日 一 二 三 四 五 六\n");
for(int i = 0; i < 6; i++){
for(int j=0;j < 7; j ++){
printf("%2d ",array[i][j]);
}
printf("\n");
}
}
void Input(int *year, int *month){
int size = 1024;
char* buff = (char*)malloc(size);
printf("please input date:\n", strlen(buff));
while(NULL != fgets(buff, size, stdin)){
if(!isValidate(buff,year, month)){
printf("Validate fallen,try again!\n", buff);
}
else{
break;
}
}
free(buff);
}
int main(void){
int year = 0;
int month = 0;
int a[6][7] = {0};
Input(&year, &month);
GetMonthData(a,year, month);
Output(a);
getchar();
getchar();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询