这是一个简单的字谜游戏代码:
#define NP 5
#define LP 10
void anagrammi() {
srand(time (NULL));
char words[NP][LP] = {"cane", "gatto", "gallo", "rana", "ibis"};
typedef struct {
char parola[LP];
int usati[LP];
char anagramma[LP];
}an ;
int a, len, i, j, cont;
char merda[LP], c;
an yoda;
a = rand()%5;
strcpy(yoda.parola, words[a]);
len = strlen(yoda.parola);
for (i = 0; i < len; i++){
yoda.usati[i] = len+2;
}
i = 0;
while (i < len){
cont = 0;
a = rand()%len;
for (j = 0; j < len; j++){
if (a == yoda.usati[j]) cont++;
} if (cont == 0) {
yoda.usati[i] = a;
yoda.anagramma[i] = yoda.parola[a];
i++;
}
}
j = 0;
cont = 0;
yoda.anagramma[i] = '\0';
printf("Anagramma di %s ? ", yoda.anagramma);
c = getchar() ;
while( c != '\n' && j < LP ){
merda[j] = c ;
j++ ;
c = getchar(); }
merda[j] = '\0';
for (i = 0; i < len; i++){
if (merda[i] =! yoda.parola[i]) cont++;
printf("\n%d", cont);
printf("\n%c", merda[i]);
printf("\n%c", yoda.parola[i]);
}
if (cont =! 0) printf("\nFALSE");
else printf ("\nTRUE");
}
程序继续,但是当我插入单词并将其与原始单词进行比较时,即使我输入了正确的单词,最终输出也始终为FALSE。问题也发生在没有\ 0.任何人都可以帮助我吗?
感谢答案,这是一个完美的工作程序:
void anagrammi() {
srand(time (NULL));
char words[NP][LP] = {"cane", "gatto", "gallo", "rana", "ibis"};
typedef struct {
char parola[LP];
int usati[LP];
char anagramma[LP];
}an ;
int a, len, i, j, cont;
char merda[LP], c;
an yoda;
a = rand()%5;
strcpy(yoda.parola, words[a]);
len = strlen(yoda.parola);
for (i = 0; i < len; i++){
yoda.usati[i] = len+2;
}
i = 0;
while (i < len){
cont = 0;
a = rand()%len;
for (j = 0; j < len; j++){
if (a == yoda.usati[j]) cont++;
} if (cont == 0) {
yoda.usati[i] = a;
yoda.anagramma[i] = yoda.parola[a];
i++;
}
}
j = 0;
cont = 0;
yoda.anagramma[i] = '\0';
printf("Anagramma di %s ? ", yoda.anagramma);
c = getchar() ;
while( c != '\n' && j < LP ){
merda[j] = c ;
j++ ;
c = getchar(); }
for (i = 0; i < len; i++){
if (merda[i] != yoda.parola[i]) cont++;
}
i = strlen(merda);
if (cont != 0 || i > len ) printf("\nNON HAI INDOVINATO");
else printf ("\nHAI INDOVINATO");
}
答案 0 :(得分:0)
(cont =! 0)
的 (cont != 0)
- kaylum