#include <stdio.h> #include <stdlib.h> #include <string.h>
double max(double x, double y)//比较俩个英语句子的长
{ if(x>=y) return x; else return y;
}double like(char s1[],char s2[])//比较俩个英语句子的相似度
{ size_t len_s1 =0;
char *s_dup = NULL; char* pch = NULL; char* psubstr = NULL; size_t len_s2 =0;
char *s_dup2 =NULL; char * pch2 =NULL;
double count1=0; double count2=0; double count3=0; double z=0;
len_s2 = strlen(s2);
s_dup2 = (char*)malloc(len_s2 * sizeof(char));
if (s_dup2 == NULL){
fputs("Memory allocating error", stderr);}
strncpy(s_dup2, s2,len_s2);
pch2 = strtok (s_dup2, " ,.");
while (pch2 != NULL )//记录录入的英语句子的单词个数
{ count3++; pch2 = strtok (NULL, " ,.");}
len_s1 = strlen(s1);
s_dup = (char*)malloc(len_s1 * sizeof(char));
if (s_dup == NULL){ fputs("Memory allocating error", stderr);}
strncpy(s_dup, s1,len_s1);
pch = strtok (s_dup, " ,."); //把正确的英语句子分成一个个单词
while (pch != NULL)
{ count1++;
psubstr = strstr(s2, pch); //在录入的英语句子s2里找pch
if (psubstr != NULL){ count2++;} pch = strtok (NULL, " ,.");}
printf("原英文句子是:%s\n",s1);
printf("原英文句子单词数有:%d\n\n",(int)(count1-1));
printf("录入英文句子是:%s\n",s2);
printf("录入英文句子单词数有:%d\n\n",(int)(count3-1));
printf("答对英语单词个数有:%d\n",(int)count2);
z=max((count1-1),(count3-1));
printf("正确率是:%.0f%%\n",((count2/z)*100));
free(s_dup);
free(s_dup2);
return 0;}
void main()
{ char s1[] = "there are some book on the table which is green.";
char s2[] = "these are some books in the desk whick is gay.";
like(s1,s2); }
程序我该了 又出现新问题了 ,例如:
原句:you are a boy.
录入:you are a goal.
在运行这俩句话时,在查找原句的a时,它找到的是录入句子的are,判断不相等,就没累加
所以最后相同单词它判断有2个,把a给省略了,求解,怎么判断a是否在录入句中出现? 或者怎么让它连续判断,判断完are不符合,再往后边的单词中查找是否还有相同的单词?
连续判断 用 更新
(1)第一次 psubstr = strstr(s2, pch); 若are a 不成功,要继续
(2) 第2次 psubstr = strstr(psubstr, pch); 若are a 不成功,要继续
(3) 第n次 psubstr = strstr(psubstr, pch); 若are a不成功,...
c++程序 这个程序一般的英语句子能对比相似度,还有问题:book和books...
if ( strcmp(s3,pch)==0) 累加器加1; 这样 book books 就不相等了。}