作为 MWE,这会将“calling determine letter”打印到控制台 6 次:
#include <stdio.h>
#include <string.h>
int newtrick_value(char* hand, int suit) {
int len = strlen(hand);
int x = 0, i = 50, y = 0;
while (i < 63)
{
x = 0;
while(x < len)
{
printf("i%d, y%d, suit %d, hand[x] %d, hand[x+1] %d\n", i, y, suit, hand[x], hand[x+1]);
if (((hand[x] == i || hand[x] == y) && hand[x+1] == suit))
{
if(i>= 58)
{
return y;
}
return i;
}
x++;
}
if(i >= 57)
{
printf("Calling determine letter\n");
//y = determine_letter(i);
}
i++;
}
return 0;
}
int main(void)
{
newtrick_value("2H,3H", (int)"H");
return 0;
}
据我所知,我没有更改任何逻辑(只是一些格式)。它不应该给出这种行为,因为花色和价值应该匹配。
奇怪的是,打印结果如下:
i50, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i50, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i50, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i50, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i50, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i51, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i51, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i51, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i51, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i51, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i52, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i52, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i52, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i52, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i52, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i53, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i53, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i53, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i53, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i53, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i54, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i54, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i54, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i54, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i54, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i55, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i55, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i55, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i55, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i55, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i56, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i56, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i56, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i56, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i56, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i57, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i57, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i57, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i57, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i57, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i58, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i58, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i58, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i58, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i58, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i59, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i59, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i59, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i59, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i59, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i60, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i60, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i60, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i60, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i60, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i61, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i61, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i61, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i61, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i61, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i62, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i62, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i62, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i62, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i62, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
因此,将“H”转换为整数给出的花色值似乎特别古怪。
我已经假设您在结构中如何表示 mes->hand,但希望这仍然成立。不过我注意到的一件事是,你为什么要以int 而不是char 的身份传递西装?这似乎是一件更合乎逻辑的事情。
因此,如果您进入无限循环,我怀疑mes->hand 可能不是以空值结尾的,这意味着strlen(mes->hand) 将返回一些未知值,这意味着while(x < len) 可能看起来就像它永远不会终止一样。您能否检查 mes->hand 以确保它是有效(和预期的)字符串?