作者:成语大世界日期:
返回目录:成语故事
#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;
}