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

📄 lzw.txt

📁 LZW编码
💻 TXT
字号:
#include "stdafx.h"
#include<stdlib.h>

#include <stdio.h>

#include<iomanip.h>
#include   <string.h> 


int Len,admit=100000;

struct LZW

{

 int id;   //编码点位

 int link;  //连接点位

 char code;  //编码字符

 LZW* next;

};

struct Lzwstring

{
 int id;   //对应 编码点位

 int length;  //字符串长度 不包括'\0'

 char* code;  //字符串

 Lzwstring* next;

};


LZW* coding(char* filename)

{

 FILE* stream;

 //打开文件

 if( ( stream = fopen( filename, "r" )) == NULL )

 {  

  printf( "The file was not opened!!\n" );

  printf("Press any key to exit!");

  getchar();

  exit(0);

 }

 else 

 {

  //printf( "The file was opened\n" );

 }

 //生成编码链表头节点

 LZW* head = (LZW*)malloc(sizeof(LZW));  

 head->id = 0;

 head->link = 0;

 head->next = NULL;

 LZW* node; //编码链表节点

 //编码字符串链表

 Lzwstring* stringhead; //头节点 

 Lzwstring* stringnode; //节点

 Lzwstring* stringtemp; //临时节点

 //开始读取文件

 char ch = fgetc( stream); //读取第一个字符

 if( !feof(stream)) //文件非空 --读取第一个字符后, 如果判断feof(stream)==1,则文件是空文件!

 { 
  Len=1;
  //第一个编码节点, 保存第一个字符.  

  node = (LZW*)malloc(sizeof(LZW)); 

  node->id = 1;

  node->link = 0;

  node->code = ch;

  node->next = NULL;

  head->next = node;

  //生成编码字符串链表 保存已经存在的字符串

  stringhead = (Lzwstring*)malloc(sizeof(Lzwstring)); //头节点

  stringnode = (Lzwstring*)malloc(sizeof(Lzwstring)); //

  stringnode->id = 1;   //第一个字符串节点的id为0, 表示是第一个编码节点的字符串节点

  stringnode->next = NULL;

  stringhead->next = stringnode;

  stringtemp = (Lzwstring*)malloc(sizeof(Lzwstring)); //临时接点

  //在第一个节点保存首个字符串

  char str[2];

  str[0] = ch;

  str[1] = '\0';  

  stringnode->code = (char*)malloc(2*sizeof(char));

  stringnode->length = 1;

  strcpy( stringnode->code, str);

  

  //printf("%s\n",stringnode->code);

  //一些临时变量

  //读取文件中的第二个字符

  int flag = 0;  //标志位: 0-没有相同字符存在 1-有相同的字符串存在 

  int restart = 0;  //标志位: 0-重新开始查找新的字符串 1-继续...

  int len = 0;   //字符串长度

  int nodenum = 2;  //节点数目, 由于已经有了第一个编码节点, 所以从2记数.

  int linknum = 0;  //连接节点编号

  int endlinknum = 0;  //专门为了处理最后的字符串用的.

  ch = fgetc( stream );

  Len++;

  while( !feof( stream ) && ch!='\10') //文件没有结束

  {

   str[0] = ch;

   str[1] = '\0';

   Len++;

   if(Len>=admit)

   {

    printf("It has more than %d numbers!\n",admit);

    //exit(0);

    break;

   }

   //开始或重新开始

   if( 0 == restart)

   {

len = 1;

    stringtemp->code = (char*)malloc((len+1)*sizeof(char));  //申请2个长度的内存空间

    stringtemp->next = NULL;

    stringtemp->length = 1;

    strcpy( stringtemp->code, str);

    linknum = 0;

   }

   else

   {

    len++; //字符串长度 

    char* pstr = stringtemp->code;        //保存先前的字符串 

    stringtemp->code = (char*)malloc((len+2)*sizeof(char));  //重新申请指定长度的内存空间

    stringtemp->next = NULL;

    stringtemp->length = len;

    strcpy( stringtemp->code, pstr);       //复制先前的字符串

    strcat( stringtemp->code, str);        //连接新字符

    free(pstr);           //释放先前的字符串内存空间 

   }

   
   //从头搜索, 比较编码字符串链表中是否已经存在此字符串

   Lzwstring* pl3 = stringhead->next;

   LZW* plzw = head->next;

   while( NULL != pl3)

   {

    //如果已经有相同的字符串, 设置标志位

    if( 0 == strcmp(stringtemp->code, pl3->code))    

    {

     flag = 1; 

     restart = 1; //继续读取字符

     endlinknum = linknum; //保存前一次的连接节点编号, 给最后一个字符串专用的.

     linknum = pl3->id;

     break;

    }

    //  

    pl3 = pl3->next;

    plzw = plzw->next;

   }

   // 没有匹配的字符串, 则...

   if( 0 == flag) 

   {

    restart = 0;

    //添加编码节点

    node->next = (LZW*)malloc(sizeof(LZW)); 

    node = node->next;

    node->id = nodenum;

    node->link = linknum;

    node->code = ch;

    node->next = NULL;
    
    //添加到字符串链表, 新节点

    stringnode->next = (Lzwstring*)malloc(sizeof(Lzwstring));    

    stringnode = stringnode->next;

    stringnode->next = NULL;

    stringnode->id = nodenum;

    stringnode->length = len;

    stringnode->code = (char*)malloc((len+2)*sizeof(char)); 

    strcpy(stringnode->code, stringtemp->code);  

    //清空字符串

    stringtemp->code = NULL;

    //

    nodenum++;  //节点数 - 编码点位

   }
   
   //读取下一字符准备进行新的比较

   flag = 0; //重置标志位

   ch = fgetc(stream);

  }

  // while循环结束

  //对最后的字符串特别处理一下

  if(stringtemp->code != NULL) //若最后的字符串不为空

  {   

   //添加编码节点

   node->next = (LZW*)malloc(sizeof(LZW)); 

   node = node->next;

   node->id = nodenum;

   node->link = endlinknum;

   node->code = str[0];

   node->next = NULL;

   //添加到字符串节点

   stringnode->next = stringtemp; 

  }

  else

  {

   free(stringtemp); //否则释放内存空间

  }

  

  //输出结果

  printf("\n节点 连接节点\t字符\t相应的字符串\n");

  Lzwstring* p518 = stringhead->next;

  LZW* pp = head->next;

  while( NULL != pp)

  {

   printf("%d\t%d\t%c\t", pp->id, pp->link, pp->code);

   pp = pp->next;

   printf("%s\n", p518->code);

   p518 = p518->next;

  }

   

 }

 return head;

}


int main(int argc, char* argv[])

{

 //输入文件名

 long time;

 char fname[50];

 printf("\n--------------------\n");

 printf("Input file name: (length<50)\n");

 scanf("%s", &fname);

 

 //读取文件

 coding(fname);

 

 if(Len>=admit)

 {

  printf("The length of the character string in this file is equal to or more than %d.\n",admit);

  printf("Thus the program is broken!\n");

 }


 printf("press any key to exit.\n");

 getchar();

 getchar();

 return 0;

}
#include "stdafx.h"
#include<stdlib.h>

#include <stdio.h>

#include<iomanip.h>
#include   <string.h> 


int Len,admit=100000;

struct LZW

{

 int id;   //编码点位

 int link;  //连接点位

 char code;  //编码字符

 LZW* next;

};

struct Lzwstring

{
 int id;   //对应 编码点位

 int length;  //字符串长度 不包括'\0'

 char* code;  //字符串

 Lzwstring* next;

};


LZW* coding(char* filename)

{

 FILE* stream;

 //打开文件

 if( ( stream = fopen( filename, "r" )) == NULL )

 {  

  printf( "The file was not opened!!\n" );

  printf("Press any key to exit!");

  getchar();

  exit(0);

 }

 else 

 {

  //printf( "The file was opened\n" );

 }

 //生成编码链表头节点

 LZW* head = (LZW*)malloc(sizeof(LZW));  

 head->id = 0;

 head->link = 0;

 head->next = NULL;

 LZW* node; //编码链表节点

 //编码字符串链表

 Lzwstring* stringhead; //头节点 

 Lzwstring* stringnode; //节点

 Lzwstring* stringtemp; //临时节点

 //开始读取文件

 char ch = fgetc( stream); //读取第一个字符

 if( !feof(stream)) //文件非空 --读取第一个字符后, 如果判断feof(stream)==1,则文件是空文件!

 { 
  Len=1;
  //第一个编码节点, 保存第一个字符.  

  node = (LZW*)malloc(sizeof(LZW)); 

  node->id = 1;

  node->link = 0;

  node->code = ch;

  node->next = NULL;

  head->next = node;

  //生成编码字符串链表 保存已经存在的字符串

  stringhead = (Lzwstring*)malloc(sizeof(Lzwstring)); //头节点

  stringnode = (Lzwstring*)malloc(sizeof(Lzwstring)); //

  stringnode->id = 1;   //第一个字符串节点的id为0, 表示是第一个编码节点的字符串节点

  stringnode->next = NULL;

  stringhead->next = stringnode;

  stringtemp = (Lzwstring*)malloc(sizeof(Lzwstring)); //临时接点

  //在第一个节点保存首个字符串

  char str[2];

  str[0] = ch;

  str[1] = '\0';  

  stringnode->code = (char*)malloc(2*sizeof(char));

  stringnode->length = 1;

  strcpy( stringnode->code, str);

  

  //printf("%s\n",stringnode->code);

  //一些临时变量

  //读取文件中的第二个字符

  int flag = 0;  //标志位: 0-没有相同字符存在 1-有相同的字符串存在 

  int restart = 0;  //标志位: 0-重新开始查找新的字符串 1-继续...

  int len = 0;   //字符串长度

  int nodenum = 2;  //节点数目, 由于已经有了第一个编码节点, 所以从2记数.

  int linknum = 0;  //连接节点编号

  int endlinknum = 0;  //专门为了处理最后的字符串用的.

  ch = fgetc( stream );

  Len++;

  while( !feof( stream ) && ch!='\10') //文件没有结束

  {

   str[0] = ch;

   str[1] = '\0';

   Len++;

   if(Len>=admit)

   {

    printf("It has more than %d numbers!\n",admit);

    //exit(0);

    break;

   }

   //开始或重新开始

   if( 0 == restart)

   {

len = 1;

    stringtemp->code = (char*)malloc((len+1)*sizeof(char));  //申请2个长度的内存空间

    stringtemp->next = NULL;

    stringtemp->length = 1;

    strcpy( stringtemp->code, str);

    linknum = 0;

   }

   else

   {

    len++; //字符串长度 

    char* pstr = stringtemp->code;        //保存先前的字符串 

    stringtemp->code = (char*)malloc((len+2)*sizeof(char));  //重新申请指定长度的内存空间

    stringtemp->next = NULL;

    stringtemp->length = len;

    strcpy( stringtemp->code, pstr);       //复制先前的字符串

    strcat( stringtemp->code, str);        //连接新字符

    free(pstr);           //释放先前的字符串内存空间 

   }

   
   //从头搜索, 比较编码字符串链表中是否已经存在此字符串

   Lzwstring* pl3 = stringhead->next;

   LZW* plzw = head->next;

   while( NULL != pl3)

   {

    //如果已经有相同的字符串, 设置标志位

    if( 0 == strcmp(stringtemp->code, pl3->code))    

    {

     flag = 1; 

     restart = 1; //继续读取字符

     endlinknum = linknum; //保存前一次的连接节点编号, 给最后一个字符串专用的.

     linknum = pl3->id;

     break;

    }

    //  

    pl3 = pl3->next;

    plzw = plzw->next;

   }

   // 没有匹配的字符串, 则...

   if( 0 == flag) 

   {

    restart = 0;

    //添加编码节点

    node->next = (LZW*)malloc(sizeof(LZW)); 

    node = node->next;

    node->id = nodenum;

    node->link = linknum;

    node->code = ch;

    node->next = NULL;
    
    //添加到字符串链表, 新节点

    stringnode->next = (Lzwstring*)malloc(sizeof(Lzwstring));    

    stringnode = stringnode->next;

    stringnode->next = NULL;

    stringnode->id = nodenum;

    stringnode->length = len;

    stringnode->code = (char*)malloc((len+2)*sizeof(char)); 

    strcpy(stringnode->code, stringtemp->code);  

    //清空字符串

    stringtemp->code = NULL;

    //

    nodenum++;  //节点数 - 编码点位

   }
   
   //读取下一字符准备进行新的比较

   flag = 0; //重置标志位

   ch = fgetc(stream);

  }

  // while循环结束

  //对最后的字符串特别处理一下

  if(stringtemp->code != NULL) //若最后的字符串不为空

  {   

   //添加编码节点

   node->next = (LZW*)malloc(sizeof(LZW)); 

   node = node->next;

   node->id = nodenum;

   node->link = endlinknum;

   node->code = str[0];

   node->next = NULL;

   //添加到字符串节点

   stringnode->next = stringtemp; 

  }

  else

  {

   free(stringtemp); //否则释放内存空间

  }

  

  //输出结果

  printf("\n节点 连接节点\t字符\t相应的字符串\n");

  Lzwstring* p518 = stringhead->next;

  LZW* pp = head->next;

  while( NULL != pp)

  {

   printf("%d\t%d\t%c\t", pp->id, pp->link, pp->code);

   pp = pp->next;

   printf("%s\n", p518->code);

   p518 = p518->next;

  }

   

 }

 return head;

}


int main(int argc, char* argv[])

{

 //输入文件名

 long time;

 char fname[50];

 printf("\n--------------------\n");

 printf("Input file name: (length<50)\n");

 scanf("%s", &fname);

 

 //读取文件

 coding(fname);

 

 if(Len>=admit)

 {

  printf("The length of the character string in this file is equal to or more than %d.\n",admit);

  printf("Thus the program is broken!\n");

 }


 printf("press any key to exit.\n");

 getchar();

 getchar();

 return 0;

}
/* ma.txt的内容是:woshiyigeshiren*/

⌨️ 快捷键说明

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