Demo entry 6741255

haiming

   

Submitted by anonymous on May 15, 2018 at 03:30
Language: C. Code size: 4.1 kB.

function varargout = hamming(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @hamming_OpeningFcn, ...
                   'gui_OutputFcn',  @hamming_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before hamming is made visible.
function hamming_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
function varargout = hamming_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;
% --- Executes on button press in Ham_Encode.

function [B,n,r]=Ham_Encode_Callback(hObject, eventdata, handles)%±àÂë³ÌÐò

DataS = get(handles.Input_Datas,'String');
k = length(DataS);%Êý¾Ý³¤¶È
for i = 1 : k
    Data(i) = str2num(DataS(i));
end

for i=1:k
        if (Data(i)~=0&&Data(i)~=1)
            set(handles.Code_O,'String', 'Please Input Right Code!');
            return;
        end
end

for i = 0:100
    if(2^i -1-i >= k)
        r = i;  %УÑéλÊý
        break;
    end
end
n = k + r ;%±àÂ볤¶È
for i = 1:r  %УÑéλËùÔÚλÊý
    P(i) = 2^(i-1);
end
j=1;
for i = 1:n   %Çó±àÂëÐòÁÐ
    if(length(find(P==i))==0)
        Code(i)=Data(j);
        j=j+1;
    else
        Code(i) = 0;
    end
end
B=zeros(n,r);%Çó¾ØÕó
for i = 1:n
    t= dec2bin(i);
    for j=1:length(t)
        B(i,r-length(t)+j)=bin2dec(t(j));
    end
end
L=Code*B;
Code_J=Code;
Code_O=Code;
for i = 1 : r   %żУÑé
    if(mod(L(i),2)==0)
        s = 2^(r-i);
        Code_O(s) = 0;
        Code_J(s) = 1;
    else
        s = 2^(r-i);
        Code_O(s) = 1;
        Code_J(s) = 0;
    end
end
set(handles.Code_O,'String',  num2str(Code_O));
set(handles. Code_J,'String',  num2str(Code_J));
guidata(hObject, handles); % ¸üнṹÌå

% --- Executes on button press in Ham_Decode.
function Ham_Decode_Callback(hObject, eventdata, handles)%½âÂë³ÌÐò
[B,n,r] = Ham_Encode_Callback(hObject, eventdata, handles);
Right='Right£¡';
Wrong='Wrong£¡';
dcCodes = get(handles.dcCodes,'String');
if(length(dcCodes)~=n)
     set(handles.Judge,'String', Wrong);
     return;
end
if(get(handles.Sel_O,'value'))
    sel=0;
else
    sel=1;
end
for i = 1 : n
    dcCode(i)=str2num(dcCodes(i));
end
C=dcCode*B;
C=mod(C,2);
if(sel==0)
    l=zeros(1,r);
    if(l==C)
       set(handles.Judge,'String', Right);
    else
      numw=num2str(bin2dec(num2str(C)));
      Wrongs = strcat(Wrong,strcat('  The ',strcat(num2str(numw),'th Wrong!')));
      set(handles.Judge,'String', Wrongs);
    end
else
    l=ones(1,r);
    if(l==C)
       set(handles.Judge,'String', Right);
    else
       numw=num2str(7-bin2dec(num2str(C)));
       Wrongs = strcat(Wrong,strcat('  The ',strcat(num2str(numw),'th Wrong')));
      set(handles.Judge,'String', Wrongs);
    end
end
% --- Executes on button press in Sel_O.
function Sel_O_Callback(hObject, eventdata, handles)
if(get(handles.Sel_J,'value'))
    set(handles.Sel_J,'value',0);
end
guidata(hObject, handles);
    
function Sel_J_Callback(hObject, eventdata, handles)
if(get(handles.Sel_O,'value'))
    set(handles.Sel_O,'value',0);
end
guidata(hObject, handles);
    
function Input_Datas_Callback(hObject, eventdata, handles)

function Input_Datas_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function dcCodes_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function dcCodes_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).