满意答案
Prearnercal
2013.05.02
采纳率:40% 等级:12
已帮助:8541人
没有判断输入不合常识的情况
#include
#include
#include
int main()
{
int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
printf("请输入年月日(YYYY-MM-DD):");
char s[11] = {'\0'},year[5] = {'\0'},month[3] = {'\0'},day[3] = {'\0'};
int y,m,d,sum = 0;
scanf("%s", &s);
int size = strlen(s);
for(int it = 0,temp = 0; it < size; it++,temp++)
{
if(s[it] != '-')
{
year[temp] = s[it];
continue;
}
else
break;
}
it++;
temp = 0;
for(; it < size; it++,temp++)
{
if(s[it] != '-')
{
month[temp] = s[it];
continue;
}
else
break;
}
it++;
temp = 0;
for(; it < size; it++,temp++)
{
day[temp] = s[it];
}
y = atoi(year);
m = atoi(month);
d = atoi(day);
if(m > 2 && y % 4 == 0 || (y % 100 == 0 && y % 400 == 0))
sum += 1;
for(int i = 0; i < m - 1; i++)
sum += a[i];
sum += d;
printf("这是%d年的第%d天\n",y,sum);
return 0;
}
00分享举报