求问用C语言,给出年月日,计算这是第几天的问题
答案:5 悬赏:60 手机版
解决时间 2021-11-17 03:58
- 提问者网友:送舟行
- 2021-11-16 16:11
求问用C语言,给出年月日,计算这是第几天的问题
最佳答案
- 五星知识达人网友:鱼忧
- 2021-11-16 16:25
#include
int year(int x)
{
int leap;
if(x%4==0)
{
if(x%100==0)
{
leap=0;
}
else
leap=1;
}
else
leap=0;
return (leap);
}
int month(int y)
{
int q=0;
int i=0;
int p[12]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=0;i q=q+p[i];
return(q);
}
void main()
{
int x,y,dd,sum,r;
printf("Please enter yyyy mm dd:\n");
scanf("%d %d %d",&x,&y,&dd);
sum=dd+month(y);
r=year(x);
if(r==1)
{
if(y>2)
sum=sum+1;
}
else
sum=sum;
printf("%d",sum);
}
int year(int x)
{
int leap;
if(x%4==0)
{
if(x%100==0)
{
leap=0;
}
else
leap=1;
}
else
leap=0;
return (leap);
}
int month(int y)
{
int q=0;
int i=0;
int p[12]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=0;i
return(q);
}
void main()
{
int x,y,dd,sum,r;
printf("Please enter yyyy mm dd:\n");
scanf("%d %d %d",&x,&y,&dd);
sum=dd+month(y);
r=year(x);
if(r==1)
{
if(y>2)
sum=sum+1;
}
else
sum=sum;
printf("%d",sum);
}
全部回答
- 1楼网友:洎扰庸人
- 2021-11-16 19:03
- 2楼网友:举杯邀酒敬孤独
- 2021-11-16 17:56
if(x%4==0)
{
if(x%100==0)
{
leap=1;
else (错误提示:misplaced else in function year)
leap=0;
}
else leap=1;
}
else leap=0;
return (leap);
}
第一个错误是if else 语法不对,去掉 else leap=0;
{
if(x%100==0)
{
leap=1;
else (错误提示:misplaced else in function year)
leap=0;
}
else leap=1;
}
else leap=0;
return (leap);
}
第一个错误是if else 语法不对,去掉 else leap=0;
- 3楼网友:低音帝王
- 2021-11-16 17:44
你的代码错误太多,最起码的句式都有问题:scanf("%d",x);、scanf("%d%d%d",&x&y&dd);这都叫什么句式;分支语句逻辑上错误也很多。得先搞清楚它们的基本用法。改跟重写一样,给你写了个作参考——
//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
void main(void){
int year,month,day,count;
int m[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,59,90,120,151,181,212,243,273,304,334}};
char *pw[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
while(1){
printf("Enter the year,month,day(format:y/m/d)\n");
scanf("%d/%d/%d",&year,&month,&day);
if(year%400==0 || year%4==0 && year%100!=0) m[0][1]=29;
else m[0][1]=28;
if(year>0 && month>0 && month<13 && day>0 && day<=m[0][month-1]){
year--;
count=365*year+m[1][month-1]+day+year/4-year/100+year/400;
count+=month>2 && m[0][1]==29;
printf("It is %s.\n",pw[count%7]);
}
else printf("Error! redo: ");
printf("Next(y/n)?");
fflush(stdin);
if((getchar()|0x20)!='y') break;
}
}
//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
void main(void){
int year,month,day,count;
int m[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,59,90,120,151,181,212,243,273,304,334}};
char *pw[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
while(1){
printf("Enter the year,month,day(format:y/m/d)\n");
scanf("%d/%d/%d",&year,&month,&day);
if(year%400==0 || year%4==0 && year%100!=0) m[0][1]=29;
else m[0][1]=28;
if(year>0 && month>0 && month<13 && day>0 && day<=m[0][month-1]){
year--;
count=365*year+m[1][month-1]+day+year/4-year/100+year/400;
count+=month>2 && m[0][1]==29;
printf("It is %s.\n",pw[count%7]);
}
else printf("Error! redo: ");
printf("Next(y/n)?");
fflush(stdin);
if((getchar()|0x20)!='y') break;
}
}
- 4楼网友:不想翻身的咸鱼
- 2021-11-16 17:14
else没有配对的if
p重复定义
i不许被定义在for语句中。这是c不是c++
p定义不清,同2
另外,你可以把计算式写成switch(month)的语句。
switch(month){
case12:days+=30;
case11:days+=31;
case10:days+=30;
case9:days+=31;
case8:days+=31;
case7:days+=30;
case6:days+=31;
case5:days+=30;
case4:days+=31;
case3:days+=28+leap;
case2:days+=31;
case1:days+=date;break;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯