سلام
شرمنده همه هستم كه دير به دير ميام ، واقعا سرم شلوغه و نميتونم زياد بيام
خوب توي چند پست چنديم كد از برنامه نويسي مطلب ميزارم كه خيلي ها درخواست داشتن .
كد اولي كه ميزارم كد هافمن هست كه بدرد ميخوره . براي دريافت كد به ادامه مطلب بريد .
كد :
========
clc
clear all
close all
a=input('Please Enter The String For Haffman Coding: ','s');
st=a;
temp=0;%temp for counter and st for stop repeating of counter.
len=length(a);sc=0;
if len~=0
s1=a(1);ii=0;
for j=1:len
s=a(j);
temp=0;
for i=1:len
if (s==a(i)& j==1)
temp=(temp+1);
end
if (s==a(i)& j~=1 & a(i)~=s1)
temp=(temp+1);
a(i)=s1;
end
end% end of for i
if temp~=0
ii=(ii+1);
sc(1,ii)=temp;
sv(1,ii)=s;
end
end% end of for j <<>>
end%end of if for trase not free.
len1=length(sc);
hf(1,:)=sc;
if len1<=1
hftree=hf
else
for j=1:len1
%select s and s1 that not zero.
s=max(hf(j,:));
s1=max(hf(j,:));
%select minimom 1 and minimom 2 or equal minimom number.
for i=len1:-1:1
if s>=hf(j,i)& hf(j,i)~=0
s=hf(j,i);
e=i;
end
end
for i=len1:-1:1
if s1>=hf(j,i)& hf(j,i)~=0 & i~=e
s1=hf(j,i);
e1=i;
end
end
ii=e;
ii1=e1;
% sum balanse
for i=1:len1
if ii< ii1
sb=s+s1;
hf(j+1,ii)=sb;
else if ii>ii1
sb=s+s1;
hf(j+1,ii1)=sb;
else
hf(j+1,ii)=hf(j,ii);
end
end
if i~=ii1 & i~=ii
hf(j+1,i)=hf(j,i);
end
end%end of for i
end%end of for j
hftree=hf([1:len1],:);
end
fprintf('\t<<>>\n');
hf=hftree
if hf==0
fprintf('\n<<<<<< The Cod Not Exist>>>>>>>.\n');
else
ln=length(hf);
for i=1:ln
temp=1;s=1;
for j=1:ln-1
if hf(j,i)~=hf(j+1,i)
if hf(j+1,i)~=0
import.a(i,s)='0';
s=s+1;
else
import.a(i,s)='1';
s=s+1;i1=i-1;
while (hf(j,i1)==hf(j+1,i1) & i1>1)
i1=i1-1;
end
if j
for j1=1:j
if hf(j1,i1)~=hf(j1+1,i1)
temp=temp+1;
end
end
end
if temp~=0
at=import.a(i1,:);
le=length(at);
for j1=temp:le
import.a(i,s)=at(j1);
s=s+1;
end
end
end
end
end%end of for j
end%end of for i
end% end of if for trase not zero
%sort of the result and erase null
ln=length(sc);
import.a(:,:)
if length(sc)==1 & length(st)>0
result(1,1)='0';
rcode=result;
result(1,length(st)+2)=st(1);
result
end
if ln>1
for i=1:ln
at=import.a(i,:);
s=1;
le=length(at);
for j=le:-1:1
if (at(j)=='0' | at(j)=='1')
import.a(i,s)=at(j);
s=s+1;
end
end
end
code=import.a;
result=code;
rcode=result;
for i=1:ln
result(i,len+2)=sv(i);
end
result
end
%//////////////////////code of hafman for send\\\\\\\\\\\\\\\\\\\\\\
st;% reshte vorudi dar halat avalea
if length(st)>0
if length(result)>1
lst=length(st);%tool reshte vorudi
lsc=length(sc);% tool vazn ya tekrar
lresult=len+2;% sakhteman kode
s=1;
for i=1:lst
for j=1:lsc
if length(result)==1
scode(1,1)='0';
else if st(i)==result(j,lresult)
at=rcode(j,:);
lat=length(at);
for z=1:lat
if at(z)=='1' | at(z)=='0'
scode(s)=at(z);
s=s+1;
end
end
end
end
end
end
scode;% cod binery for send in network
end
end% end of first if for trase not a null
%\\\\\\\\\\\\\\\\\\\\\\figure\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
if length(st) > 0
fprintf('Do you Want to ploting...! \n');
q=input('Other===>no 1(Enter)===>Yes :');
lscode=length(scode);
if q~=1 | lscode<1
scode;
else
scode;
low=-12;high=12;
longb=length(scode);
%input lengthof pals high and low
ton=input('please enter the for length high pals:');
toff=input('please enter the for length low pals:');
%main long pals is
if ton>0 & toff>0
s=scode(1);
i=1;leen=1;
figure(1);
for j=1:longb
i=i+1;
switch s
case{'1'}
t=0:0.1:ton;
a=(high)+(0.*t);% for lenght of t! create the matrix of high
w=ton:0.1:(toff+ton);
b=(low)+(0.*w);
A=[a b];
%sqw=repmat(A,1,1); this comand equall as A
if(j~=1)
b=leen:(j*(length(A))-1);
else
b=leen:(length(A));% for start of matrix not zero
end
leen=j*(length(b));
f=1;
for u=((j-1)*(length(b))+1):((j)*(length(b))) %creat the ends matrix
ga(u)=b(f);
gb(u)=A(f);
f=f+1;
end
%this method is high bit that is <<1>>
case{'0'}
t=0:0.1:toff;
a=(low)+(0.*t);% for lenght of t! create the matrix of high
w=toff:0.1:(toff+ton);
b=(high)+(0.*w);
A=[a b];
%sqw=repmat(A,1,1); this comand equall as A
if(j~=1)
b=leen:(j*(length(A))-1);
else
b=leen:(j*(length(A)));% for start of matrix not zero
end
leen=j*(length(b));
f=1;
for u=((j-1)*length(b)+1):((j)*(length(b))) %creat the ends matrix
ga(u)=b(f);
gb(u)=A(f);
f=f+1;
end
%this method is low bit that is <<0>>
otherwise
disp('unknow method,...please try egain>')
end%end of switch
if i<=longb
s=scode(i);%set one bit of aray for select condition
end
end%end of for j
plot(ga,gb,'g');%ga==n
%noi=gb.*rand(1,length(gb));%gb==sqw
%hold on
%plot(ga,noi,'--r');
clear ga;clear gb;clear noi;clear lba;clear longb;clear f;
clear leen;
else
fprintf('please enter the ton and toff for diagram.')
clear ga;clear gb;clear lba;clear longb;clear f;clear leen;
% clear noi;
end
end
end
%/////////////////////////////create header of huffman code\\\\\\\\\\\\\\
%scode is cod for send & rcode is code for each character & st is
%inputstring
f2=0;
if length(st)>0
st
rcode;scode;result;
if length(scode)>0% this if for amniat bishtar jahat ersal
f2=1;
end
end
%///////////////////////// export data of code <>/////////////////////
%\\\\\\\\\\\\\\\\\\\\\\\export tekrar of length scode\\\\\\\\\\\\\\\\\\\\
if f2==1% chek the input code that not null
lscode=length(scode);
[lrcode,max]=size(rcode);
k=1;
for i=1:lrcode
at=rcode(i,:);
lat=length(at);
temp=0;
for z=1:lat
if at(z)=='1' | at(z)=='0'
temp=temp+1;
end
end
if i==1
tkr(k)=temp;
else
ltkr=length(tkr);
ft=1;
for j=1:ltkr
if temp==tkr(j)
ft=0;
end
end
if ft==1
k=k+1;
tkr(k)=temp;
end
end
end
tkr=sort(tkr);% tool tekrar cod haee daryafti
disp('-----------------------------------------');
disp('---------The Header of send code---------');
disp('-----------------------------------------');
result
scode
%\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\export data of
%code\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
i=0;ii=1;s=0;
clear at;clear lat;
ltkr=length(tkr);
while(1)
for j=1:ltkr
t=tkr(j);%select the length of header scode
s=i+t;
%\\\\\\\\\\\\\\select rcode \\\\\\\\\
for z=1:lrcode%lrcode is tedad satrhaee rcode
at=rcode(z,:);
lat=length(at);
ff=1;
for p=1:lat
if (at(p)=='1' | at(p)=='0')
srcode(ff)=at(p);
ff=ff+1;
end
end% end of p
f1=length(srcode);
%s,i
if (s-i)==f1if s<=lscode & i
if scode(i+1:s)==srcode
le=length(result);% for find andis of character in header.
hfst(ii)=result(z,le);% result(le) is the character.
ii=ii+1;
i=s;
end
end
end
clear at;
clear lat;
clear srcode;
end% end of z
end%end of j
if i==lscode
break
end
end%end of while
hfst
end%end of f2 trase for not a null resived