c字符串压缩
package javase1.day02;x0dx0a /**x0dx0a * 1)一种字符串压缩算法x0dx0a * str ="aaaabbccccddeaaa"x0dx0a * 压缩为:"4a2b4c2d1e3a"x0dx0a * 原理实现:x0dx0a * str = "aaaabbccccddeaaa"x0dx0a * x0dx0a * c = str.charAt(i)//c是每个字符x0dx0a * 1) 初始化x0dx0a * StringBuilder buf = new StringBuilder();x0dx0a * int count = 0;代表相同的字符个数x0dx0a * char ch = str.charAt(0);代表正在统计的相同字符'a' x0dx0a * 2) 从i=1开始迭代每个字符x0dx0a * c = str.charAt(i);//c是每个当前字符x0dx0a * 3) 检查当前字符c与被统计ch是否一致x0dx0a * 如果一致 count++x0dx0a * 否则(不一致)x0dx0a * 向缓冲区buf增加count+chx0dx0a * count=0,ch=c;x0dx0a * 3)没有下个字符就结束x0dx0a * 4)还有字符串吗?回到2)x0dx0a * x0dx0a * 2)实现还原算法x0dx0a * str = "4a2b4c2d1e3a";x0dx0a * ix0dx0a */x0dx0apublic class Demo5 {x0dx0a public static void main(String[] args) {x0dx0a String s = comp("aaaawwwwe");x0dx0a System.out.println(s);x0dx0a// System.out.println(decomp(s));x0dx0a x0dx0a }x0dx0a public static String comp(String str){x0dx0a int i = 1;x0dx0a StringBuilder buf = new StringBuilder();x0dx0a int count = 1;x0dx0a char ch = str.charAt(0);x0dx0a for(;;){x0dx0a char c = i==str.length() ? '\10':str.charAt(i);x0dx0a if(c==ch){x0dx0a count++;x0dx0a }else{x0dx0a if(count == 1)x0dx0a buf.append(ch);x0dx0a else x0dx0a buf.append(count).append(ch);x0dx0a count=1;x0dx0a ch = c;x0dx0a } x0dx0a i++;x0dx0a if(i==str.length()+1){x0dx0a break;x0dx0a } x0dx0a }x0dx0a return buf.toString();x0dx0a x0dx0a }x0dx0a}
② 字符串解压缩
刚才编成了压缩的程序,不好意思,现在的可以解压了。
程序没有给你编写读写文件的内容,文件读写自已去编,那个相对就简单了,程序只介绍了实现基本功能的内容。你可以输入3A4B7D测试。
void
main()
{int
m=0;int
j=0;
//string
a;
//char
c[111];
char
a[111];
char
b[111];
scanf("%s",a);
for(int
i=0;a[i]!='\0';i++)
{
cout<<"a"<
1&&a[i]-'0'<9)
{
m=a[i]-'0';}
else{b[j]=a[i];j++;}
while(m>1)
{
b[j]=a[i+1];
j++;
m--;
}
}
cout<
评论
0
0
0
加载更多
③ c++怎么压缩字符串
一个个读入字符,记住当前一个和上一个
如果当前和上一个相同,计数加一
不同那么把上一个和计数放入结果字符串,上一字符值替换成当前,计数设置为1
接收到换行或者eof时结束程序并输出
④ 字符串压缩与解压缩
由于精度问题,该算法的压缩能力有限,字符串长度不能过长,否则会出现溢出,压缩会出错。还有,忘了对空格键处理,所以你一旦输入空格就会结束字符串输入
#include<iostream>
#include<math.h>
#include<string>
#include<vector>
using namespace std;
struct node
{
char elem;
double weigh;
double low;
double high;
double rang;
};
////////////////////
double value(string &code)
{
double res=0;
for(int i=0;i<code.size();i++)
{
if(code[i]=='1')
res=res+pow(2,-(i+1));
}
return res;
}
/////////////////////
int search(vector<node> &array,char &e)
{
for(int i=0;i<array.size();i++)
{
if(array[i].elem==e)
return i;
}
return -1;
}
//////////////////////
void set(string &data,vector<node> &array)
{
cin>>data;
data=data+'$';
node temp;
for(int i=0;i<data.size();i++)
{
int f=0;
for(int j=0;j<array.size();j++)
{
if(array[j].elem==data[i])
{
array[j].weigh++;
f=1;
break;
}
}
if(f==1)continue;
temp.elem=data[i];
temp.weigh=1;
array.push_back(temp);
}
array[0].low=0;
array[0].rang=array[0].weigh/data.size();
array[0].high=array[0].low+array[0].rang;
for(i=1;i<array.size();i++)
{
array[i].low=array[i-1].high;
array[i].rang=array[i].weigh/data.size();
array[i].high=array[i].low+array[i].rang;
}
}
//////////////////////
void output(vector<node> &array)
{
cout<<"elem low high rang"<<endl;
for(int i=0;i<array.size();i++)
{
cout<<array[i].elem;
cout.width(10);
cout<<array[i].low;
cout.width(10);
cout<<array[i].high;
cout.width(10);
cout<<array[i].rang<<endl;
}
}
///////////
void getarith(string &data,vector<node> &array,vector<node> &arith)
{
node temp;
int t=search(array,data[0]);
temp=array[t];
arith.push_back(temp);
for(int i=1;i<data.size();i++)
{
temp.elem=data[i];
int t=search(array,data[i]);
temp.low=arith[i-1].low+array[t].low*arith[i-1].rang;
temp.rang=arith[i-1].rang*array[t].rang;
temp.high=temp.low+temp.rang;
arith.push_back(temp);
}
}
///////////////
void code(double low,double high,string &res)
{
while(value(res)<low)
{
string temp=res+'1';
if(value(temp)>high)
res=res+'0';
else
res=temp;
}
}
////////////
void decode(double math,vector<node> &array)
{
while(1)
{
for(int i=0;;i++)
{
if(math<array[i].high)
break;
}
if(array[i].elem=='$')break;
cout<<array[i].elem;
math=math-array[i].low;
math=math/array[i].rang;
}
}
//////////////////
int main()
{
string data;
vector<node> array;
set(data,array);
string result;
vector<node> arith;
/* array[0].elem='a';
array[0].low=0;
array[0].rang=0.2;
array[0].high=0.2;
array[1].elem='b';
array[1].low=0.2;
array[1].rang=0.1;
array[1].high=0.3;
array[2].elem='c';
array[2].low=0.3;
array[2].rang=0.2;
array[2].high=0.5;
array[3].elem='d';
array[3].low=0.5;
array[3].rang=0.05;
array[3].high=0.55;
array[4].elem='e';
array[4].low=0.55;
array[4].rang=0.3;
array[4].high=0.85;
array[5].elem='f';
array[5].low=0.85;
array[5].rang=0.05;
array[5].high=0.9;
array[6].elem='$';
array[6].low=0.9;
array[6].rang=0.1;
array[6].high=1;*/
getarith(data,array,arith);
cout<<"字符数据表为:"<<endl;
output(array);
cout<<"输入字符串的算术编码数据表为:"<<endl;
output(arith);
string res;
code(arith[arith.size()-1].low,arith[arith.size()-1].high,res);
cout<<"字符串的算术编码为:"<<endl;
cout<<res<<endl;
double math=value(res);
cout<<math<<endl;
cout<<endl<<"解码结果为:"<<endl;
decode(math,array);
cout<<endl;
system("pause");
return 0;
}
⑤ 字符串如何压缩
去网上搜索免费代码,这年代还自己写通用代码,又浪费时间又浪费精力————前提,别拿别人的免费代码做商业用途。
⑥ 下面的c语言字符串压缩程序怎么写呀 输入样例 a5b3aba13b4 输出: aaaaabbbabaaaaaaaaaaaaabbbb
#include <stdio.h>
#include <string.h>
void main()
{
char s[80];
int n=0,i=0,j;
printf("请输入字符串:");
gets(s);
if(strlen(s)%2!=0)
{
printf("输入有误!");
return;
}
for(i=0;s[i]!='\0';i+=2)
{
for(j=1;j<=s[i+1]-48;j++)
printf("%c",s[i]);
}
printf("\n");
}
⑦ C语言求助:请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
用下面的代码给你提供个思路。这代码连续字符不得超过9个……
#include "stdio.h"
void main(void){
char a[1000]="gcccddecc";
int i,j,k,n;
printf("Type an integer(a~z)...\nStr=");
gets(a);
for(k=i=0;a[i];i++){
for(j=i+1,n=0;a[i]==a[j];j++) n++;
if(n){
a[k++]=n+'1';
a[k++]=a[--j];
i+=n;
}
else a[k++]=a[i];
}
a[k]='\0';
printf("The result = %s.\n",a);
}
⑧ 用C语言编程 字符串原地压缩。题目:“eeeeeaaaff" 压缩为 "e5a3f2"把s字符串压缩处理后结果保存在res中
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#defineMAX50
typedefstructstr
{
charch;
intccount;
structstr*next;
}node;
node*func(chara[],intn);
intmain()
{
intindex=0;
node*head,*head2,*temp;
chars[MAX],res[MAX];//最大输入字符可以自己设置
scanf("%s",s);
head2=head=func(s,sizeof(s));
while(head)
{
res[index++]=head->ch;
res[index++]=head->ccount+48;
head=head->next;
}
res[index]='