MFC简单字符串压缩程序

一个mfc简单字符串压缩程序;按以下情况进行压缩;

1 仅压缩连续重复出现的字符。比如"abcbc"无连续重复字符,压缩后还是"abcbc"。

2 压缩的格式为"字符重复的次数+字符"。例如,"xxxyyyyyyz"压缩后就成为"3x6yz"。

cpp 复制代码
void CYssDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	char str[100] = {'\0'};
    char res[100] = {'\0'};
    
	CString strText;
	GetDlgItemText(IDC_EDIT1, strText);
	//str=strText.GetBuffer(strText.GetLength());
	//WideCharToMultiByte(CP_ACP,0,str,strText.GetLength(),strText,strText.GetLength());
	sprintf(str, "%s", strText);

    int length = strlen(str);
    int i=0, j=0, k=0;
    int count = 0;
    do
    {
        if(i < length && str[i++] == str[j])
            count++;
        if(str[i] != str[j])
        {
            if(count <= 1)
                res[k++] = str[j];
            else
            {
                if(count > 1)
                {
                    char temp[10] = {'\0'};
                    itoa(count,temp,10);
                    strcpy(res+k,temp);
                    k+=strlen(temp);
                    res[k++] = str[j];
                }
            }
            j = i;
            count = 0;
        }
    }while(i<length);
    res[k] = '\0';	

	SetDlgItemText(IDC_EDIT2, res);
}

运行情况如下;

1

1

目前看上去没问题;最好是别输入数字;因为 aa11111,压缩后为2a51,数字可能不是太好分;

可执行文件可在此下载;

百度网盘 请输入提取码

提取码:gc92

相关推荐
山上三树几秒前
详细介绍读写锁
开发语言·c++·spring
苏宸啊6 分钟前
string实现
c++
马士兵教育13 分钟前
计算机专业学生入行IT行业,编程语言如何选择?
java·开发语言·c++·人工智能·python
naruto_lnq22 分钟前
C++与自动驾驶系统
开发语言·c++·算法
奶茶树30 分钟前
【数据结构进阶】AVL树(详解)
数据结构·c++
面条有点辣38 分钟前
C++内存管理基础概念入门到理解
c++
蒹葭玉树42 分钟前
【C++上岸】C++常见面试题目--网络篇(第二十六期)
网络·c++·面试
老四啊laosi1 小时前
[C++初阶] 10. string模拟实现
c++·stl·string
EmbedLinX1 小时前
C++ STL 学习笔记
c++·stl
m0_736919101 小时前
C++中的策略模式实战
开发语言·c++·算法