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

相关推荐
AA陈超2 分钟前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-18.生成火球术
c++·游戏·ue5·游戏引擎·虚幻
wxin_VXbishe12 分钟前
springboot居家养老管理系统-计算机毕业设计源码55953
java·c++·spring boot·python·spring·django·php
ULTRA??22 分钟前
归并排序算法实现,kotlin,c++,python
c++·python·kotlin
deng-c-f28 分钟前
C/C++内置库函数(5):值/引用传递、移动构造、以及常用的构造技巧
开发语言·c++
qq_3106585128 分钟前
mediasoup源码走读(十)——producer
服务器·c++·音视频
Tipriest_35 分钟前
C++ Python使用常用库时如何做欧拉角 ⇄ 四元数转换
c++·python·四元数·欧拉角
小尧嵌入式38 分钟前
C语言中的面向对象思想
c语言·开发语言·数据结构·c++·单片机·qt
fpcc44 分钟前
跟我学C++中级篇——循环展开的分析
c++·优化
埃伊蟹黄面1 小时前
算法 --- hash
数据结构·c++·算法·leetcode
ULTRA??2 小时前
Informed RRT*实现椭圆启发式采样
c++·算法