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

📄 main.c

📁 VC++编程
💻 C
字号:
// string compression algorithm written in C
// by jared bruni
// www.lostsidedead.com


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>


void strcomp(char* source);// compress the string
void strdecomp(char* source);// decompress string
int  strdecomplen(char* source); // get just the decompressed length of a compressed string

void main()
{
	char data[500];
	int len;

	strcpy(data,"here is the raw data: tttt;kjlkdsajf dsajfsa fdsalfjdsa ffffffffffffffffffffffffffffffffffffffffffffff ;lakdsjf;lksajf;lsajf;lsajf asl;jfd;lfsafadsf f                 sadflkjasfd   tttttttttttttttttttttt fffffffffffffffffffffff test jaaaared");
	
	printf("the data to be compressed size(%i): %s\n\n",strlen(data),data);
	
	strcomp(data);

	len = strdecomplen(data);

	printf("the decompressed size is %i\n",len);
	

	printf("the compressed data size(%i): %s\n",strlen(data),data);

	strdecomp(data);

	printf("the data once again decompressed size(%i): %s\n",strlen(data),data);



	system("pause");

}


// simple string compress algorithm
void strcomp(char* source)
{
	char* temp = malloc( strlen(source) + 1 );
	int i, len = strlen(source),pos = 0;

	for(i = 0; i < len; i++)
	{
		// compress
		if( source[i+1] == source[i] && source[i+2] == source[i] && source[i+3] == source[i] )
		{
			int stop;
			int ic = 0;
			int amount = 0;
			char amt[15];
			int ilen;
			int z;


			for(ic = i; ic < len; ic++)
			{
				if(source[i] != source[ic])
				{
					stop = ic;
					break;// found stopping character
				}
				else
				{
					amount++;
				}
			}

			// append the amount of characters
			itoa(amount,amt,10);
			ilen = strlen(amt);
			temp[pos] = source[i];
			pos++;
			temp[pos] = (char) 200;
			pos++;
			for(z = 0; z < ilen; z++)
			{
				temp[pos] = amt[z];
				pos++;
			}
			temp[pos] = (char) 200;
			pos++;
		
			i = stop-1;


		}
		else// add it
		{
			temp[pos] = source[i];
			pos++;

		}
	}


	temp[pos] = 0;

	strcpy(source,temp);

	free(temp);
}

// simple string decompression algorithm
void strdecomp(char* source)
{
	char* temp = malloc(strlen(source) + strlen(source) + strlen(source) +  1);
	int i = 0,len = strlen(source),pos = 0;

	for(i = 0; i < len; i++)
	{
		if(source[i] == (char)200)
		{
			int stop = 0;
			int ic = 0;
			int startpos = i + 1;
			char xnum[15];
			int  xpos = 0;
			int ia = 0;
			int amt = 0;

			for(ic = startpos; ic < len; ic++)
			{
				if(source[ic] == (char)200)
				{
					stop = ic;
					break;
				}
			}
			// right here is were I get the number
			for(ia = i+1; ia < stop; ia++)
			{
				xnum[xpos] = source[ia];
				xpos++;
			}
			xnum[xpos] = 0;

			amt = atoi(xnum);

	
			for(ia = 0; ia < amt-1; ia++)
			{
				temp[pos] = source[i-1];
				pos++;
			}

			i = stop;
			continue;
		}
		else
		{
			temp[pos] = source[i];
			pos++;
		}
	}

	temp[pos] = 0;

	strcpy(source,temp);

	free(temp);
}


// get the decompressed length of a compressed string
int strdecomplen(char* source)
{
	int i = 0,len = strlen(source),pos = 0;
	int amt = 0;


	for(i = 0; i < len; i++)
	{
		if(source[i] == (char)200)
		{
			int stop = 0;
			int ic = 0;
			int startpos = i + 1;
			char xnum[15];
			int  xpos = 0;
			int ia = 0;
		
			for(ic = startpos; ic < len; ic++)
			{
				if(source[ic] == (char)200)
				{
					stop = ic;
					break;
				}
			}
			// right here is were I get the number
			for(ia = i+1; ia < stop; ia++)
			{
				xnum[xpos] = source[ia];
				xpos++;
			}
			xnum[xpos] = 0;

			amt += atoi(xnum)-1;

			i = stop;
			continue;
		}
		else
		{
			amt++;
		}
	}

	return amt;
}

⌨️ 快捷键说明

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