一道作业,问题给了一个数组A(字符串形式),要求把它转换成压缩形式的B数组(整型),比如AAAABBBBCCD-->4A4BCCD,两个一样的字母不变。同时要求调用三个函数,一个用来压缩(变量是A,B两个数组),一个用来print B数组,一个把B数组还原回原字符串print。
话说B数组不应该是整形呀,不然不能保存字母了。以下是我的代码。。。
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
void yasuo(char a[],char b[])
{
int count=1,p=0;
for(int i=0; i<strlen(a); i++)
if(a[i]==a[i+1])
count++;
else if(count>2)
{
b[p++]=(char)(count+'0');
b[p++]=a[i];
count=1;
}
else if(count==2)
{
b[p++]=a[i];
b[p++]=a[i];
count=1;
}
else
b[p++]=a[i];
}
void printB(char b[])
{
cout<<b<<endl;
}
void backB(char b[])
{
for(int i=0; i<strlen(b); i++)
if(b[i]<='9'&&b[i]>='3')
{
for(int j=0; j<(int)(b[i]-'0'); j++)
cout<<b[i+1];
i++;
}
else
cout<<b[i];
cout<<endl;
}
int main()
{
char a[1000]= {0},b[1000]= {0};
gets(a);
yasuo(a,b);
printB(b);
backB(b);
}
追答满意请采纳(⊙o⊙)哦!