C语言计算某年某月某日 是该年的第几天
发布网友
发布时间:2023-05-18 10:55
我来回答
共5个回答
热心网友
时间:2023-09-30 16:52
#include
void main()
{
int y,m,d;
int sum=0;
int flog;//标识是否是闰年
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&y,&m,&d);
if(( y%4==0 && y%100!=0 ) || y%400==0)
flog=1;
else
flog=0;
while((y<0)||(m>12||m<0)||(d<0||d>31)||((m%2==0)&&(d==31))||((flog==1)&&(m==2)&&(d>28)))
{
printf("input errro!\n");
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&y,&m,&d);
}
switch (m-1)
{
case 12: sum+=31;
case 11: sum+=30;
case 10: sum+=31;
case 9: sum+=30;
case 8: sum+=31;
case 7: sum+=31;
case 6: sum+=30;
case 5: sum+=31;
case 4: sum+=30;
case 3: sum+=31;
case 2:
if(flog==1)
//这里改一下
sum+=28;
else
sum+=30;
case 1: sum+=31;break;
//这里加上一个break语句
default:printf("非法月份\n");
break;
}
sum=sum+d;
if(flog==1)
printf("%d is leap year!\n",y);
printf("这一天是这年的第%d天",sum);
}
热心网友
时间:2023-09-30 16:53
你的程序有2处错误,已改正,win-tc下运行正确。
#include<stdio.h>
#include<string.h>
#include<conio.h>
int
leap(int
year)
{
int
leap1;
if(year%400==0||(year%4==0&&year%100!=0))
leap1=1;
else
leap1=0;
return
leap1;
}
int
main()
{
int
year,month,day,sum1=0,sum,i;
int
monthday;
printf("Input
the
date:
");
scanf("%d%d%d",&year,&month,&day);
for(i=1;i<month;i++)
/*
月份是从1开始的
*/
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
monthday=31;
else
if(i==4||i==6||i==9||i==11)
monthday=30;
else
{
if(leap(year)==1)
monthday=29;
/*
你搞反了,闰年是29天
*/
else
monthday=28;
}
sum1=sum1+monthday;
}
sum=sum1+day;
printf("%d",sum);getch();
}
热心网友
时间:2023-09-30 16:53
算法很乱 看不出 我给你三个
//===================================================================
//计算日期是那一天
//二维数组的算法
//===================================================================
#include<iostream>
using namespace std;
int b[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 theday(int,int,int);
bool leadyear(int);
//-------------------------------------------------------------------
void main(){
for(int y,m,d;cin>>y>>m>>d;)
cout<<"the day is "<<theday(y,m,d)<<" of the day."<<endl;
}//------------------------------------------------------------------
bool leadyear(int year){
if(year%4==0 && year%100!=0||year%400==0)
return true;
else
return false;
}//-------------------------------------------------------------------
int theday(int y,int m,int d){
int total=0;
if(leadyear(y)){
for(int i=0;i<m-1;i++)
total+=b[1][i];
}
else{
for(int i=0;i<m-1;i++)
total+=b[0][i];
}
return (total+d);
}//====================================================================
//===================================================================
//计算日期是那一天
//两个算法:theday与theday2
//===================================================================
#include<iostream>
using namespace std;
//-------------------------------------------------------------------
int theday(int ,int ,int);
int theday2(int ,int ,int);
//-------------------------------------------------------------------
void main(){
for(int y,m,d;cin>>y>>m>>d;){
cout<<"那一天是那一年的第"<<theday(y,m,d)<<"天"<<endl;
cout<<"那一天是那一年的第"<<theday2(y,m,d)<<"天"<<endl;
}
}//------------------------------------------------------------------
int theday(int year,int month,int day){
int total=0;
switch(month){
case 1:total=day;break;
case 2:total=day+31;break;
case 3:total=day+59;break;
case 4:total=day+90;break;
case 5:total=day+120;break;
case 6:total=day+151;break;
case 7:total=day+181;break;
case 8:total=day+212;break;
case 9:total=day+243;break;
case 10:total=day+273;break;
case 11:total=day+304;break;
case 12:total=day+334;break;
}
if((year%4==0 && year%100!=0 || year%400==0) && month>2)
total=total+1;
return total;
}//------------------------------------------------------------------
int theday2(int year,int month,int day){
int total=0;
if(month>0)
total=day;
if(month>1)
total=total+31;
if(month>2)
total=total+28;
if(month>3)
total=total+31;
if(month>4)
total=total+30;
if(month>5)
total=total+31;
if(month>6)
total=total+30;
if(month>7)
total=total+31;
if(month>8)
total=total+31;
if(month>9)
total=total+30;
if(month>10)
total=total+31;
if(month>11)
total=total+30;
if((year%4==0 && year%100!=0 || year%400==0) && month>2)
total=total+1;
return total;
}//================================================================
热心网友
时间:2023-09-30 16:54
你的程序有2处错误,已改正,win-tc下运行正确。
#include<stdio.h>
#include<string.h>
#include<conio.h>
int leap(int year)
{ int leap1;
if(year%400==0||(year%4==0&&year%100!=0))
leap1=1;
else
leap1=0;
return leap1;
}
int main()
{
int year,month,day,sum1=0,sum,i;
int monthday;
printf("Input the date: ");
scanf("%d%d%d",&year,&month,&day);
for(i=1;i<month;i++) /* 月份是从1开始的 */
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
monthday=31;
else if(i==4||i==6||i==9||i==11)
monthday=30;
else
{
if(leap(year)==1)
monthday=29; /* 你搞反了,闰年是29天 */
else
monthday=28;
}
sum1=sum1+monthday;
}
sum=sum1+day;
printf("%d",sum);getch();
}
热心网友
时间:2023-09-30 16:54
for里面因该是i=1;i<mouth;i++
因为你的0月算在2月里了