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

📄 rlcoding.cpp

📁 本程序实现了数据压缩中RLC(游程编码)过程。执行环境为 TC 3.0。
💻 CPP
字号:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include <dir.h>
#include <sys\stat.h>
#include <fcntl.h>
#include <io.h>
#define	Sc_flag1  	24
#define Sc_flag2  	25
#define F_name_flag	92
#define	str_number	160
struct	RLC
{
    char  ch_x;
    char  ch_Sc;
    int   int_RL;
}   RLC_stream[str_number];

void main()
{
//----------Initialization.
begin:
    FILE  *f_stream;
    FILE  *f_stream_out;
    int	  array_length,RLC_length=0;
    int   i,j,ch_counter=1,handle,f_name_length;
    char  *f_path,*f_out_path,*f_full_name;
    char  s[MAXPATH],drive[MAXDRIVE],dir[MAXDIR],file[MAXFILE],ext[MAXEXT];
    char  str_array[str_number];
    char  ch_temp,ch_temp_ch,ch_again,ch_ask;
    char  *str_exit={"System is trying to Quit...Byebye!"};
//----------Choose action.
    printf("Compressing,Decopressing,Quit(C/D/Q)?\t");
ask_ch:
    ch_ask=getch();
    if(ch_ask=='c'||ch_ask=='C'||ch_ask=='d'||ch_ask=='D'||ch_ask=='q'||ch_ask=='Q')
	{putch(ch_ask);}
    switch (ch_ask)
    {
//*-*-*-*-*-Compressing start here.
    case'C':
    case'c':
	printf("\nPlease input a compressing file:\t");
	gets(f_path);
	f_stream=fopen(f_path,"r");
	fgets(str_array,str_number,f_stream);
	fclose(f_stream);
	array_length=strlen(str_array);
//----------Input error?
	if(array_length>str_number)
	    {printf("The string OVERFLOW!\n");goto exit;}
	if(f_stream==NULL)
	    {printf("The file is not exist or is empty!\n");goto begin;}
//----------Coding start here.
	else if(array_length>0&&array_length<=str_number)
	{
//----------Creat the compressing file name.
	    fnsplit(f_path,drive,dir,file,ext);
	    strcpy(f_full_name,file);
	    strcat(f_full_name,".rlc");
//----------
	    printf("The length of the string is:\t\t%d\n",array_length);
	    ch_temp_ch=str_array[0];
	    for(i=1;i<array_length+1;i++)
	    {
		ch_temp=str_array[i];
		if(ch_temp==ch_temp_ch)
		    {ch_counter++;}
		else
		{
		    if(ch_counter>3)
		    {
			RLC_length++;
			RLC_stream[RLC_length-1].int_RL=ch_counter;
			RLC_stream[RLC_length-1].ch_Sc=Sc_flag1;
			RLC_stream[RLC_length-1].ch_x=ch_temp_ch;
		    }
		    else
		    {
			ch_temp_ch=str_array[i-1];
			for(j=0;j<ch_counter;j++)
			{
			    RLC_length++;
			    RLC_stream[RLC_length-1].int_RL=1;
			    RLC_stream[RLC_length-1].ch_Sc=Sc_flag2;
			    RLC_stream[RLC_length-1].ch_x=ch_temp_ch;
			}
		    }
		    ch_counter=1;
		    ch_temp_ch=str_array[i];
		}
	    }
//----------Creat a compressing file.
	    printf("Please input the Saving Path:\t\t");
	    gets(s);
	    strcat(s,"\\");
	    strcat(s,f_full_name);
	    _fmode = O_BINARY;
	    handle = creat(s, S_IREAD | S_IWRITE);
	    close(handle);
	    strcpy(f_out_path,s);
//----------Write to the file.
	    f_stream=fopen(f_out_path,"w+");
	    for(i=0;i<RLC_length;i++)
	    {
		if(RLC_stream[i].ch_Sc==Sc_flag1)
		{
		    fprintf(f_stream,"%c",RLC_stream[i].ch_x);
		    fprintf(f_stream,"%c",RLC_stream[i].ch_Sc);
		    fprintf(f_stream,"%d",RLC_stream[i].int_RL);
		}
		else{fprintf(f_stream,"%c",RLC_stream[i].ch_x);}
	    }
	    fprintf(f_stream,"%c",F_name_flag);
	    fprintf(f_stream,"%s",file);
	    fprintf(f_stream,"%s",ext);
	    fclose(f_stream);
	    printf("The Compressed file located at:\t\t%s\n",f_out_path);
	}
	break;
//*-*-*-*-*-Decompressing start here.
    case'D':
    case'd':
	printf("\nPlease input a Decompressing file:\t");
	gets(f_out_path);
	f_stream=fopen(f_out_path,"r");
	if(f_stream==NULL)
	    {printf("The file is not exist or is empty!\n\n");goto begin;}
//----------Get the file name from str_array.
	fgets(str_array,str_number,f_stream);
	array_length=strlen(str_array);
	f_full_name=strtok(str_array,"\\");
	f_full_name=strtok(NULL,"\\");
	f_name_length=strlen(f_full_name);
//----------Get the RLC_length.
	for(i=0;i<array_length-f_name_length;i++)
	{
	    if(str_array[i]!=Sc_flag1)
		{RLC_length++;}
	}
	fclose(f_stream);
//----------Creat a Decompressing file.
	printf("Please input the Saving Path:\t\t");
	gets(s);
	strcat(s,"\\");
	strcat(s,f_full_name);
	_fmode = O_BINARY;
	handle = creat(s, S_IREAD | S_IWRITE);
	close(handle);
	strcpy(f_path,s);
	printf("The Decompressed file is located at:\t%s\n",s);
//----------Write to the file.
	f_stream=fopen(f_out_path,"r");
	f_stream_out=fopen(f_path,"w+");
	fscanf(f_stream,"%c",&ch_temp_ch);
	ch_temp=ch_temp_ch;
	for(i=0;i<14;i++)
	{
	    fscanf(f_stream,"%c",&ch_temp);
	    if(ch_temp==Sc_flag1)
	    {
		fscanf(f_stream,"%d",&ch_counter);
		for(j=0;j<ch_counter;j++)
		    {fprintf(f_stream_out,"%c",ch_temp_ch);}
		fscanf(f_stream,"%c",&ch_temp_ch);
	    }
	    else
	    {
		 fprintf(f_stream_out,"%c",ch_temp_ch);
		 ch_temp_ch=ch_temp;
	    }
	}
	fclose(f_stream);
	fclose(f_stream_out);
	break;
    case 'q':
    case 'Q':	goto exit;
    default :	goto ask_ch;
    }
    printf("Mission complete!\n");
//----------Release variables.
    for(i=0;i<RLC_length;i++)
    {
	RLC_stream[i].ch_x=NULL;
	RLC_stream[i].ch_Sc=NULL;
	RLC_stream[i].int_RL=NULL;
    }
//----------Try again?
    printf("\nTry again now?(Y/N)...\t\t\t");
again:
    ch_again=getch();
    if(ch_again=='Y'||ch_again=='y'||ch_again=='N'||ch_again=='n')
	{putch(ch_again);}
    switch (ch_again)
    {
	case 'Y':
	case 'y':    printf("\n\n");goto begin;
	case 'N':
	case 'n':    goto exit;
	default:     goto again;
    }
//----------To exit.
exit:
    printf("\n");
    for(i=0;i<34;i++)
	{printf("%c",str_exit[i]);delay(30);}
    delay(400);
    printf("\n\n");
}

⌨️ 快捷键说明

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