我在c语言中的情况如下,结构如下:
struct afreq
{
unsigned char sym;
int freq;
short int left,right;
};
struct afreq data[512] ;
编译编写的代码后,它形成这种类型的结构:
[a 0 0 0]
[b 1 0 0]
[z 1 b a]
[c 2 0 0]
[d 3 0 0]
[z 3 c z]
[e 4 0 0]
[z 6 z d]
[z 10 z e]
第一个元素是“sym”,第二个元素是“freq”,第三个元素是“left”,第四个元素是“right”。
现在我要做的是:
从最后一个元素(即[z 10 z e])开始,我必须转到“左”和“右”都为零的所有元素。我还必须计算它们与最右边元素的距离(就“零”的数量而言,我的意思是假设如果我们看到上面的结构,我们在 sym=“e” 处有 left=right=“0”,所以距离将是 "00",因为它距离最右边的元素有 3 个元素)。
所以输出必须是这样的:
e= 00
d= 000
c= 0000
b= 00000
a= 000000
我的实现它的代码如下(不起作用):
i=dataSize-1; //you can see the output below to know it's value, which is exactly
//corresponding to the element matrix i have taken above at starting.
printf("before i:%d\n");
do{
char temp[30];
strcpy(temp, var);
strcat(temp, "0");
printf("\ntemp: %s ", temp);
printf("sym :%c \n", data[i].sym );
printf("Valuse of i:%d\n",i);
i=i-1;
} while(data[i].left==0 && data[i].right==0);
与之对应的输出是(这不是预期的):
[a 0 0 0] [b 1 0 0] [z 1 b a] [c 2 0 0] [d 3 0 0] [z 3 c z] [e 4 0 0] [z 6 z d] [z 10 z e]
data size 9
before i:2
temp: 0
sym :z //and it prints the last element whereas it's
left and right child are not zero
Valuse of i:8
有人可以解释一下我应该如何更改我的代码以获得所需的输出吗? (我是一名初级程序员)
最佳答案
删除中断
!!!!!!!!!!!!!!!!!!!!!
关于c - while 循环在 c 中未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21183896/