c语言(输入年月日,算这是一年中的第几天)

如题所述

1、首先打开vc6.0, 新建一个vc项目。

2、添加头文件和main函数

3、定义day、month、year、sum、leap 五个变量。

4、使用 scanf给定义的变量赋值。

5、使用switch语句,先计算某月以前的月份的总天数。

6、使用sum, 加上某天的天数。

7、判断是不是闰年。

8、如果是闰年且月份大于2, 总天数应该加一天。

9、使用printf打印sum。

10、运行程序。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-03-30
首先,在main前要有void(返回值为空),其次,你定义的n没有用到,而且你那getch()没有定义,放在那干嘛的?
#include
len_of_year(int
year)
{
int
z;
if
(year%4==0&&year%100!=0||year%400==0)
z=1;
else
z=0;
return
z;
}
len_of_month(int
year,int
month)
{
int
a;
if
(month==2)
if
(len_of_year(year)==1)
a=29;
else
a=28;
else
if
(month==4||month==6||month==9||month==11)
a=30;
else
a=31;
return
a;
}
len_of_days(int
year,int
month,int
date)
{
int
n,month_days;
for(n=1,month_days=0;n
month_days+=len_of_month(year,month);
month_days+=date;
return
month_days;
}
void
main()
{
int
year,month,date,days;
printf("please
input
year,month,date:");
scanf("%d%d%d",&year,&month,&date);
days=len_of_days(year,month,date);
printf("\n%d
%d
%d
is
the
%d
in
%d\n",year,month,date,days,year);
}
我没试过,如还有错,就是算法的问题,我觉得是能行的。
第2个回答  2019-07-04
#include
class
date
{
public:
date(){}
void
set(int
p1,int
p2,int
p3)
{
year=p1;month=p2;day=p3;
}
int
year,month,day;
};
int
a[]={31,28,31,30,31,30,31,31,30,31,30,31},*p1,*p2,*p3,i=1,k=0;
int
main()
{
int
days(date
k);
date
m,n;
int
p1,p2,p3;
cout<<"请依次输入年·月·日:"<
cin>>p1>>p2>>p3;
if(p1%4==0&&p1%100!=0||p1%400==0)
a[1]=29;
if(p2>12||p3>a[p2-1])
cout<<"您的输入有误!"<
else
m.set(p1,p2,p3);
cout<<"这天是该年的第"<
return
0;
}
int
days(date
k)
{int
i,c=0;
for(i=0;i
c+=a[i];
c+=k.day;
return
c;
}
这个应该可以的,我试过了
第3个回答  2019-05-16
1、先定义每个月的天数,2月按28天算输入年月日后,根据年判断是否闰年(闰年加1天),再从1月加到当月的前一月,再加上日期就可以了
2、例程:
#include
int
month[12]
=
{31,28,31,30,31,30,31,31,30,31,30,31};
main()
{
int
yy,mm,dd,
days;
int
i;
printf("input
year:");
scanf("%d",
&yy);
printf("input
month:");
scanf("%d",
&mm);
printf("input
day:");
scanf("%d",
&dd);
/*
如果大于2月,要做闰年的判断
*/
if(
mm
>
2
&&
((year%4==0
&&
year%100!=0)
||
year%400==0)
)
days
=
1;
else
days
=
0;
/*
加上前面各整月的天数
*/
for(i
=
0;
i
<
mm-1;
i++)
days
+=
month[i];
/*
加上日数
*/
days
+=
dd;
printf("This
is
the
%d
day
of
year
%d!!\n",
days,
yy);
}
//输入:1999
2
1
//输出:This
is
the
32
day
of
year
1999!!
相似回答