求问这个问题的代码解法,( 我用如下的代码无法识别,所有的计数都是零)
//这是一个错误的代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 256
int compare(char* a) {
int cnt = 0;
char str[MAX + 2];
char* s = str, * p;
scanf("%[^\n]",s);
strcat(s, " ");
while (*s) {
p = strchr(s, ' ');
*p = '\0';
if (strcmp(s, a) == 0) {
cnt++;
}
s = p + 1;
}
return cnt;
}
int main() {
int i, j, k, x, y;
scanf("%d", &x);//if、if-else、switch、while、do-while、for
int a[6] = { 0 };
char forn[4] = "for";
char ifn[3] = "if";
char ifeln[8] = "if-else";
char switchn[7] = "switch";
char whilen[6] = "while";
char dowhn[9] = "do-while";
int fori1,ifi1,swi1,ifei1,whi1,dowi1;
for (i = 0; i < x; i++) {
fori1+= compare(forn);
ifi1 += compare(ifn);
ifei1 += compare(ifeln);
swi1 += compare(switchn);
whi1 += compare(whilen);
dowi1 += compare(dowhn);
}
scanf("%d", &y);
int fori2, ifi2, swi2, ifei2, whi2, dowi2;
for (j = 0; j < y; j++) {
fori2 += compare(forn);
ifi2 += compare(ifn);
ifei2+= compare(ifeln);
swi2 += compare(switchn);
whi2 += compare(whilen);
dowi2 += compare(dowhn);
}
if (ifi1 == ifi2 && fori1 == fori2 &&ifei2== ifei1\
&& swi1 == swi2 && whi1 == whi2 && dowi1 == dowi2) {
printf("yes\n");
}
else {
printf("no\n");
}
return 0;
}
//为什么输出不对劲呢?