早教吧作业答案频道 -->其他-->
C语言中国有句俗语叫做“三天打鱼两天晒网”求高手!中国有句俗语叫做“三天打鱼两天晒网”,这句话用在Tomcat身上正好.原来Tom从2000年1月1日搬到海边后每打三天鱼就要晒两天网.如今以前
题目详情
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!
中国有句俗语叫做“三天打鱼两天晒网”,这句话用在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;
}
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;
}
看了 C语言中国有句俗语叫做“三天...的网友还看了以下:
一打点计时器固定在倾角为θ的斜面上,一小车拖着穿过打点计时器的纸带从斜面上滑下,打出的纸带的一段为 2020-05-17 …
某地通信公司的收费标准有两种:(1)每月打不打电话都要交18元月租费,然后每打电话1分钟交费0.1 2020-06-16 …
)某商店有A种练习本出售,每本零售价为0.30元,一打(共12本)售价为3.00元买10打以上)某 2020-06-16 …
某商店练习本每本零售价0.6元,一打(12本)售价6元,买10打以上每打还可以按5.44元付款.( 2020-06-16 …
某商店有A种练习本出售,每本零售价为0.3元,一打(12本)售价为3元某商店有A种练习本出售,每本 2020-06-16 …
某市打市内电话的收费标准是;前3分钟0.2元不满3分钟按3分钟计算,以后每打一分钟加0.1元;打长 2020-07-28 …
本地通话费的意思是不是拨打.接听都一样?在本地与中国移动客户本地通话费每分钟0.15元,与其他客户通 2020-11-21 …
1.非节假日7—21时的市话费为;钱3分钟0.2元,以后每分钟0.1元,王叔叔在非节假日的上午8时打 2020-12-14 …
某飞机起飞前,在跑道上加速滑行,加速度是每秒4.0米2次方,滑行了20秒打到起飞速度,求飞机起飞时的 2020-12-23 …
如图是某同学连接的电路:(1)如果合上开关,将出现的不良后果;(2)请你只拆除一根导线,组成串联电路 2021-01-13 …