⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 detect.m

📁 matlab编写的
💻 M
字号:
%检测一种编码是否为唯一可译码
function outinformation=detect(user_code);
%input code
user_code=input('输入待验证的编码:','s');
k=1;
while k<5
    if isempty(user_code)
       user_code=input('输入编码为空,请重新输入:','s');
       k=k+1;
    else
       disp('输入的编码为:');
       user_code
       break;
    end
end
%change the string to a character array
%code=char();
temp=user_code;
for i=1:20
    [token,rem]=strtok(temp);
    if strcmp(rem,'')
        break;
    else
        code{i}=token;
        temp=rem;
    end
end
%code
%compare each element of the first array
L=length(code);
for i=1:L
    for j=i+1:L
        if strcmp(code{i},code{j})
            disp(sprintf('第%d个和第%d个为相同的码%s,所以此编码不是唯一可译码',i,j,code{i}));
            return;
        end
    end
end
newcode=cellstr(code);
newstr1=char('');
newstr2=char('');
for i=1:L
    for j=i+1:L
        if length(newcode{i})<length(newcode{j})
            longstr=newcode{j};
            shortstr=newcode{i};
            n=length(shortstr);
            m=length(longstr);
        elseif length(newcode{i})>length(newcode{j})
            longstr=newcode{i};
            shortstr=newcode{j};
            n=length(shortstr);
            m=length(longstr);
        else
            continue;
        end
        if strncmp(longstr,shortstr,n)
            if m-n==1
                tempstr=longstr(m);
            else
                for k=1:m-n
                    tempstr(k)=longstr(k+n);
                end
            end
           newstr1=char(newstr1,tempstr)
        end      
    end
end
%N=length(newstr1);
newstr=cellstr(newstr1);
% check1=newstr{2}
% check2=newstr{3}
N=length(newstr);
if N==1
    disp('此编码为唯一可译码');
    return;
end
%we have known the first and the second arrary, now we will fine the others
%from the first and the second
while N > 1
    %newstr2=char()
for i=1:1:L
    
    for j=1:1:N
        %newstr2=char()
        %newstr
        %j
        %i
        %newcode{i}
        %check2=newstr{2}
        %check1=newstr{j}
        
        if strcmp(newcode{i},newstr{j})
            disp('此编码不是唯一可译码');
            return;
        elseif length(newcode{i})<length(newstr{j})
            longstr=newstr{j};
            shortstr=newcode{i};
            n=length(shortstr);
            m=length(longstr);
        elseif length(newcode{i})>length(newstr{j})
            longstr=newcode{i};
            shortstr=newstr{j};
            n=length(shortstr);
            m=length(longstr);
        else
            continue;
        end
        tempstr='';
        if strncmp(longstr,shortstr,n)
            if m-n==1
                tempstr=longstr(m);
            else
               for k=1:(m-n)
                   %k
                   tempstr(k)=longstr(k+n);
                   %tempstr
               end
            end
               newstr2=char(newstr2,tempstr)%newstr2 are the other arrays
               %newstr=cellstr(newstr2);
               %str=newstr2           
        end
    end    
end
newstr=cellstr(newstr2);
%newstr=cellstr(str);
N=length(newstr);
newstr2=char('');
end

disp('子集合中没有与用户输入的编码相同的码字,因此编码为唯一可译码');
return; 

    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -