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

相关推荐
旖旎夜光6 小时前
多态(11)(下)
c++·学习
yangpipi-6 小时前
《C++并发编程实战》 第4章 并发操作的同步
开发语言·c++
Chance_to_win6 小时前
C++基础知识
c++
有趣的我6 小时前
C++ 多态介绍
开发语言·c++
WBluuue7 小时前
Codeforces 1068 Div2(ABCD)
c++·算法
阿沁QWQ8 小时前
C++的map和set
开发语言·c++
charlie11451419110 小时前
现代C++工程实践:简单的IniParser3——改进我们的split
开发语言·c++·笔记·学习
fish_xk10 小时前
c++的引用和类的初见
开发语言·c++
晨尘光11 小时前
【Windows 下FlatBuffers 编译.fbs文件并应用】
c++·windows
煤球王子11 小时前
学而时习之:C++中的文件处理2
c++