输入年月日,计算该日期是该年第几天(注意平年闰年)c语言编程怎么编?
#include<stdio.h>
int main()
{
int Total = 0,Day = 0,Month = 0,Year = 0,Leap = 0;
printf("Please input: Year Month Day\n");
scanf("%d %d %d",&Year,&Month,&Day);
switch(Month)
{
case 1:Total=0;break;
case 2:Total=31;break;
case 3:Total=59;break;
case 4:Total=90;break;
case 5:Total=120;break;
case 6:Total=151;break;
case 7:Total=181;break;
case 8:Total=212;break;
case 9:Total=242;break;
case 10:Total=273;break;
case 11:Total=303;break;
case 12:Total=334;break;
default:printf("error\n");break;
}
Total=Total+Day;
if( Month > 2 && ( (Year%4 == 0 && Year%100 != 0) || (Year%400) == 0 ) )
{
Leap=1;
}
Total=Total+Leap;
printf("%d:%d:%d is %dth Day\n", Year, Month, Day, Total);
return 0;
}
这个你要看不懂简单的如下
#include <stdio.h>
main()
{
while(1)
{
int year,month,day,time,m;
printf("please input: year month day\n");
scanf("%d %d %d",&year,&month,&day);
switch (((year%4==0)&&(year%100!=0))||(year%400==0))
{
case 1:
{
switch (month)
{
case 1: time=day;break;
case 2: time=31+day;break;
case 3: time=31+29+day;break;
case 4: time=31*2+29+day;break;
case 5: time=31*2+29+30+day;break;
case 6: time=31*3+29+30+day;break;
case 7: time=31*3+29+30*2+day;break;
case 8: time=31*4+29+30*2+day;break;
case 9: time=31*5+29+30*2+day;break;
case 10: time=31*5+29+30*3+day;break;
case 11: time=31*6+29+30*3+day;break;
case 12: time=31*6+29+30*4+day;break;
}
};break
default:
{
switch (month)
{
case 1: time=day;break;
case 2: time=31+day;break;
case 3: time=31+28+day;break;
case 4: time=31*2+28+day;break;
case 5: time=31*2+28+30+day;break;
case 6: time=31*3+28+30+day;break;
case 7: time=31*3+28+30*2+day;break;
case 8: time=31*4+28+30*2+day;break;
case 9: time=31*5+28+30*2+day;break;
case 10: time=31*5+28+30*3+day;break;
case 11: time=31*6+28+30*3+day;break;
case 12: time=31*6+28+30*4+day;break;
}
}
}
printf("%d:%d:%d is %d day\n",year,month,day,time);
}
}
2013-10-21
{
int day,month,year,sum,leap;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month)/*先计算某月以前月份的总天数*/
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");break;
}
sum=sum+day; /*再加上某天的天数*/
if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
leap=1;
else
leap=0;
if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
printf("It is the %dth day.",sum);}
int leap(int year)
{
int lp;
lp=(year%4==0&&year100!=0||year%400==0)?1:0;
return lp;
}
int month_days(int year,int month)
{
int d;
switch(month)
{
case 1;
case 3;
case 5;
case 7;
case 8;
case 10;
case 12:d=31;break;
case 2:d=leap(year)?29:28;break;
default :d=30;
}
return d;
}
int days(int year,int month,int day)
{
int i,ds=0;
for(i=0;i<month;i++)
ds=ds+month_days(year,i);
ds=ds+day;
return ds;
}
void main()
{
int year,month,day,t_day;
printf("Input year-month-day:\n");
scanf("%d-%d-%d",&year,&month,&day);
t_day=days(year,month,day);
printf("%d=%d-%d is %dth day of the year !\n",year,month,day,t_day);
}
//这种方法是比较简单的。
package test4;
import java.util.Scanner;
public class day3_2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("输入年份:");
int year=input.nextInt();
System.out.println("输入月份:");
int mon=input.nextInt();
System.out.println("输入天数:");
int day=input.nextInt();
int sum=0;
if(((year%4==0) && (year%100 !=0)) || (year%400 == 0)) {
if(mon<3) {
sum=day+(mon-1)*31;
System.out.println("这是今年的第"+sum+"天");
}
if(mon>=3 && mon<=7) {
if(mon%2!=0) {
sum=(mon-1)/2*(31+30)+day-1;
System.out.println("这是今年的第"+sum+"天");
}else {
sum=(mon-1)/2*30+mon/2*31+day-1;
System.out.println("这是今年的第"+sum+"天");
}
}
if(mon>7 && mon<12) {
sum=(mon+1)/2*31+(mon-1)/2*30+day-1;
System.out.println("这是今年的第"+sum+"天");
}
if(mon==12) {
sum=335+day;
System.out.println("这是今年的第"+sum+"天");
}
}else {
if(mon<3) {
sum=day*mon+(mon-1)*(day+31);
System.out.println("这是今年的第"+sum+"天");
}
if(mon>=3 && mon<=7) {
if(mon%2!=0) {
sum=(mon-1)/2*(31+30)+day-2;
System.out.println("这是今年的第"+sum+"天");
}else {
sum=(mon-1)/2*30+mon/2*31+day-2;
System.out.println("这是今年的第"+sum+"天");
}
}
if(mon>7 && mon<12) {
sum=(mon+1)/2*31+(mon-1)/2*30+day-2;
System.out.println("这是今年的第"+sum+"天");
}
if(mon==12) {
sum=334+day;
System.out.println("这是今年的第"+sum+"天");
}
}
}
}