消息关闭
    暂无新消息!
我利用了基姆拉尔森公式,先写了一个简单的思路结构,又自定义一个函数想让老师看起来高端一些,但是我运行后发现输出的星期和实际不相符,所以我想求大神给看看指点一下,指出一下错误!
#include<stdio.h>
int main(){
int year,month,day,week;
printf("Please enter year,month,day:\n",year,month,day);
scanf("%d,%d,%d",&year,&month,&day);
        if(month==1 || month==2){
month+=12;
year--;
}
week=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;
switch(week)
{
case 0:
printf("Sunday!\n");
break;
case 1:
printf("Monday!\n");
break;
case 2:
    printf("Tuesday!\n");
break;
case 3:
printf("Wednesday!\n");
break;
case 4:
printf("Thursday!\n");
break;
case 5:
printf("Friday!\n");
break;
case 6:
printf("Saturday!\n");
break;
}
flushall();
}

#include<stdio.h>
void JudgementWeekday(year,month,day,week){
        if(month==1 || month==2){
month+=12;
year--;
}
week=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;
switch(week)
{
case 0:
printf("Sunday!\n");
break;
case 1:
printf("Monday!\n");
break;
case 2:
    printf("Tuesday!\n");
break;
case 3:
printf("Wednesday!\n");
break;
case 4:
printf("Thursday!\n");
break;
case 5:
printf("Friday!\n");
break;
case 6:
printf("Saturday!\n");
break;
}
}
int main(){
int year,month,day,week;
char ch;
while(ch!='\048'){
printf("Please enter year,month,day:\n",year,month,day);
scanf("%d,%d,%d",&year,&month,&day);
JudgementWeekday();
flushall();
printf("Type 0 exit, press any key to continue!\n");
ch=getch();
}
}


第一个思路比较简单清晰,第二个我的想法是,建立一个循环,判断一次以后,输入0退出,如果想继续判断日期就按任意键,可惜效果不好,麻烦大神帮忙看看!


1个回答

︿ 1
修改后的代码如下,关键处有注释:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

void JudgementWeekday(int year, int month, int day) {//week是输出值 ,不用放在参数列表中 
int week;
if (month == 1 || month == 2) {
month += 12;
year--;
}
week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
switch (week)
{
case 0:
printf("Sunday!\n");
break;
case 1:
printf("Monday!\n");
break;
case 2:
printf("Tuesday!\n");
break;
case 3:
printf("Wednesday!\n");
break;
case 4:
printf("Thursday!\n");
break;
case 5:
printf("Friday!\n");
break;
case 6:
printf("Saturday!\n");
break;
}
}
int main() {
int year, month, day;
char ch;
while (true) {
printf("Please enter year,month,day:\n");
scanf("%d,%d,%d", &year, &month, &day);
JudgementWeekday(year, month, day);
printf("\nType 0 exit, press any key to continue!\n");
if ((ch = getch()) == '0'){ //输出一次week结果后,判断输入字符是否为0,再进行不同处理。
break;
}
else{
continue;
}
}
}

运行效果如下图: