編碼c語言實現
❶ 怎麼做 c語言編碼
#include "stdio.h"
void main(){
printf("\t\t\t *************************************\n");
printf("\t\t\t* 1-------輸入成績 *\n")
printf("\t\t\t* 2-------查詢成績 *\n");
printf("\t\t\t* 3-------成績統計 *\n");
printf("\t\t\t* 4-------修改成績 *\n");
printf("\t\t\t* 5-------列印成績單 *\n");
printf("\t\t\t* 0-------退出 *\n");
printf("\t\t\t *************************************\n");
}
#include "stdio.h"
void main(){
int i,j;
for(i=0;i<14;i++)
printf("\n");
for(i=0;i<9;i++)
printf(" ");
for(j=0;j<4;j++){
for(i=0;i<j;i++)printf(" ");
for(i=0;i<6;i++)printf("*");
printf("\n");}
}
❷ huffman編碼解碼的c語言實現
留個腳印,晚上回去看看
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
//typedef int TElemType;
const int UINT_MAX=1000;
char str[50];
typedef struct
{
int weight,K;
int parent,lchild,rchild;
}HTNode,* HuffmanTree;
typedef char **HuffmanCode;
//-----------全局變數-----------------------
HuffmanTree HT;
HuffmanCode HC;
int w[50],i,j,n;
char z[50];
int flag=0;
int numb=0;
// -----------------求赫夫曼編碼-----------------------
struct cou{
char data;
int count;
}cou[50];
int min(HuffmanTree t,int i)
{ // 函數void select()調用
int j,flag;
int k=UINT_MAX; // 取k為不小於可能的值,即k為最大的權值1000
for(j=1;j<=i;j++)
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
t[flag].parent=1;
return flag;
}
//--------------------slect函數----------------------
void select(HuffmanTree t,int i,int &s1,int &s2)
{ // s1為最小的兩個值中序號小的那個
int j;
s1=min(t,i);
s2=min(t,i);
if(s1>s2)
{
j=s1;
s1=s2;
s2=j;
}
}
// --------------演算法6.12--------------------------
void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{ // w存放n個字元的權值(均>0),構造赫夫曼樹HT,並求出n個字元的赫夫曼編碼HC
int m,i,s1,s2,start;
//unsigned c,f;
int c,f;
HuffmanTree p;
char *cd;
if(n<=1)
return;//檢測結點數是否可以構成樹
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode)); // 0號單元未用
for(p=HT+1,i=1;i<=n;++i,++p,++w)
{
p->weight=*w;
p->parent=0;
p->lchild=0;
p->rchild=0;
}
for(;i<=m;++i,++p)
p->parent=0;
for(i=n+1;i<=m;++i) // 建赫夫曼樹
{ // 在HT[1~i-1]中選擇parent為0且weight最小的兩個結點,其序號分別為s1和s2
select(HT,i-1,s1,s2);
HT[s1].parent=HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
// 從葉子到根逆向求每個字元的赫夫曼編碼
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
// 分配n個字元編碼的頭指針向量([0]不用)
cd=(char*)malloc(n*sizeof(char)); // 分配求編碼的工作空間
cd[n-1]='\0'; // 編碼結束符
for(i=1;i<=n;i++)
{ // 逐個字元求赫夫曼編碼
start=n-1; // 編碼結束符位置
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
// 從葉子到根逆向求編碼
if(HT[f].lchild==c)
cd[--start]='0';
else
cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
// 為第i個字元編碼分配空間
strcpy(HC[i],&cd[start]); // 從cd復制編碼(串)到HC
}
free(cd); // 釋放工作空間
}
//---------------------獲取報文並寫入文件---------------------------------
int InputCode()
{
//cout<<"請輸入你想要編碼的字元"<<endl;
FILE *tobetran;
if((tobetran=fopen("tobetran.txt","w"))==NULL)
{
cout<<"不能打開文件"<<endl;
return 0;
}
cout<<"請輸入你想要編碼的字元"<<endl;
gets(str);
fputs(str,tobetran);
cout<<"獲取報文成功"<<endl;
fclose(tobetran);
return strlen(str);
}
//--------------初始化赫夫曼鏈表---------------------------------
void Initialization()
{ int a,k,flag,len;
a=0;
len=InputCode();
for(i=0;i<len;i++)
{k=0;flag=1;
cou[i-a].data=str[i];
cou[i-a].count=1;
while(i>k)
{
if(str[i]==str[k])
{
a++;
flag=0;
}
k++;
if(flag==0)
break;
}
if(flag)
{
for(j=i+1;j<len;j++)
{if(str[i]==str[j])
++cou[i-a].count;}
}
}
n=len-a;
for(i=0;i<n;i++)
{ cout<<cou[i].data<<" ";
cout<<cou[i].count<<endl;
}
for(i=0;i<=n;i++)
{*(z+i)=cou[i].data;
*(w+i)=cou[i].count;
}
/* 原來未修改的初始化程序段:
flag=1;
int num;
int num2;
cout<<"下面初始化赫夫曼鏈表"<<endl<<"請輸入結點的個數n:";
cin>>num;
n=num;
w=(int*)malloc(n*sizeof(int));
z=(char*)malloc(n*sizeof(char));
cout<<"\n請依次輸入"<<n<<"個字元(字元型):"<<endl;
char base[2];
for(i=0;i<n;i++)
{
cout<<"第"<<i+1<<"個字元:"<<endl;
gets(base);
*(z+i)=*base;
}
for(i=0;i<=n-1;i++)
{
cout<<setw(6)<<*(z+i);
}
cout<<"\n請依次輸入"<<n<<"個權值:"<<endl;
for(i=0;i<=n-1;i++)
{
cout<<endl<<"第"<<i+1<<"個字元的權值:";
cin>>num2;
*(w+i)=num2;
}*/
HuffmanCoding(HT,HC,w,n);
//------------------------列印編碼-------------------------------------------
cout<<"字元對應的編碼為:"<<endl;
for(i=1;i<=n;i++)
{
puts(HC[i]);
}
//--------------------------將赫夫曼編碼寫入文件------------------------
cout<<"下面將赫夫曼編碼寫入文件"<<endl<<"...................."<<endl;
FILE *htmTree;
char r[]={' ','\0'};
if((htmTree=fopen("htmTree.txt","w"))==NULL)
{
cout<<"can not open file"<<endl;
return;
}
fputs(z,htmTree);
for(i=0;i<n+1;i++)
{
fprintf(htmTree,"%6d",*(w+i));
fputs(r,htmTree);
}
for(i=1;i<=n;i++)
{
fputs(HC[i],htmTree);
fputs(r,htmTree);
}
fclose(htmTree);
cout<<"已將字元與對應編碼寫入根目錄下文件htmTree.txt中"<<endl<<endl;
}
//---------------------編碼函數---------------------------------
void Encoding()
{
cout<<"下面對目錄下文件tobetran.txt中的字元進行編碼"<<endl;
FILE *tobetran,*codefile;
if((tobetran=fopen("tobetran.txt","rb"))==NULL)
{
cout<<"不能打開文件"<<endl;
}
if((codefile=fopen("codefile.txt","wb"))==NULL)
{
cout<<"不能打開文件"<<endl;
}
char *tran;
i=99;
tran=(char*)malloc(100*sizeof(char));
while(i==99)
{
if(fgets(tran,100,tobetran)==NULL)
{
cout<<"不能打開文件"<<endl;
break;
}
for(i=0;*(tran+i)!='\0';i++)
{
for(j=0;j<=n;j++)
{
if(*(z+j-1)==*(tran+i))
{
fputs(HC[j],codefile);
if(j>n)
{
cout<<"字元錯誤,無法編碼!"<<endl;
break;
}
}
}
}
}
cout<<"編碼工作完成"<<endl<<"編碼寫入目錄下的codefile.txt中"<<endl<<endl;
fclose(tobetran);
fclose(codefile);
free(tran);
}
//-----------------解碼函數---------------------------------
void Decoding()
{
cout<<"下面對根目錄下文件codefile.txt中的字元進行解碼"<<endl;
FILE *codef,*txtfile;
if((txtfile=fopen("txtfile.txt","w"))==NULL)
{
cout<<"不能打開文件"<<endl;
}
if ((codef=fopen("codefile.txt","r"))==NULL)
{
cout<<"不能打開文件"<<endl;
}
char *work,*work2,i2;
int i4=0,i,i3;
unsigned long length=10000;
work=(char*)malloc(length*sizeof(char));
fgets(work,length,codef);
work2=(char*)malloc(length*sizeof(char));
i3=2*n-1;
for(i=0;*(work+i-1)!='\0';i++)
{
i2=*(work+i);
if(HT[i3].lchild==0)
{
*(work2+i4)=*(z+i3-1);
i4++;
i3=2*n-1;
i--;
}
else if(i2=='0') i3=HT[i3].lchild;
else if(i2=='1') i3=HT[i3].rchild;
}
*(work2+i4)='\0';
fputs(work2,txtfile);
cout<<"解碼完成"<<endl<<"內容寫入根目錄下的文件txtfile.txt中"<<endl<<endl;
free(work);
free(work2);
fclose(txtfile);
fclose(codef);
}
//-----------------------列印編碼的函數----------------------
void Code_printing()
{
cout<<"下面列印根目錄下文件CodePrin.txt中編碼字元"<<endl;
FILE * CodePrin,* codefile;
if((CodePrin=fopen("CodePrin.txt","w"))==NULL)
{
cout<<"不能打開文件"<<endl;
return;
}
if((codefile=fopen("codefile.txt","r"))==NULL)
{
cout<<"不能打開文件"<<endl;
return;
}
char *work3;
work3=(char*)malloc(51*sizeof(char));
do
{
if(fgets(work3,51,codefile)==NULL)
{
cout<<"不能讀取文件"<<endl;
break;
}
fputs(work3,CodePrin);
puts(work3);
}while(strlen(work3)==50);
free(work3);
cout<<"列印工作結束"<<endl<<endl;
fclose(CodePrin);
fclose(codefile);
}
//-------------------------------列印解碼函數---------------------------------------------
void Code_printing1()
{
cout<<"下面列印根目錄下文件txtfile.txt中解碼字元"<<endl;
FILE * CodePrin1,* txtfile;
if((CodePrin1=fopen("CodePrin1.txt","w"))==NULL)
{
cout<<"不能打開文件"<<endl;
return;
}
if((txtfile=fopen("txtfile.txt","r"))==NULL)
{
cout<<"不能打開文件"<<endl;
return;
}
char *work5;
work5=(char*)malloc(51*sizeof(char));
do
{
if(fgets(work5,51,txtfile)==NULL)
{
cout<<"不能讀取文件"<<endl;
break;
}
fputs(work5,CodePrin1);
puts(work5);
}while(strlen(work5)==50);
free(work5);
cout<<"列印工作結束"<<endl<<endl;
fclose(CodePrin1);
fclose(txtfile);
}
//------------------------列印赫夫曼樹的函數-----------------------
void coprint(HuffmanTree start,HuffmanTree HT)
{
if(start!=HT)
{
FILE * TreePrint;
if((TreePrint=fopen("TreePrint.txt","a"))==NULL)
{cout<<"創建文件失敗"<<endl;
return;
}
numb++;//該變數為已被聲明為全局變數
coprint(HT+start->rchild,HT);
cout<<setw(5*numb)<<start->weight<<endl;
fprintf(TreePrint,"%d\n",start->weight);
coprint(HT+start->lchild,HT);
numb--;
fclose(TreePrint);
}
}
void Tree_printing(HuffmanTree HT,int w)
{
HuffmanTree p;
p=HT+w;
cout<<"下面列印赫夫曼樹"<<endl;
coprint(p,HT);
cout<<"列印工作結束"<<endl;
}
//------------------------主函數------------------------------------
void main()
{
char choice;
while(choice!='q')
{ cout<<"\n******************************"<<endl;
cout<<" 歡迎使用赫夫曼編碼解碼系統"<<endl;
cout<<"******************************"<<endl;
cout<<"(1)要初始化赫夫曼鏈表請輸入'i'"<<endl;
cout<<"(2)要編碼請輸入'e'"<<endl;
cout<<"(3)要解碼請輸入'd'"<<endl;
cout<<"(4)要列印編碼請輸入'p'"<<endl;
cout<<"(5)要列印赫夫曼樹請輸入't'"<<endl;
cout<<"(6)要列印解碼請輸入'y'"<<endl;
if(flag==0)cout<<"\n請先初始化赫夫曼鏈表,輸入'i'"<<endl;
cin>>choice;
switch(choice)
{
case 'i':
Initialization();
break;
case 'e':
Encoding();
break;
case 'd':
Decoding();
break;
case 'p':
Code_printing();
break;
case 't':
Tree_printing(HT,2*n-1);
break;
case 'y':
Code_printing1();
break;
default:
cout<<"input error"<<endl;
}
}
free(z);
free(w);
free(HT);
}
❸ 用c語言實現算術編碼和解碼
Turbo c 2.0編譯通過
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineLENGTH100 /*字元串(編碼前或編碼後)的最大長度*/
/*編碼*/
voidencode(char*strsource);
/*解碼*/
voiddecode(char*strcode);
voidmain()
{
charcode[LENGTH]="BILLGATES";
encode(code);
printf("\nencodedstringis:%s\n",code);
decode(code);
printf("\ndecodedstringis:%s\n",code);
getch();
}
voidencode(char*strsource){
char*p=strsource,tmp[LENGTH]={'\0'},buffer[3];
while(*p){
itoa(*p++,buffer,10);
strcat(tmp,"%");
strcat(tmp,buffer);
}
strcpy(strsource,tmp);
}
voiddecode(char*strcode){
inti=0;
char*p,*s=strcode,tmp[LENGTH]={'\0'};
char*cSplit="%";
p=strtok(s,cSplit);
while(p)
{
tmp[i++]=atoi(p);
p=strtok(NULL,cSplit);
}
strcpy(strcode,tmp);
}
❹ 如何用C語言實現PCM編碼
PCM 脈沖編碼調制是Pulse Code Molation的縮寫。脈沖編碼調制是數字通信的編碼方式之一。主要過程是將話音、圖像等模擬信號每隔一定時間進行取樣,使其離散化,同時將抽樣值按分層單位四捨五入取整量化,同時將抽樣值按一組二進制碼來表示抽樣脈沖的幅值。
模擬信號數字化必須經過三個過程,即抽樣、量化和編碼,以實現話音數字化的脈沖編碼調制(PCM,Pulse Coding Molation)技術。
抽樣(Sampling)
抽樣是把模擬信號以其信號帶寬2倍以上的頻率提取樣值,變為在時間軸上離散的抽樣信號的過程。例如,話音信號帶寬被限制在0.3~3.4kHz內,用 8kHz的抽樣頻率(fs),就可獲得能取代原來連續話音信號的抽樣信號。對一個正弦信號進行抽樣獲得的抽樣信號是一個脈沖幅度調制(PAM)信號,如下圖對模擬正弦信號的抽樣所示。對抽樣信號進行檢波和平滑濾波,即可還原出原來的模擬信號。
量化(quantizing)
抽樣信號雖然是時間軸上離散的信號,但仍然是模擬信號,其樣值在一定的取值范圍內,可有無限多個值。顯然,對無限個樣值一一給出數字碼組來對應是不可能的。為了實現以數字碼表示樣值,必須採用「四捨五入」的方法把樣值分級「取整」,使一定取值范圍內的樣值由無限多個值變為有限個值。這一過程稱為量化。
量化後的抽樣信號與量化前的抽樣信號相比較,當然有所失真,且不再是模擬信號。這種量化失真在接收端還原模擬信號時表現為雜訊,並稱為量化雜訊。量化雜訊的大小取決於把樣值分級「取整」的方式,分的級數越多,即量化級差或間隔越小,量化雜訊也越小。
編碼(Coding)
量化後的抽樣信號在一定的取值范圍內僅有有限個可取的樣值,且信號正、負幅度分布的對稱性使正、負樣值的個數相等,正、負向的量化級對稱分布。若將有限個 量化樣值的絕對值從小到大依次排列,並對應地依次賦予一個十進制數字代碼(例如,賦予樣值0的十進制數字代碼為0),在碼前以「+」、「-」號為前綴,來 區分樣值的正、負,則量化後的抽樣信號就轉化為按抽樣時序排列的一串十進制數字碼流,即十進制數字信號。簡單高效的數據系統是二進制碼系統,因此,應將十 進制數字代碼變換成二進制編碼。根據十進制數字代碼的總個數,可以確定所需二進制編碼的位數,即字長。這種把量化的抽樣信號變換成給定字長的二進制碼流的 過程稱為編碼。常式:
#include<iostream>
usingnamespacestd;
intmain()
{
constintsect=8;//numberofsegement.
constintstartingVol[sect+1]={0,16,32,64,128,256,512,1024,2048};
//.
constintquanIntvl[sect]={1,1,2,4,8,16,32,64};
//,1equealto1/2048.
intpcmInd=0;//pcmcode'sindex.
intpcmCode[sect]={0,0,0,0,0,0,0,0};//8bitofpcmcodes.
intsampleValue=1270;
intstartPoint;//
//suchasstartingVol[startPoint]=16or128etc.
intfinePoint;//.
intquanValue;//it'.
intquanError;//errorcausedbyquantity.
//
intlow=0;
inthigh=sect;
intmid;
intloopInd1=0;//loopindextogetsegmentcode
intloopInd2=0;//
//getthefirst_digitcodeofpolarity
(sampleValue>0)?(pcmCode[pcmInd++]=1):(pcmCode[pcmInd]=0);
sampleValue=abs(sampleValue);//makesurethevoltageispositive
//
while(loopInd1<3)//
{
mid=(low+high)/2;
//after3loops,sampeValuefallsinstartingVol[mid]-startingVol[mid]or
//instartingVol[mid-1]-startingVol[mid]
if(sampleValue<startingVol[mid])
{
pcmCode[pcmInd++]=0;
high=mid;
startPoint=mid-1;
}
else
{
pcmCode[pcmInd++]=1;
low=mid;
startPoint=mid;
}
loopInd1++;
}//endwhile
//getthelastfourbitscodesofpcm
low=0;
high=16;//
while(loopInd2<4)
{
mid=(low+high)/2;
//.
quanValue=startingVol[startPoint]+mid*quanIntvl[startPoint];
cout<<startingVol[startPoint]<<"+"<<quanIntvl[startPoint]<<"*"<<mid<<"="
<<quanValue<<"?"<<sampleValue<<endl;
//.
if(sampleValue<startingVol[startPoint]+mid*quanIntvl[startPoint])
{
pcmCode[pcmInd++]=0;
high=mid;
finePoint=mid-1;
}
else
{
pcmCode[pcmInd++]=1;
low=mid;
finePoint=mid;
}
loopInd2++;
}//endwhile
quanValue=startingVol[startPoint]+finePoint*quanIntvl[startPoint];
quanValue+=quanIntvl[startPoint]/2;//finalquantityvalue.
quanError=abs(sampleValue-quanValue);//errorofquantity.
cout<<"Finalquantityvalueis:"<<quanValue<<endl;
cout<<"Errorofquantityis:"<<quanError<<endl;
cout<<"PCMcodesare:";
for(inti=0;i<8;i++)
{
cout<<pcmCode[i]<<"";
}//endfor
cout<<endl;
return0;
}
❺ 哈夫曼編碼C語言實現
我寫了一個注釋較為完整且壓縮、解壓縮比較全面的:
#include <stdio.h>
#include <conio.h>
#define MAX_FILE 5000/* 假設的文件最大長度 */
#define MAXLIST 256/* 最大MAP值 */
#define MAX_HOFFMAN_LENGTH 50/* 哈夫曼編碼長度 */
char dictionary[MAXLIST][2]={0};/* Hash映射,[][0]為權值,[][1]為字元 */
char fileContent[MAX_FILE];/* 處理的字元串大小 */
int Hoffman[MAXLIST][MAX_HOFFMAN_LENGTH]={2};/* 哈夫曼編碼序列 */
char HoffmanList[MAXLIST]={0};/* 哈夫曼編碼對應的字元有序序列 */
char HoffFileCode[MAX_FILE]={0};/* 哈夫曼編碼字元串序列 */
char HoffFile[MAX_FILE]={0};
/* 編碼到假設的文件的哈夫曼壓縮格式: 依次存儲 原字元串長度(1位元組存儲:可擴展到2位元組)、哈夫曼編碼數(1位元組)、每個哈夫曼編碼的長度序列、每個哈夫曼編碼對應的字元序列、編碼過的哈夫曼字元串 */
char GetFile[MAX_FILE]={0};/* 解碼序列 */
void ShellSort(char pData[MAXLIST][2],int Count)/* Shell排序,用於准備有序化要構造的編碼權值構造哈夫曼樹做准備 */
{
int step[4]={9,5,3,1};/* 增量序列 */
int iTemp,cTemp;
int k,s,w,i,j;
for(i=0;i<4;i++)
{
k=step[i];
s=-k;
for(j=k;j<Count;j++)
{iTemp=pData[j][0];
cTemp=pData[j][1];
w=j-k;
if(s==0)
{
s=-k;
s++;
pData[s][0]=iTemp;
pData[s][1]=cTemp;
}
while((iTemp<pData[w][0])&&(w>=0)&&(w<=Count))
{
pData[w+k][0]=pData[w][0];/* 權值交換 */
pData[w+k][1]=pData[w][1];/* 字元交換 */
w=w-k;
}
pData[w+k][0]=iTemp;
pData[w+k][1]=cTemp;
}
}
}
struct TNode/* 哈夫曼樹結點 */
{
struct TNode* pNode;
struct TNode* lNode;
struct TNode* rNode;
char dictionary;
char weight;
};
void TNode_init(struct TNode*tn,char dic,char wei)
{
tn->pNode=0;
tn->lNode=0;
tn->rNode=0;
tn->dictionary=dic;
tn->weight=wei;
}
struct LNode/* 鏈表結點,用於存儲哈夫曼樹結點,進而構造哈夫曼樹(保證每一步鏈表結點包含的哈夫曼結點都是有序的) */
{
struct LNode* prev;
struct LNode* next;
struct TNode* tnode;
};
void LNode_init(struct LNode* ln)
{
ln->prev=ln->next=0;
ln->tnode=0;
}
int len=0;/* 哈夫曼編碼數 */
int deep=-1;/* 深度 */
void Preorder(struct TNode * p);/* 前序遍歷 */
void byLeft(struct TNode*p)/* 經由左結點 */
{
deep++;
Hoffman[len][deep]=0;
Preorder(p);
Hoffman[len][deep]=2;
deep--;
}
void byRight(struct TNode*p)/* 經由右結點 */
{
deep++;
Hoffman[len][deep]=1;
Preorder(p);
Hoffman[len][deep]=2;
deep--;
}
void Preorder(struct TNode * p)
{
int i;
if(p->lNode!=0)/* 當左子結點非空則遍歷 */
{
byLeft(p->lNode);
}
if(p->rNode!=0)/* 當右子結點非空則遍歷 */
{
byRight(p->rNode);
}
if((p->lNode==0)&&(p->rNode==0))/* 當左右結點都為空,則增加哈夫曼編碼數到另一個記錄 */
{
Hoffman[len][deep+1]=2;
i=0;
for(;Hoffman[len][i]!=2;i++)
{
Hoffman[len+1][i]=Hoffman[len][i];
}
Hoffman[len+1][i]=2;
HoffmanList[len]=p->dictionary;
len++;
}
}
char generateOne(int k)/* 產生k個連續1的二進制串,比如111,1111,111111,用於編碼進假設的文件 */
{
char c=0;
for(;k!=0;k--)
{
c|=(1<<(k-1));
}
return c;
}
int compareBits(char b1,char b2,char c,int l,int d)/* 判斷由 [b1,b2] 組成的16位二進制數以d為起點,是否是長度為l的c二進制串(哈夫曼編碼)的前綴 */
{
unsigned char t=(((((0x00ff&b1)<<8)|(0x00ff&b2))>>(8-d))&0x00ff);
return (((t)&((generateOne(l)<<(8-l))&0xff))==((c<<(8-l))&0xff));
}
int main()
{
struct LNode* t,*p;
struct LNode* head;
struct TNode *tempTNode,*k1;
int i=0,j,k;
unsigned short fileLen=0;
int len=0,l,b1,b2,d;
char c;
int code[500],h=0;
int codeLen=0,total=0;
/* 或許假定的文件字元串向量中的字元串 */
printf("please Enter string to be pressed:");
scanf("%s",&fileContent);
/* Hash進dictionary */
for(;fileContent[i]!='\0';i++,fileLen++)
{
++dictionary[fileContent[i]][0];
dictionary[fileContent[i]][1]=fileContent[i];
}
/* 把Hash了的dictionary向前靠攏 */
{
for(i=0;i!=MAXLIST;i++)
{
if(dictionary[i][0]!=0)
{
dictionary[len][0]=dictionary[i][0];
dictionary[len][1]=dictionary[i][1];
len++;
}
}
}
printf("the number of Huffman's codes:%d\n",len);
/* 對dictionary按權值進行排序 */
ShellSort(dictionary,len);
/* 構造鏈表,鏈表中放有序dictionary權值的樹結點 */
head=(struct LNode*)malloc(sizeof(struct LNode)),p=head;
LNode_init(head);
head->next=(struct LNode*)malloc(sizeof(struct LNode));
LNode_init(head->next);
tempTNode=(struct TNode*)malloc(sizeof(struct LNode));
TNode_init(tempTNode,dictionary[0][1],dictionary[0][0]);
head->tnode=tempTNode;
{
for(i=0;i!=len-1;i++)
{
p->next->prev=p->next;
p=p->next;
p->next=(struct LNode*)malloc(sizeof(struct LNode));
LNode_init(p->next);
tempTNode=(struct TNode*)malloc(sizeof(struct TNode)) ;
TNode_init(tempTNode,dictionary[i+1][1],dictionary[i+1][0]);
p->tnode=tempTNode;
}
}
free(p->next);
p->next=0;
/* 每次最小權值的前面兩個鏈表結點中的樹結點組成一個子樹,子樹有合權值,子數的根按權值排序進鏈表*/
for(p=head;p->next!=0;)
{
p->tnode->pNode=(struct TNode*)malloc(sizeof(struct TNode)) ;
TNode_init(p->tnode->pNode,'\0',(p->tnode->weight)+(p->next->tnode->weight));
p->next->tnode->pNode=p->tnode->pNode;
p->tnode->pNode->lNode=p->tnode;
p->tnode->pNode->rNode=p->next->tnode;
head=p->next;
free(p);
p=head;
p->tnode=p->tnode->pNode;
for(t=head;t->next!=0;t=t->next)
{
if(t->tnode->weight>t->next->tnode->weight)
{
k1=t->tnode;
t->tnode=t->next->tnode;
t->next->tnode=k1;
}
}
}
/* 前序遍歷構造哈夫曼編碼 */
Preorder(p->tnode);
{
for(i=0;i!=len;i++)
dictionary[HoffmanList[i]][0]=i;
}
/* 存儲字元串的哈夫曼壓縮編碼串,並且打包文件格式 */
{
for(i=0;i!=fileLen;i++)
{
int j=dictionary[fileContent[i]][0];
for(k=0;Hoffman[j][k]!=2;k++)
{
HoffFileCode[codeLen]|=(Hoffman[j][k]<<(7-total%8));
code[h++]=Hoffman[j][k];
if(((total+1)%8)==0)
{
HoffFile[1+len*3+1+codeLen]=HoffFileCode[codeLen];
codeLen++;
}
total++;
}
}
}
HoffFile[1+len*3+1+codeLen]=HoffFileCode[codeLen];
HoffFile[0]=(fileLen);
/* 解壓縮假定的文件HoffFile成為原字元串序列 */
printf("Huffman's code list:\n");
HoffFile[1]=len;
{
for(i=0,j=0;i!=len;i++,j=0)
{
for(;Hoffman[i][j]!=2;j++);
HoffFile[i+2]=j;
HoffFile[i+2+2*len]=HoffmanList[i];
for( k=0;k!=j;k++)
{
printf("%d",Hoffman[i][k]);
HoffFile[i+2+len]|=(Hoffman[i][k]<<(j-1-k));
}
printf(":%d\n",HoffmanList[i]);
}
}
{
for(i=0,j=0;i!=(HoffFile[0]&0xff);i++)
{
for(k=0;k!=HoffFile[1];k++)
{
l=HoffFile[2+k],d=j%8,b1=HoffFile[j/8+2+HoffFile[1]*3],b2=HoffFile[j/8+1+2+HoffFile[1]*3];
c=HoffFile[HoffFile[1]+2+k];
if(compareBits(b1,b2,c,l,d))
{
j+=HoffFile[2+k];
GetFile[i]=HoffFile[2+HoffFile[1]*2+k];
break;
}
}
}
}
{
printf("Huffman code List Pressed :\n");
for(i=0;i!=h;i++)
{
printf("%c",code[i]);
if((i+1)%8==0)
printf(" ");
}
}
printf("\n");
{
printf("Huffman code packaged:\n");
for(i=0;i!=HoffFile[0]+HoffFile[1]*3;i++)
{
printf("%c",HoffFile[i]);
}
printf("\n");
}
printf("The initial len :%d\n",fileLen);
printf("The string len pressed:%d\n",(h)/8+1);
printf("The rate%.2f\%",((h/8.0+1)/fileLen)*100);
{
printf("The number of bytes:%d\n",(HoffFile[0]&0xff));
printf("The string decoded:");
for(i=0;i!=(HoffFile[0]&0xff);i++)
{
printf("%c",GetFile[i]);
}
printf("\n");
}
getch();
return 1;
}
❻ 初學數據結構 請問該怎麼用C語言實現編碼
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#include <string.h>
typedef struct _list{
int value;
_list *next;
}list;
void init(list **p,int *length){
*p = (list *)malloc(sizeof(list));
(*p)->value = NULL;
(*p)->next = NULL;
*length = 0;
}
void add(int x,int value,list **p,int *length){
if(!*length){ //如果鏈表為空
(*p)->value = value;
(*p)->next = NULL;
}
else if(x == 1){ //如果要插入元素到第一個位置
list *q = (list *)malloc(sizeof(list));
q->value = value;
q->next = *p;
*p = q;
}
else{
list *iterator = *p;
list *q = (list *)malloc(sizeof(list));
q->value = value;
if(x >= *length + 1 ) //如果要插入到鏈表尾部
{
for( ; iterator->next != NULL;){
iterator = iterator->next;
}
iterator->next = q;
q->next = NULL;
}
else{
int i;
for(i = 1; i < x - 1; i ++){ //如果要插入到鏈表中部
iterator = iterator->next;
}
list *tmp = iterator->next;
iterator->next = q;
q->next = tmp;
}
}
(*length) ++;
}
void del(int x,list **p,int *length){
list *q = *p;
list *iterator = *p;
if(*length == 1){ //如果鏈表只有一個元素
free(*p);
*p = NULL;
}
else if(x == 1){ //如果要刪除第一個元素
q = q->next;
free(*p);
*p = q;
}
else{
x = x > (*length) ? *length : x; //如果要刪除剩餘元素
for(; x > 2; x--){
iterator = iterator->next;
}
q = iterator->next;
if(q->next == NULL){
free(q);
iterator->next = NULL;
}else{
iterator->next = q->next;
free(q);
}
}
(*length) -- ;
}
void print(int x,list *p,int *length){
if(!(*length)){
printf("empty\n");
}
else{
x = x > *length ? *length : x;
for(; x > 1; x--,p = p->next){
}
printf("%d ",p->value);
}
}
int main(){
list *p; //鏈表頭
int length; //鏈表長度
int n;
char input[255];
scanf("%d",&n);
while(n--){
fflush(stdin); //刷新輸入緩沖區
gets(input); //getline
if(strncmp(input,"INIT",strlen("INIT")) == 0){
init(&p,&length);
}
else if(strncmp(input,"ADD",strlen("ADD")) == 0){
int x,value;
sscanf(input,"%*s%d%d", &x, &value);
add(x,value,&p,&length);
}
else if (strncmp(input,"LENGTH",strlen("LENGTH")) == 0){
printf("%d\n",length);
}
else if (strncmp(input,"PRINT",strlen("PRINT")) == 0){
int i = 1;
for(i; i <= length; i++)
print(i,p,&length);
printf("\n");
}
else if (strncmp(input,"DELETE",strlen("DELETE")) == 0){
int x,value;
sscanf(input,"%*s%d", &x);
del(x,&p,&length);
}
}
}
❼ 用C語言實現CRC編碼程序
#include <stdio.h>
#include <string.h>
#include "stdlib.h"
unsigned int char2int(char *str)
{
unsigned int count=0, ret=0;
for(count = 0; count<strlen(str);count++)
{
ret = ret<<1;
if('0' != str[count])
{ ret+=1;}
}
return ret;
}
unsigned int getR(char *str)
{
unsigned int c =0 ;
int ret = strlen(str)-1;
for(c=0;c < strlen(str);c++)
{if(str[c] != '0')<br/> {return ret-c;}
}
}
int getRi(unsigned int num)
{
int c =0;
for(;num != 0; c++)
{num = num>>1;}
return c;
}
void CRC(char *scode, char *p, char*g )
{
unsigned int iP = char2int(p);
unsigned int iG = char2int(g);
unsigned int r= getR(g);
unsigned int code = iP << r;
unsigned int yx = code;
for(;getRi(yx) >= getRi(iG);)
{ yx = yx ^ (iG<<(getRi(yx) - getRi(iG)));}
code += yx;
itoa(code,scode,2);
}
void main() //定義主函數
{
char data[8]="" , bds[8]="",code[16]="";
printf("數據:");
scanf("%s", data);
printf("表達式:");
scanf("%s", bds);
CRC(code,data,bds);
printf("編碼:%s",code);
}