C语言中输入年月日,判断这一天是一年的第几天……
1#include<stdio.h>
2#include<stdlib.h>
3/*题目:输入某年某月某日,判断这一天是这一年的第几天*/
4intmain(){
5system("color1F");//设定显示框为蓝底白字
6system("modeconcols=80lines=30");//固定显示框尺寸
7/
8intdays[11];
9intyear,moon,day,i,t1,t2;
10while(1){
11loop:
12printf("请输入年份:");
13scanf("%d",&year);
14if(year%4==0&&year%100!=0||year%400==0)
15days[1]=29;
16elsedays[1]=28;//判断是否闰年
17printf("请输入月份:");
18scanf("%d",&moon);
19printf("请输入日期:");
20scanf("%d",&day);
21if(moon>12||day>31||moon==2&&day>days[1]||moon==4&&day>30||moon==6&&day>30||moon==9&&day>30||moon==11&&day>30)
22{
23printf("输入错误,请重新输入\n");
24gotoloop;//输入数据错误检查
25}
26days[0]=31;
27days[2]=31;
28days[3]=30;
29days[4]=31;
30days[5]=30;
31days[6]=31;
32days[7]=31;
33days[8]=30;
34days[9]=31;
35days[10]=30;
36t1=0;
37for(i=0;i<moon-1;i++)
38t1=t1+days[i];
39t2=t1+day;
40printf("%d年%d月%d日是这一年的第【%d】天\n-----------------------------------------------------------------------------\n",year,moon,day,t2);
41}
42
43/
44return0;
45}
扩展资料
Python语言输入某年某月某日,判断这一天是这一年的第几天
year=int(input('year:\n'))
month=int(input('month:\n'))
day=int(input('day:\n'))
s=0
months=[0,31,59,90,120,151,181,212,243,273,304,334]
if0<=month<=12:
s=months[month-1]
else:
print('dataerror')
s+=day
leap=0
if(year%400==0)or((year%4==0)and(year%100!=0)):
leap=1
if(leap==1)and(month>2):
s+=1
print('itisthe%dthday.'%s)
以2月10日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
源代码:
#include "stdio.h"
#include "stdlib.h"
int main()
{
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\n");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.\n",sum);
return 0;
}
输出
please input year,month,day
2019,02,10
It is the 41th day.
扩展资料
c语言编写程序根据输入的时间分别输出问候语
#include <stdio.h>
int main()
{
int a;
printf("请输入时间,例如:17");
scanf("%d",&a);
if(a<=12&&a>=0) printf("早");
else if(a>12&&a<=14) printf("午");
else printf("晚");
printf("%d",s);
return 0;
2019-07-31
int main()
{
int day,month,year,sum=0,leap;
printf("输入年月日如2019 7 12\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))&&month>2)
sum++;
printf("这是这一年的第%d天。",sum);
return 0;
}
方法2
#include<stdio.h>
int day_of_year(int (*p)[13],int year,int month,int day)
{
int i,leap;
leap=(year%100!=0 && year%4 ==0||year%400 ==0);
for(i=1;i<month;i++)
day+=*(*(p+leap)+i);
return day;
}
main()
{
static int day_tab[][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
printf("%d\n",day_of_year(day_tab,a,b,c));
}
#include<iostream>
using namespace std;
int main()
{
int y, m, d, x, i;
cout << "请输入年,月,日" << endl;
cin >> y >> m >> d;
x = 0;
i = m;
while (i!=0)//从当月开始减1直到等于0循环停止
{
switch (i-1)//当月的天数是d,上个月的天数才是30或31,应根据上个月天数分支
{
case 1:case 3:case 5: case 7: case 8: case 10: case 12: x+=31;break;
case 4:case 6: case 9: case 11: x+=30;break;
case 2:x=((y%4==0 && y%100 != 0) || (y%400==0))?(x+29):(x+28);break;
case 0:x+=d;break;//上月减1是0,表示已经减到了当月,直接加上d即可,注意不能x=d
}
i-- ;
}
cout << "为这一年的第" << x << "天" << endl;
return 0;
}