请版主帮忙删帖
本帖最后由 小伊布 于 2022-1-9 10:31 编辑#include <stdio.h>
#include <string.h>
_Bool IS(int year){ //是否是闰年
return (year%4==0 && year%100!=0) || year%400==0;
}
int Year(int year){ //闰年366天,非闰年365天
if(IS(year))
return 366;
else
return 365;
}
int main(){
int a = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
char Month = {
"", "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
char Week = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
int year, month, week, day;
int num, row, i, y;
char M;
while(scanf("%d %s %d", &day, M, &year) != EOF){
num=0;
y=1;
while(y<year){ //公元1年到输入年份之间经过的天数
num += Year(y);
y++;
}
for(i=0; i<=13; i++){ //根据输入的英文月份找到第几月
if(!strcmp(M, Month))
month = i;
}
row = IS(year);
for(i=0; i<=month; i++){ //计算该年份经过的天数
if(i==month)
num += day;
else
num += a;
}
week = num % 7;
printf("%s\n", Week);
}
return 0;
}
代码有时候会连续输出两个字符串,为什么出现这种情况呢?
再问个小白问题,我想debug找到问题,输入后要经过2000多次循环,怎么跳过这一段程序直接调试后面的?如果一开始直接跳到后面debug,就没有输入了。 已找到问题所在,谢谢 我的代码供参考:#include <stdio.h>
#include <string.h>
char *month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char *week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int isLeap(int y){return !(y%4) && y%100 || !(y%400);}
int days(int d, int m, int y){
int res = d;
while(--m){
if(m!=2) res += ((m%2) && (m<8)) || (!(m%2) && (m>7))? 31 : 30;
else res += isLeap(y) ? 29 : 28;
}
while(--y) res += isLeap(y) ? 366 : 365;
return res;
}
int function(){
int d, m, y;
char mth;
if(scanf("%d%s%d", &d, mth, &y) == EOF) return 0;
for(int i = 0; i < 12; i++) if(!strcmp(mth, month)) m = i+1;
printf("%s\n", week);
return 1;
}
int main(){
while(function());
return 0;
}
页:
[1]