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

相关推荐
有点。15 分钟前
C++ ⼀级 2024 年 03 ⽉
c++
CC.GG36 分钟前
【C++】二叉搜索树
java·c++·redis
Savior`L2 小时前
二分算法及常见用法
数据结构·c++·算法
深海潜水员2 小时前
OpenGL 学习笔记 第一章:绘制一个窗口
c++·笔记·学习·图形渲染·opengl
mmz12072 小时前
前缀和问题(c++)
c++·算法·图论
ULTRA??2 小时前
初学protobuf,C++应用例子(AI辅助)
c++·python
旖旎夜光3 小时前
list实现(7)(上)
c++
不会c嘎嘎3 小时前
深入理解 C++ 异常机制:从原理到工程实践
开发语言·c++
崇山峻岭之间3 小时前
C++ Prime Plus 学习笔记026
c++·笔记·学习
赖small强4 小时前
【Linux C/C++开发】Linux 平台 Stack Protector 机制深度解析
linux·c语言·c++·stack protector·stack-protector·金丝雀机制