1.输入某年某月某日得一年的第几天2.某年第几天得该年几月几日3.Switch语句综合两种情况

#include <stdio.h>
#include <math.h>
int  DayofYear(int year, int month, int day); 
int main()
{
    int year,month,day;
    printf("Please enter year, month, day:");
    scanf("%d,%d,%d",&year,&month,&day);
    printf("yearDay = %d\n",DayofYear(year,month,day));
    return 0;
}
int  DayofYear(int year, int month, int day)
{
    int m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int sum=0;
    sum=sum+day;
    int i;
    if(year%4==0&&year%100!=0||year%400==0)
    {
        if(month<=2)
        {
            for(i=0;i<month-1;i++)
            {
                sum=sum+m[1];
            }
        }
        else
        {
            sum=sum+1;
            for(i=0;i<month-1;i++)
            {
                sum=sum+m[i];
            }
        }
 
    }
    else
    {
        for(i=0;i<month-1;i++)
            {
                sum=sum+m[i];
            }
    }
    return sum;
}
#include<stdio.h>
void monthday (int year,int yearday,int *pmonth,int*pday);
int main()
{
	int year,month,day,yearday;
	scanf("%d%d",&year,&yearday);
	monthday(year,yearday,&month,&day);
	printf("%d,%d",month,day);
	return 0;
}
void monthday(int year,int yearday,int *pmonth,int *pday)
{
	int a,i,m;
	int days[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}};
	if((year%400==0)||(year%100!=0&&year%4==0))
	{
		a=1;
	}
	else
	{
		a=0;
	}
	m=yearday;
	for (i=0;i<12;i++)
	{
		m=m-days[a][i];
		if (m<=0)
		{
			break;
		}
	}
	if (m==0)
	{
		*pmonth=i+1;
		*pday=days[a][i];
	}
	else if (m!=0)
	{
		*pmonth=i+1;
		*pday=m+days[a][i];
	}
}
#include<stdio.h>
#include<stdlib.h>
int dayofyear (int year,int month,int day);
void monthday(int year,int yearday,int *month,int *day);
int main ()
{
	int year,month,day,yearday,choice;
	printf("1.year/month/day--yearday\n""2.yearday--year/month/day\n""3.exit\n""please enter your choice:\n");
	scanf("%d",&choice);
	switch(choice)
	{
		case 1:
			printf("year,month,day");
			scanf("%d %d %d",&year,&month,&day);
			yearday=dayofyear(year,month,day);
		    printf("%d",yearday);
			break;
		case 2:
			printf("year,yearday");
			scanf("%d %d",&year,&yearday);
			monthday(year,yearday,&month,&day);
			printf("%d %d",month,day);
		case 3:
			exit(0);	
	}
	return 0;
}

int dayofyear (int year,int month,int day)
{
	int yearday=0,a,i;
	int days[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}};
	if((year%400==0)||(year%100!=0&&year%4==0))
	{
		a=1;
	}
	else
	{
		a=0;
	}
	if (month==1)
	{
		yearday=day;
	}
	else
	{
		for(i=0;i<month-1;i++)
		{
			yearday+=days[a][i];
		}
		yearday+=day;
	}
	return yearday;
}

void monthday(int year,int yearday,int *pmonth,int *pday)
{
	int a,i,m;
	int days[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}};
	if((year%400==0)||(year%100!=0&&year%4==0))
	{
		a=1;
	}
	else
	{
		a=0;
	}
	m=yearday;
	for (i=0;i<12;i++)
	{
		m=m-days[a][i];
		if (m<=0)
		{
			break;
		}
	}
	if (m==0)
	{
		*pmonth=i+1;
		*pday=days[a][i];
	}
	else if (m!=0)
	{
		*pmonth=i+1;
		*pday=m+days[a][i];
	}
}






上一篇:星期几计算(C++)


下一篇:二十一天好习惯 第一期-10