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

相关推荐
前进吧-程序员9 分钟前
现代 C++ 异步编程:从零实现一个高性能 ThreadPool (C++20 深度实践)
开发语言·c++·c++20
pearlthriving33 分钟前
c++当中的泛型思想以及c++11部分新特性
java·开发语言·c++
t***5441 小时前
Dev-C++中哪些选项可以设置
开发语言·c++
2301_803554522 小时前
C++ 并发核心:std::promise、std::future、std::async 超详细全解
开发语言·c++
EverestVIP2 小时前
C++ 成员函数的指针
c++
俺不要写代码2 小时前
线程启动、结束,创建线程多法、join,detach,线程的移动语义
服务器·开发语言·网络·c++
思麟呀2 小时前
应用层协议HTTP
linux·服务器·网络·c++·网络协议·http
小徐不徐说3 小时前
面试C++易错点总结
开发语言·c++·面试·职场和发展·程序设计·工作
xier_ran4 小时前
【C++】“内部”、“外部”、“派生类”、“友元“类
java·开发语言·c++
熬夜敲代码的猫4 小时前
C/C++:内存管理
c语言·c++·动态内存管理