这个真棒网站的第一篇文章!
所以,我几乎是C语言的一个重要人物,我正在创建一个程序,可以在用户输入的日期返回。我没有使用任何函数或结构,因为我想首先在C中构建我的基本概念,这里是if-else,switch语句等等... 该计划迅速汇编而成;没有错误,但答案延迟了1天;)我的意思是,当我进入'21 7 1993',这是一个'星期三',我得到'周四”。其他人也一样。 我确信这个概念有问题。请帮我解决。欢迎提出以下代码的评论:
#include<stdio.h>
int temp,yr,yr_new,yr_latest,date,month,i,leap,ord,odd=0;
char flag='0';
int main()
{
clrscr();
puts("Enter the date in the format dd/month-no/yyyy");
scanf("%d %d %d",&date,&month,&yr);
temp=yr/1000;
switch(temp)
{
case 1: if(temp==0)
puts("ERROR");
case 2: if(temp==1)
{
if(yr<1600)
yr_new=yr-1200;
else
yr_new=yr-1600;
break;
}
case 3: if(temp==2)
{ if (yr<2400)
yr_new=yr-2000;
else if(yr<2800)
yr_new=yr-2400;
else
yr_new=yr-2800;
break;
}
}
temp=(yr_new/100);
odd+=(temp*5);
yr_latest=yr_new-(temp*100);
leap=yr_latest/4;
ord=yr_latest-leap;
for(i=1;i<=leap;i++)
odd+=2;
for(i=1;i<=ord;i++)
odd+=1;
/* Leap Year */
if(yr%400==0 && yr%100!=0)
flag='1';
/* month */
switch(month)
{
case 1:
{odd+=(date%7); break; }
case 2:
{odd+=(date%7);break;}
case 3:
{ odd+=((date%7)+3);
if(flag=='1')
odd+=1;
break ;
}
case 4:
{ odd+=((date%7)+6);
if(flag=='1')
odd+=1;
break; }
case 5:
{ odd+=((date%7)+8);
if (flag=='1')
odd+=1;
break;
}
case 6:
{ odd+=((date%7)+11);
if (flag=='1')
odd+=1;
break; }
case 7:
{ odd+=((date%7)+13);
if(flag=='1')
odd+=1;
break; }
case 8:
{ odd+=((date%7)+16);
if(flag=='1')
odd+=1;
break ;}
case 9:
{ odd+=((date%7)+19);
if(flag=='1')
odd+=1;
break;}
case 10:
{ odd+=((date%7)+21);
if(flag=='1')
odd+=1;
break;}
case 11: {
odd+=((date%7)+24);
if(flag=='1')
odd+=1;
break; }
case 12:
{ odd+=((date%7)+26);
if(flag=='1')
odd+=1;
break;
}
}
odd=odd%7;
switch(odd)
{ case 0:puts("Sunday"); break;
case 1:puts("Monday"); break;
case 2:puts("Tuesday"); break;
case 3:puts("wednesday"); break;
case 4:puts("thursday"); break;
case 5:puts("friday"); break;
case 6:puts("Saturday"); break;
default: puts("error!");
}
getch();
return 0;
}
答案 0 :(得分:0)
我认为你的开关盒有问题!尝试以下更改,这是与您的不同逻辑 -
#include<stdio.h>
int temp,yr,yr_new,yr_latest,date,month,i,leap,ord,odd=0;
int main()
{
puts("Enter the date in the format dd/month-no/yyyy");
scanf("%d %d %d",&date,&month,&yr);
temp=yr/1000;
switch(temp)
{
case 1: if(temp==0)
puts("ERROR");
case 2: if(temp==1)
{
if(yr<1600)
yr_new=yr-1200;
else
yr_new=yr-1600;
break;
}
case 3: if(temp==2)
{
if(yr == 2000) // Note this change also
yr_new=yr-1900;
else if (yr<2400)
yr_new=yr-2000;
else if(yr<2800)
yr_new=yr-2400;
else
yr_new=yr-2800;
break;
}
}
temp=(yr_new/100);
odd+=(temp*5);
yr_latest=yr_new-(temp*100);
yr_latest=yr_latest-1; // Here i am leaving the current year in odd days calculation.
leap=yr_latest/4;
ord=yr_latest-leap;
for(i=1;i<=leap;i++)
odd+=2;
for(i=1;i<=ord;i++)
odd+=1;
for(i=1;i<month;i++) // this logic is to calculate the odd days for the current year.
{
switch(i)
{
case 1:
odd+=3;
break;
case 2:
if((yr_latest+1)%4 == 0)
odd+=1;
else odd+=0;
break;
case 3:
odd+=3;
break;
case 4:
odd+=2;
break;
case 5:
odd+=3;
break;
case 6:
odd+=2;
break;
case 7:
odd+=3;
break;
case 8:
odd+=3;
break;
case 9:
odd+=2;
break;
case 10:
odd+=3;
break;
case 11:
odd+=2;
break;
case 12:
odd+=3;
break;
}
}
odd+=date;
odd=odd%7;
switch(odd)
{ case 0:puts("Sunday"); break;
case 1:puts("Monday"); break;
case 2:puts("Tuesday"); break;
case 3:puts("wednesday"); break;
case 4:puts("thursday"); break;
case 5:puts("friday"); break;
case 6:puts("Saturday"); break;
default: puts("error!");
}
return 0;
}
本年度意味着,如果你的输入是'21 7 1993',首先我计算奇数天直到'12 .12.1992',然后我计算其余天数的奇数天数!