4.4基于switch语句的译码器

时间:2025-01-23 17:32:56

Q:已知前缀码如右图所示,求0/1字符串“001011101001011001”相对应的译码。

a b c
1 01 001
#include<iostream>
#include<cstdio>
using namespace std; int f(char *str,int n) {
int i=0;
while(i<18) {
switch(str[i]) {
case '1': printf("a"); break;
case '0': {
i++;
switch(str[i]) {
case '1':printf("b"); break;
case '0': {
i++;
switch(str[i]) {
case'1':printf("c"); break;
}
break;
} }
break;
}
}
i++;
}
}
int main()
{
char str[18]={001011101001011001};
f(str,18);
return 0;
}

  出现了如下的结果:4.4基于switch语句的译码器