表达式莫名出错
#include <stdio.h>#include <ctype.h>
#include <string.h>
char *s_gets(char *st, int n);
int lower(char *st);
int days(int year, int month, int day);
struct month{
char monthname;
char abbrev;
int days;
int num_month;
};
const struct month months = {
{"January", "Jan", 31, 1},
{"February", "Feb", 28, 2},
{"March", "Mar", 31, 3},
{"April", "Apr", 30, 4},
{"May", "May", 31, 5},
{"June", "Jun", 30, 6},
{"July", "Jul", 31, 7},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12}
};
int main(void)
{
char god;
int year, month, day;
printf("请输入年:");
while(scanf("%d", &year)==1)
{
printf("请输入月:");
if(scanf("%d", &month)==1)
;
else{
int i;
s_gets(god, 10);
for(i=0;i<12;i++)
{
if(strcmp(god, months.monthname)==0||strcmp(god, months.abbrev)==0)
{
month = months.num_month;
break;
}
}
}
printf("请输入日:");
scanf("%d", &day);
printf("您输入了%d年%d月%d日\n", year, month, day);
printf("一年直至这一天需要%d天\n", days(year,month,day));
printf("请输入年:");
}
printf("byb!\n");
return 0;
}
char *s_gets(char *st, int n)
{
char *fa;
char *find;
fa = fgets(st, n, stdin);
if(fa)
{
find = strchr(fa,'\n');
if(find)
*find = '\0';
else
while(getchar()!='\n')
continue;
}
return fa;
}
int days(int year, int month, int day)
{
int ch = 0;
int i ;
months.days = year % 4 == 0 ? 29 : 28;
for(i= 0;i<month-1;i++)
{
ch += months.days;
}
return ch +day;
}
红色那行,我觉得表达式没错误啊
怎么一直提醒报错,我晕了
求大佬帮助{:10_278:} 你写的是const struct,不能改变
所以就报错了
去掉const可以:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char *s_gets(char *st, int n);
int lower(char *st);
int days(int year, int month, int day);
struct month{
char monthname;
char abbrev;
int days;
int num_month;
};
struct month months = {
{"January", "Jan", 31, 1},
{"February", "Feb", 28, 2},
{"March", "Mar", 31, 3},
{"April", "Apr", 30, 4},
{"May", "May", 31, 5},
{"June", "Jun", 30, 6},
{"July", "Jul", 31, 7},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12}
};
int main(void)
{
char god;
int year, month, day;
printf("请输入年:");
while(scanf("%d", &year)==1)
{
printf("请输入月:");
if(scanf("%d", &month)==1)
;
else{
int i;
s_gets(god, 10);
for(i=0;i<12;i++)
{
if(strcmp(god, months.monthname)==0||strcmp(god, months.abbrev)==0)
{
month = months.num_month;
break;
}
}
}
printf("请输入日:");
scanf("%d", &day);
printf("您输入了%d年%d月%d日\n", year, month, day);
printf("一年直至这一天需要%d天\n", days(year,month,day));
printf("请输入年:");
}
printf("byb!\n");
return 0;
}
char *s_gets(char *st, int n)
{
char *fa;
char *find;
fa = fgets(st, n, stdin);
if(fa)
{
find = strchr(fa,'\n');
if(find)
*find = '\0';
else
while(getchar()!='\n')
continue;
}
return fa;
}
int days(int year, int month, int day)
{
int ch = 0;
int i;
months.days = (year % 4 == 0 ? 29 : 28);
for(i= 0;i<month-1;i++)
{
ch += months.days;
}
return ch + day;
} 楼上正解
我又慢了…… 永恒的蓝色梦想 发表于 2020-5-19 21:01
楼上正解
我又慢了……
也谢谢你帮忙找错{:10_278:}
页:
[1]