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

相关推荐
智者知已应修善业30 分钟前
【查找字符最大下标以*符号分割以**结束】2024-12-24
c语言·c++·经验分享·笔记·算法
91刘仁德1 小时前
c++类和对象(下)
c语言·jvm·c++·经验分享·笔记·算法
diediedei1 小时前
模板编译期类型检查
开发语言·c++·算法
mmz12071 小时前
分治算法(c++)
c++·算法
一切尽在,你来2 小时前
C++多线程教程-1.2.1 C++11/14/17 并发特性迭代
开发语言·c++
80530单词突击赢2 小时前
C++入门指南:从零到精通
开发语言·c++
Tansmjs2 小时前
C++编译期数据结构
开发语言·c++·算法
diediedei2 小时前
C++类型推导(auto/decltype)
开发语言·c++·算法
兩尛2 小时前
c++的数组和Java数组的不同
java·开发语言·c++
lhxcc_fly3 小时前
手撕简易版的vector
c++·vector