嗨,我这里有这段代码
int calMode(RECORD list[], int count)
{
int tempMode = 1;
int i = 1, j, k;
int current = 0;
while ( i <= count)
{
k = 1;
if (list[current].score == list[current + i].score)
{
k++;
i++;
}
printf("%d:", list[current].score);
for(j = 0; j <= k ; j++)
{
printf("*");
}
printf("\n");
current = current + k;
i++;
}
return tempMode;
}
我认为代码的逻辑是正确的,为什么它会进入无限循环?
任何人都可以建议一种修复此代码的方法吗?假设在进入函数calMode之前对数据列表进行排序,我认为for循环存在问题
我编辑了代码知道我的输出是
60
66
71
71
72
75
79
82
82
82
91
size is: 12
73.50
60:**
66:*
71:*
71:*
72:*
75:*
79:*
82:*
82:*
82:*
91:*
输出错误但不再处于无限循环
答案 0 :(得分:3)
if (list[current].score == list[i].score)
{
k++;
i++;
}
如果此测试错误,i
永远不会递增,这是无限循环。
答案 1 :(得分:1)
FIX E.g。
int calMode(RECORD list[], int count){
int tempMode = 1;
int i, k;
int current = 0;
while (current < count){
printf("%d:", list[current].score);
for(k=0; (i=current + k) < count ; ++k){
if(list[current].score == list[i].score)
printf("*");
else
break;
}
printf("\n");
current = current + k;
}
return tempMode;
}
答案 2 :(得分:0)
根据你的代码,如果第0个元素和第1个元素的分数不相等,它将以无限循环结束。我认为如果声明也应该在一些增量条件之外。 [我不知道你的逻辑,这只是猜测]