C语言压缩程序 [英] Compression Program in C
问题描述
我想压缩一系列字符.例如,如果我输入
I want to compress a series of characters. For example if i type
输入:FFFFFBBBBBBBBBCCBBBAABBGGGGGSSS(27 x 8位= 216位) 输出:F5B7C2B3A2B2G5S3(14 x 8位= 112位)
Input : FFFFFBBBBBBBCCBBBAABBGGGGGSSS (27 x 8 bits = 216 bits) Output: F5B7C2B3A2B2G5S3 (14 x 8 bits = 112bits)
到目前为止,这就是我所拥有的,我可以算出数组中的字符数.但是最重要的任务是按照相同的顺序对其进行计数.我似乎无法弄清楚:( 就在几周前,我就盯着C做我,我对数组,指针,ASCII值有了解 但无论如何似乎都无法按顺序计算这些字符.香港专业教育学院尝试一切.这种方法不好,但是最接近我.
So far this is what i have, i can count the number of Characters in the Array. But the most important task is to count them in the same sequence. I can't seem to figure that out :( Ive stared doing C just a few weeks back, i have knowledge on Array, pointers, ASCII value but in any case can't seem to count these characters in a sequence. Ive try a bit of everything. This approach is no good but it the closest i came to it.
#include <stdio.h>
#include <conio.h>
int main()
{
int charcnt=0,dotcnt=0,commacnt=0,blankcnt=0,i, countA, countB;
char str[125];
printf("*****String Manipulations*****\n\n");
printf("Enter a string\n\n");
scanf("%[^'\n']s",str);
printf("\n\nEntered String is \" %s \" \n",str);
for(i=0;str[i]!='\0';i++)
{
// COUNTING EXCEPTION CHARS
if(str[i]==' ')
blankcnt++;
if(str[i]=='.')
dotcnt++;
if(str[i]==',')
commacnt++;
if (str[i]=='A' || str[i]=='a')
countA++;
if (str[i]=='B' || str[i]=='b')
countA++;
}
//PRINT RESULT OF COUNT
charcnt=i;
printf("\n\nTotal Characters : %d",charcnt);
printf("\nTotal Blanks : %d",blankcnt);
printf("\nTotal Full stops : %d",dotcnt);
printf("\nTotal Commas : %d\n\n",commacnt);
printf("A%d\n", countA);
}
推荐答案
这是我为此分配工作制定的解决方案-此函数用于压缩字符串.如果仍然有问题,希望对您有所帮助.
Here is the solution I've worked out for this assignment- This function was used to do the compression of the string. Hope it help if any still having problem.
#include <stdio.h>
extern compression_function(char arr[1000])
{
char current_char;
int count, i,j=0,t=0,G=0,H=0, char_size=0;
int original_length=0,com_number_length=0,compressed_length=0;
int index=0;
FILE* outputfile;
FILE* processing;
outputfile= fopen("C:\\Users\\Desktop\\output.txt","w");
processing= fopen("C:\\Users\\Desktop\\processing.txt","w");
if(outputfile == '\0' )
{
printf("Cannot Write To File!\n");
}
current_char = arr[0];
count = 1;
i = 0;
printf("\n\nOUTPUT: ");
//USING A WHILE LOOP
while (arr[i] != '\0')
{
//'i' WILL BE INCREMENTED TO CHECK ALL THE CHAR IN THE ARRAY
i++;
// CHECK IF THE CURENT CHAR IS THE SAME AS THE LAST ONE
if( arr[i] == current_char )
{
count++;
}
//ELSE IF NO MORE CHAR IS SIMILAR, IT WILL PRINT THE COUNT RESULT RIGHT AWAY
else
{
if(count==1)
{
//sprintf(output_array,"%c", current_char);
printf("%c", current_char);
fprintf(outputfile,"%c", current_char);
fprintf(processing,"%c", current_char);
G++;
}
if(count>=2)
{
printf("%c%d", current_char, count);
fprintf(outputfile,"%c%d", current_char,count);
fprintf(processing,"%c", current_char );
}
if (count>9)
{
j++;
}
if (count>99)
{
t++;
}
//REST ALL COUNT FOR THE SECOND DIFFRENT CHAR IN ARRAY
current_char = arr[i];
count = 1;
char_size++;
//BREAK THE LOOP WHEN CHAR IN ARRAY IS NULL
if( current_char == '\0' )
{
break;
}
}
}
original_length = strlen(arr);
com_number_length=(char_size+j+t-G);
compressed_length=(char_size+char_size+j+t-G);
fclose(outputfile);
fclose(processing);
//CALLING FUNCTION-SIZE-CALCULATOR
size_calculator(original_length,char_size,com_number_length,compressed_length);
}
这篇关于C语言压缩程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!