永发信息网

C语言中国有句俗语叫做“三天打鱼两天晒网”求高手!

答案:1  悬赏:40  手机版
解决时间 2021-08-19 15:51
C语言中国有句俗语叫做“三天打鱼两天晒网”求高手!
中国有句俗语叫做“三天打鱼两天晒网”,这句话用在Tomcat身上正好.原来Tom从2000年1月1日搬到海边后每打三天鱼就要晒两天网.如今以前的邻居老鼠Jerry来看望Tom.请问Jerry会看到Tom在打鱼还是在晒网?
输入要求:
输入数据有多组,每组数据占一行,为Jerry看望Tom的日期,格式为yyyy-mm-dd.输入为“0”时结束,该数据不处理.
输出要求:
如果Tom在给定的日期打鱼,则输出yyyy-mm-dd:he was fishing at that day.;如果Tom在给定的日期晒网,则输出yyyy-mm-dd:he was sleeping at that day..如果对于给定的日期不存在或者早于2000年1月1日,则输出yyyy-mm-dd:Rrror!.
假如输入
2013-10-01
2010-08-30
2012-06-31
2013-05-31
1998-09-12
0
应当输出
2013-10-01:he was fishing at that day.
2010-08-30:he was sleeping at that day.
2012-06-31:Error!
2013-05-31:he was sleeping at that day.
1998-09-12:Error!
最佳答案

这是我的测试结果:可以识别不同的错误种类,包括日期格式错误,日期不存在等(空行也会被识别为错误):
Please type in date as yyyy-mm-dd for each line
the last line should be 0
warning:this program uses gets(),which is unsafe.
输入:
2013-10-01
2010-08-30
2012-06-31
2013-05-31
1998-09-12
nothing
0
输出:
2013-10-01:he was fishing at that day
2010-08-30:he was sleeping at that day.
2012-06-31:Error:date doesn't exist
2013-05-31:he was sleeping at that day.
1998-09-12:Error:date too early
:Error:wrong format
nothing:Error:wrong format
代码:
#include
#include
#include
#define NEW (node *)malloc(sizeof(node))
#define MAX_CHAR_IN_LINE 50
typedef struct node{
char* date;
node* next;
} node;
int lengOfString(char* string)
{
int length=0;
while(string[length]!='\0' && length < MAX_CHAR_IN_LINE)
length ++;
return length;
}
bool checkFormat(char* string)
{
int i=0;
while(string[i])
{
if(i!=4 && i!=7)
{
if(string[i]'9' )
return false;
}
else
{
if (string[i]!='-')
return false;
}
i++;
}
if(i!=10)
return false;
return true;
}
bool isLeapYear(int year)
{
if ((year % 4 == 0) && !(year % 100 == 0))
return true;
else if(year % 400 ==0)
return true;
return false;
}
int daysInMonth(int year,int month)
{
int table[12]={ 31,28,31,30,31,30,31,31,30,31,30,31};
if (isLeapYear(year))
table[1]=29;
return table[month-1];
}
int checkDate(char* string)
{
string[4]='\0';
string[7]='\0';
int year=atoi(string);
int month=atoi(&string[5]);
int day=atoi(&string[8]);
if(month>12 || month < 1 )
return -1;
if( day daysInMonth(year,month))
return -1;
if(year < 2000)
return -2;
int days=0;
int ite_year;
int ite_month;
for (ite_year=2000; ite_yeardate);
if(checkFormat(head->date))
{
int value=checkDate(head->date);
if (value==-1)
printf(Error:date doesn't exist\n);
if (value==-2)
printf(Error:date too early \n);
if (value==0)
printf(he was sleeping at that day.\n);
if (value==1)
printf(he was fishing at that day \n);
}
else
{
printf(Error:wrong format \n);
}
head=head->next;
}
}
int main()
{
printf(Please type in date as yyyy-mm-dd for each line\n);
printf(the last line should be 0\n);
node* head=NEW;
node* tail=head;
while(true)
{
char* input=(char*)malloc(MAX_CHAR_IN_LINE*sizeof(char));
gets(input);
if (strncmp(input,0,MAX_CHAR_IN_LINE)==0)
break;
int str_length=lengOfString(input+1);
char* date= (char*)malloc(sizeof(char)*str_length);
strcpy(date,input);
free(input);
node* entry=NEW;
entry->date=date;
entry->next=NULL;
tail->next=entry;
tail=entry;
}
printList(head->next);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
国外一盲人没声歌唱家,唱没声的,把古典与现
天天都做噩梦 帮忙解解
《童趣》里“为之怡然称快”的之是什么意思?
沧州到黄骅港多少公里,沧州到黄骅坐车要多长
解放区焦作兄弟果业地址在哪里啊
面条怎样煮才会好吃呢?
龙之谷牧师怎么练....有哪几种练法...越详细
请问有没有语速为250左右的英语听力材料(为
那位电脑高手看看那这个怎么办
请问有拜仁7、10号的最新球星号的图片吗
关于释放压力的名言,压力大的危害有哪些?如
求ipad单机游戏
全国二级建造师10年考试成绩何时查询
大学用什么笔记本好,上大学买电脑,笔记本的
邓州市南阳均辉鲜蛋糕在什么地方啊,我要过去
推荐资讯
假如这个世界上没有苍蝇会怎么样?
橄榄油和凡士林是什么东东啊?
谁知道北京哪里可以买到水晶鞋,不是穿的那种
关于父亲生病的名言,形容父亲对女儿生病关心
地下城装备怎么打红字,没有异界气息的装备怎
成语舍什么,什么舍如什么成语
本地存款外地能取吗
2/3a³-a是单项式还是多项式?若是多项式,
水利工程是什么,水务局隶属水利工程管理处是
一个男人在地下五层喝酒等他女朋友,不时的看
秋初无痛人流后第三天洗头了,头发没吹干会对
分析物体在各段时间内分别做何种运动,哪一段
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?