荆门成语大世界欢迎您的到来
您的位置:荆门成语大世界 > 成语故事 > C语言“三天打鱼两天晒网”问题-C语言中国有句俗语叫做“三天打鱼两天晒网”求高手!!!

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

作者:成语大世界日期:

返回目录:成语故事


#include <stdio.h>
#include <stdlib.h>#include <time.h>
void main(){ int i, j; /*定义循环变量i,j*/ int num = 0, count = 0; /*定义输入个数num和判断正确个数count*/ printf("练习开始,按回车结束\n"); printf("请输入要生成的e799bee5baa6e79fa5e98193e4b893e5b19e362字母个数:\n"); scanf("%d", num);
srand((unsigned)time(NULL)); char *p=(char *)malloc((num+1) * sizeof(char)); if(p != NULL) { for(i = 0; i < num; i++) { p[i] = rand()%26 + 'a'; printf("%c", p[i]); /*输出随机生成的数组*/ } printf("\n"); }
clock_t start, end; /*定义时间变量*/ char *q=(char *)malloc(num * sizeof(char));
if(q != NULL) { start = clock(); /*记录开始时间*/ scanf("%s", q); end = clock(); /*记录结束时间*/ } for(j = 0; j < num; j++) { if(p[j] == q[j]) /*判断匹配数目*/ count++; }
float rate; rate = (float)count / (float)num; /*计算正确率*/ printf("输入正确了%d个字母,正确率为%d%%.\n", count, (int)(rate*100)); printf("共用了%ld秒.\n", (end-start)/1000); free(p); free(q);
system("pause"); }
请放心使用
有问题的话请追问
满意请,


首先定义了一个 date结构体,然后定义days函数,
下面zhidao是 输入日期,
term.month=12;term.day=31;这个是初始化月和日;
yeardays+=days(term);是计算2000到输入的前一年 有多少天;
yeardays+=days(today); 是上面的天数再加上指定的当年到指定日期的天数;
day=yeardays%5; 求余;
下面是判断 并打印结果;
days函数里:flag=day.year%4==0&&day.year%100!=0||day.year%400==0;是计算year是闰年还是平年,0是平年,否是闰年;
for (i=1;i<day.month;i++) 是计算本年从一月一日开始算天数

这是我的测试结果:e69da5e887aae799bee5baa6365可以识别不同的错误种类,包括日期格式错误,日期不存在等(空行也会被识别为错误):
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 <stdio.h>

#include <stdlib.h>

#include <string.h>

#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;

}

相关阅读

关键词不能为空

标签导航

为什么五月初五的艾蒿能祛邪 五月节那天的艾叶有什么说法吗? 农历五月初五这天话说有很多草是可以用来煮水可以防冶百病是怎么... 五月艾草有什么功效拜托了各位 谢谢 为什么身材好的女生买衣服,要打六折 个女人去买胸罩,打六折.打一成语 &求有史以来最好的十大网络小说!& 脑筋急转弯:女人的胸罩打六折打一成语(是高手就来看嘛!) 一个人开始对你很好,后来为什么对不理不睬,发消息给他他也是应... 如果一个人突然对你不理不睬你会怎么办,, 如果一个人对另一个人有时候不理不睬,不闻不问的而又有时候很亲... 如果一个人对你不理不睬,那是什么意思,对你没感觉吗? C语言条件语句查错 C语言 三天打鱼两天晒网(1) C语言“三天打鱼两天晒网”问题 中国有句俗语叫“三天打鱼两天晒网”某人从1990年1月1日... 看图猜成语里面有个此字还有个彼字 成语此()彼() 此什么彼什么是什么成语 成语此什么彼什么 平成语有哪些成语大全 平字的成语有哪些? 带“平”的成语有哪些? 带平字的成语有哪些 瞬息万变的瞬什么意思 瞬息万变的息什么意思 瞬息万变的息是什么意思? 瞬息万变的息是什么意思 京剧的四大流派分别是什么? 京剧四大流派创始人分别是谁 京剧四大流派及代表人物 京剧的四大主要流派是什么 cad打开别人传你的一个文件,自己的模板图层设置等等都没了咋... CAD制图中,别人发给我一份图,修改后保存再打开只有我修改的... CAD打开一个文件后无法编辑修改 cad中出现&quot;图形中一个或多个对象无法保存为指定格式&quot;怎么办 杯赛,联赛,锦标赛,冠军赛,运动会之间有什么区别 锦标赛和杯赛有哪些区别? 杯赛和锦标赛有什么区别? 联赛和杯赛的区别有哪些? “如花美眷,似水流年”是什么意思? 则为你如花美眷 似水流年 是什么意思? 汤显祖“如花美眷,似水流年”这句话有什么申请的含义么? 如花美眷,似水流年有什么含义? 膝前有孝子打一阿拉伯数字 猜无问孔明借箭,配床前有孝子,这两句是指什么生肖 久在床前无孝子和久病床前无孝子是一个意思吗 猜一个数字 床前有孝子 什么牌子的艾条艾柱最好? 什么样的艾条艾柱才算好? 艾绒,艾柱和艾条有什么区别吗 网上买的艾柱 熏艾条之后舌头起黑色血泡怎么回事?
关于我们 联系我们