关键词不能为空

位置:淮南象牙塔成语大世界 > 成语故事 > C一般发[k]为什么在e、i、y之前就要改为[s],谁可以给...

C一般发[k]为什么在e、i、y之前就要改为[s],谁可以给...

作者:淮南象牙塔
日期:2021-04-22 21:17:14
阅读:

#include
#include #include
void main(){ int i, j; /*定义循环变量i,j*/ int num = 0, count = 0; /*定义输入个数num和判断正确个数count*/ printf(“练习开始,按回车结束\n”); printf(“请输入要生成的字母个数:\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函数,
下面是 输入日期,
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

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

}

    相关推荐

    聚合标签
    玄武门之变后的李渊已经被架空了,他的退休生活是什么样的? 三皇五帝是哪三皇哪五帝?黄帝、炎帝、蚩尤又是什么? 求描写山 梦幻西游安徽一区紫蓬山可以用别的区的号转过去么? 古代神话中,三皇五帝分别是谁? 很吃力的意思一样的成语 趔趔趄趄的反义词 形容被吓得害怕的成语有哪些? “昌”字打一成语是什么? 看图猜成语 一个发光的灯泡上面一个叹号 大神给我掐算,说我有出马仙,可是出马时,仙家上身来了,确不说... 乌黑乌黑的什么填空 男女都属牛结婚会犯冲? 流可以组什么词语 “口里淡出鸟来”这句话是什么意思? 心所同然者如此 完美国际别墅任务中所有NPC的具体位置? 淡泊是什么意思? 用两个边边怎样组词 腾组词有哪些 图片里有一个懂字猜成语 飞黄腾达是褒义词还是贬义词? 关于一什么一什么的成语? 江城子(密州出猎)苏轼 注音 颇,可以组成什么词语? 左边不出头,右边不出头,不是不出头,就是不出头 猜一字:(... 对李白“痛饮狂歌”有什么看法??? 文字开头有什么成语 歹字开头的四字成语 我却总不会歌词 厚字开头的成语接龙 急求郭德纲于谦《揭瓦》台词 带胸 的成语有哪些 人生八苦 成语歹字开头的成语 咋组成语 曹操战袁绍前,操十胜,绍十败是谁提出的? 激动的心情的成语 带有灯字的成语有哪些 好说歹说的成语解释 吻字组四字词语 李渊为什么要退位 什么山什么水都有哪些成语 廉颇蔺相如列传全文翻译 形容有理想的成语 惊弓之鸟的字面意思 “宠辱不惊 忐忑不安的类似词语 什么成语能概括“世有伯乐 用······像那……一样……造句 道什么接成语 康有为有关教育主张的思想根源 为什么?现在的人都那么高傲,爱装逼 我男20岁!没文化,没技术,人又懒,朋友也没几个,感觉日子越... 康有为的介绍 围绕上无瓦片,下无立锥之地写一段文艺的100字 婉转悠扬是什么意思 婉转悠扬是否有错别字 《小时了了》文言文翻译是什么? 求《飞鸥不下》BY回南雀txt百度云资源,谢谢! 四字成语大全集500个 .带有人的成语有哪些. 中国承受的千灾百难有哪些? 情感,生活,日常 关于学习的文章 带有修成语有哪些 完美世界柳神划分在哪里个境界 江城子.密州出猎的主旨是什么 ?