You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# 二维char数组压缩功能实现代码问题排查

代码问题分析
  • 核心逻辑错误:你当前的计数是对整行所有P/F的全局累计,且每遇到一个字符就立刻输出当前累计值,不符合**连续相同字符计数(游程编码)**的压缩规则。比如连续3个P,你会输出1 P 2 P 3 P,而不是预期的3 P。
  • 无效判断冗余:if (source[i, j] > 0)完全多余,'P'和'F'的ASCII值本身就大于0,这个判断没有任何作用。
  • 未实现文件输出:当前逻辑仅打印到控制台,没有将结果写入压缩后的txt文件的功能。
修正后的代码
using System.IO;

namespace Tomorites
{
    class Compression
    {
        // 重载方法:支持直接输出到指定路径的压缩文件
        public void Compress(char[,] source, string outputPath)
        {
            using (StreamWriter sw = new StreamWriter(outputPath))
            {
                for (int i = 0; i < source.GetLength(0); i++)
                {
                    if (source.GetLength(1) == 0) 
                    {
                        sw.WriteLine();
                        continue;
                    }
                    // 初始化:记录当前块的字符和连续计数
                    char currentChar = source[i, 0];
                    int count = 1;

                    for (int j = 1; j < source.GetLength(1); j++)
                    {
                        if (source[i, j] == currentChar)
                        {
                            // 字符和当前块相同,计数+1
                            count++;
                        }
                        else
                        {
                            // 字符变化,输出上一个块的结果
                            sw.Write($"{count} {currentChar} ");
                            // 重置当前块的字符和计数
                            currentChar = source[i, j];
                            count = 1;
                        }
                    }
                    // 输出一行最后一个块的结果
                    sw.WriteLine($"{count} {currentChar} ");
                }
            }
        }

        // 保留原来仅打印到控制台的重载
        public void Compress(char[,] source)
        {
            for (int i = 0; i < source.GetLength(0); i++)
            {
                if (source.GetLength(1) == 0) 
                {
                    Console.WriteLine();
                    continue;
                }
                char currentChar = source[i, 0];
                int count = 1;

                for (int j = 1; j < source.GetLength(1); j++)
                {
                    if (source[i, j] == currentChar)
                    {
                        count++;
                    }
                    else
                    {
                        Console.Write($"{count} {currentChar} ");
                        currentChar = source[i, j];
                        count = 1;
                    }
                }
                Console.WriteLine($"{count} {currentChar} ");
            }
        }
    }
}
使用说明

如果需要输出到压缩文件,调用方法时传入输出路径即可,示例:

// 假设你已经读取txt内容生成了char[,]类型的sourceData
Compression compression = new Compression();
compression.Compress(sourceData, @"C:\自定义路径\压缩结果.txt");

内容的提问来源于stack exchange,提问作者Tomello

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 17:27:04