我花了3个小时试图让这段代码正常工作,但每当我尝试时,我最终都会循环并且控制台不断循环。我已经尝试了所有内容 - 我创建了一个只返回i并重新分配值的函数,但它似乎无法正常工作。
出于某种原因,每当我尝试使用逻辑运算符时,它都不想为我工作。 mes-> hand [x]的值类似于" 2D,4D,3C,5C,6H,7H"等等,而诉讼是" H"," C" ," D"," S"关于卡片。
int newtrick_value(struct Message *mes, int suit) {
int len = strlen(mes->hand);
int x = 0, i = 50, y = 0;
while (i < 63) {
while(x < len) {
if (((mes->hand[x] == i || mes->hand[x] == y) && mes->hand[x+1] == suit)){
if(i>= 58){
return y;
}
return i;
}
x++;
continue;
}
if(i >= 57) {
y = determine_letter(i);
}
i ++;
x = 0;
continue;
}
return 0;
}
答案 0 :(得分:1)
我有以下观察:
您的continue
语句是多余的。你已经处于循环的底部。 (虽然没有坏处。)
您的号码50
等都是指ASCII字符。添加评论。
我能找到的唯一错误是:
if(i >= 57) {
y = determine_letter(i);
}
这应该是if (i>=58)
,因为57 ASCII是9
。收到57时,determine_letter
可能会感到困惑(该功能显然将高于9的卡映射到您的卡编码方案中,而您完全没有提供)。因此,该函数可以在return y;
中返回一个无效值,调用函数无法理解该值并导致控制台循环。
此语句也应放在while (x < len)
之前,因此您首先映射该字符,然后将其与循环增量i
的末尾进行比较,以便比较下一个字符(卡片值)。
应用这些观察结果给出:
int newtrick_value(struct Message *mes, int suit) {
int len = strlen(mes->hand);
int x = 0, i = 50, y = 0;
while (i < 63) {
if (i >= 58) {
y = determine_letter(i);
}
while (x < len) {
if (((mes->hand[x] == i || mes->hand[x] == y) && mes->hand[x+1] == suit)){
if (i >= 58) {
return y;
}
return i;
}
x++;
}
i++;
x = 0;
}
return 0;
}
如果没有调用者,子功能和卡片编码方案的所有细节,我无法给出更多建议。我希望这会有所帮助。
注意:考虑到功能,该功能只返回给定套装的第一张卡的值。这意味着比较卡值的外循环看起来很没用:首先找到第一套,然后得到它的价值更高效:
int newtrick_value(struct Message *mes, int suit) {
int len = strlen(mes->hand);
int x = 0;
while (x < len) {
if (mes->hand[x] == suit)){
return(mes->hand[x-1]);
}
x++;
}
return 0;
}
答案 1 :(得分:0)
作为MWE,这打印&#34;调用确定字母&#34; 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
因此转换&#34; H&#34;给出的套装价值一个整数似乎特别棘手。
我已经假设你在你的结构中代表mes->hand
,但希望这仍然有效。我注意到的一件事是,为什么你作为int
,而不是char
来诉讼?这似乎更合乎逻辑。
因此,如果您进入无限循环,我怀疑mes->hand
可能不会以空值终止,这意味着strlen(mes->hand)
将返回一些未知值,这意味着while(x < len)
似乎永远不会终止。你能检查一下mes->hand
以确保它是一个有效的(和预期的)字符串吗?