这个就没什么需要注意的了,比较简单。
主要是模块化函数。
//UVa 489 start:2025 5.2. end 5.2
#include <stdio.h>
#include <string.h>
#define maxn 200
char r_word[maxn];
char t_word[maxn];
char text[maxn];
// 该函数作用:通过判断字母与单词重复性更新可显示单词,并更新命的数量;返回单词是否全部猜出来;
int check_word(char *r_word, char *t_word, char a, int *chance)
{
int lenr = strlen(r_word);
//x指r和t是否相同;y指chance是否要-1;
int x, y = 0;
//判断该字符是否出现在rword中
for (int i = 0; i < lenr; i++)
{
if (a == r_word[i])
{
t_word[i] = r_word[i];
y = 1;
}
}
//x指是否全对
x = strcmp(t_word, r_word);
if (!y && x == 0)
{
(*chance)--;
return 1;
}
if (!y && x != 0)
{
(*chance)--;
return 0;
}
if (y && x == 0)
{
return 1;
}
if (y && x != 0)
{
return 0;
}
}
int main()
{
int a = 0;
while (1)
{
memset(r_word, '\0', sizeof(r_word));
memset(text, '\0', sizeof(text));
memset(t_word, '\0', sizeof(t_word));
scanf("%d", &a);
printf("%d\n",a);
if (a == -1)
break;
// 样例输入
scanf("%s", r_word);
scanf("%s", text);
int lent = strlen(text), chance = 7;
// 逐个字母判断,并更新状态;
int i = 0;
for (int j = 0; i < lent; i++)
{
//猜测的字符串中当出现重复字符时,直接-1,不需要执行后面的检测了,while中的条件 i指代的是,保证第一个(i = 0时)不需要执行该操作
while (i && !(memchr(text, text[i], i) == NULL))
{
chance--;
i++;
}
if (i < lent)
{
j = check_word(r_word, t_word, text[i], &chance);
if (j && chance)
{
printf("You win.\n");
break;
}
if (!chance)
{
printf("You lose.\n");
break;
}
}
}
if (chance && i == lent)
printf("You chickened out.\n");
}
return 0;
}
1656

被折叠的 条评论
为什么被折叠?



