永发信息网

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

答案:1  悬赏:20  手机版
解决时间 2021-04-02 02:11
C语言中国有句俗语叫做“三天打鱼两天晒网”求高手!!!
最佳答案
这是我的测试结果:可以识别不同的错误种类,包括日期格式错误,日期不存在等(空行也会被识别为错误):
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]<'0' || 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 <1 || 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_year<=year;ite_year++)

{

for(ite_month=1;ite_month< (ite_year==year ? month : 13);ite_month++)

{

days+= daysInMonth(ite_year,ite_month);

}

}

days+=day;

int remainder=days%5;

if(remainder==4 || remainder==0)

return 0;//sleeping

else

return 1;//fishing

}

void printList(node* head)

{

while(head!=NULL)

{

printf("%s: ",head->date);

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;

}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
男人为什么天天想要
历史上康熙妃子有明朝的吗,李易欢是康熙的妃
dnf跨六拍卖行合了吗
马诗其四的意思!急用啊!
刚开始用韩束现在脸上起了斑点是怎么回事
郭京飞表白遭回怼是怎么回事 郭京飞向谁表白
狗狗拉血尿是什么原因
木屋可以建在平房上面吗n
冬季蓬蓬裙搭配什么鞋子好看
拜泉县齐齐哈尔鑫东方菜馆地址是什么,有没有
支付宝买家已经付款,卖家一直不发货怎么办?
在乐理中,各音名(C.D.E.F.G.A.B)间的音程关
王者荣耀百里守约特工魅影值得买吗 百里守约
用了瑾蜜爱人能当时做房事吗
吃下食品蜡纸有害吗?
推荐资讯
为什么我按摩眉毛中部(鱼腰穴)上边,会感觉
重庆市六院电话多少,业务洽谈室
想出道,怎么做
给说说a货MCM包包质量怎么样多少钱
李东白是李白吗 那李东白又是谁?
配电房做高压预试,一般要做哪些项目,有哪些
日记110字以下
oppox907手机固件存储目录根目录在哪里
“苍天饶过谁”的前一句是什么?
黑民族都叫什么名字
男友早上给你发个8是什么意思?
营口的知名人物
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?