跳到正文

休闲·娱乐

T3

__SSY__约 42 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月14日
篇幅
约 42 分钟

版本

  1. v2当前
  2. v1
HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能字符串解压工具</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .glass-effect {
            background: rgba(255, 255, 255, 0.25);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.18);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .fade-in {
            animation: fadeIn 0.5s ease-in;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .typewriter {
            overflow: hidden;
            border-right: 2px solid #667eea;
            white-space: nowrap;
            animation: typing 3.5s steps(40, end), blink-caret 0.75s step-end infinite;
        }
        @keyframes typing {
            from { width: 0 }
            to { width: 100% }
        }
        @keyframes blink-caret {
            from, to { border-color: transparent }
            50% { border-color: #667eea }
        }
    </style>
</head>
<body class="min-h-screen gradient-bg">
    <div class="container mx-auto px-4 py-8">
        <!-- 头部导航 -->
        <header class="text-center mb-12 fade-in">
            <h1 class="text-4xl md:text-5xl font-bold text-white mb-4">
                <i class="fas fa-file-archive mr-3"></i>智能字符串解压工具
            </h1>
            <p class="text-white/80 text-lg max-w-2xl mx-auto">
                支持嵌套压缩格式解析,实时可视化展示解压过程,提供完整的解压解决方案
            </p>
        </header>

        <!-- 主功能区域 -->
        <div class="max-w-6xl mx-auto">
            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-8">
                <!-- 输入区域 -->
                <div class="glass-effect rounded-2xl shadow-xl p-6 card-hover">
                    <h2 class="text-2xl font-semibold text-white mb-6">
                        <i class="fas fa-keyboard mr-2"></i>压缩字符串输入
                    </h2>

                    <div class="space-y-6">
                        <!-- 格式说明 -->
                        <div class="bg-white/20 p-4 rounded-lg">
                            <h3 class="text-white font-medium mb-2">支持的格式:</h3>
                            <ul class="text-white/90 text-sm space-y-1">
                        <li><code class="bg-white/20 px-2 py-1 rounded">数字[字符串]</code> - 如: 3[abc] → abcabcabc</li>
                        <li><code class="bg-white/20 px-2 py-1 rounded">嵌套格式</code> - 如: 2[3[a]b] → aaabaaab</li>
                        <li><code class="bg-white/20 px-2 py-1 rounded">混合格式</code> - 如: 2[ab]3[cd] → ababcdcdcd</li>
                    </ul>
                </div>

                <!-- 输入框 -->
                <div>
                    <label class="block text-white font-medium mb-3">输入压缩字符串:</label>
                    <textarea
                        id="inputString"
                        class="w-full h-40 p-4 bg-white/90 border border-white/30 rounded-xl focus:ring-2 focus:ring-white focus:border-white resize-none transition-all duration-200"
                        placeholder="请输入压缩字符串,如:3[abc]、2[3[a]b] 或 2[ab]3[cd]"
                    >2[3[a]b]</textarea>
                </div>

                <!-- 控制按钮 -->
                <div class="flex gap-4">
                    <button
                        id="decompressBtn"
                        class="flex-1 bg-white text-purple-600 hover:bg-white/90 font-semibold py-3 px-6 rounded-xl transition-all duration-200 flex items-center justify-center"
                    >
                        <i class="fas fa-play-circle mr-2"></i>执行解压
                    </button>
                    <button
                        id="clearBtn"
                        class="px-6 bg-white/20 text-white hover:bg-white/30 font-medium py-3 rounded-xl transition-all duration-200"
                    >
                        <i class="fas fa-broom mr-2"></i>清空
                    </button>
                </div>
            </div>
        </div>

        <!-- 输出区域 -->
        <div class="glass-effect rounded-2xl shadow-xl p-6 card-hover">
            <h2 class="text-2xl font-semibold text-white mb-6">
                    <i class="fas fa-file-export mr-2"></i>解压结果与过程
                </h2>

                <div class="space-y-6">
                    <!-- 解压过程 -->
                    <div>
                        <label class="block text-white font-medium mb-3">解压过程追踪:</label>
                        <div id="processDisplay" class="bg-black/20 p-4 rounded-xl h-48 overflow-y-auto text-white/90 text-sm font-mono">
                            <div class="text-white/60">等待输入...解压过程将实时显示在这里</div>
                        </div>
                    </div>

                    <!-- 最终结果 -->
                    <div>
                        <label class="block text-white font-medium mb-3">最终解压结果:</label>
                        <div id="resultDisplay" class="bg-green-500/20 border border-green-400/30 p-4 rounded-xl min-h-20">
                            <div class="text-white/60">解压结果将显示在这里</div>
                        </div>
                    </div>

                    <!-- 统计信息 -->
                    <div class="flex flex-wrap items-center justify-between gap-4">
                        <div id="statsDisplay" class="text-white/80 text-sm">
                            <span>输入长度: 0</span>
                            <span class="mx-3">|</span>
                            <span>输出长度: 0</span>
                            <span class="mx-3">|</span>
                            <span>压缩比: 0%</span>
                        </div>

                        <div class="flex gap-2">
                            <button
                                id="copyBtn"
                                class="bg-white/20 text-white hover:bg-white/30 px-4 py-2 rounded-lg transition-colors duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
                                disabled
                            >
                                <i class="fas fa-copy mr-1"></i>复制结果
                            </button>
                            <button
                                id="saveBtn"
                                class="bg-white/20 text-white hover:bg-white/30 px-4 py-2 rounded-lg transition-colors duration-200"
                            >
                                <i class="fas fa-save mr-1"></i>保存记录
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 示例区域 -->
        <div class="mt-8 glass-effect rounded-2xl shadow-xl p-6 fade-in">
                <h3 class="text-xl font-semibold text-white mb-4">
                    <i class="fas fa-lightbulb mr-2"></i>快速示例
                </h3>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                    <div class="bg-white/20 p-4 rounded-xl cursor-pointer hover:bg-white/30 transition-all duration-200 group" onclick="loadExample('3[abc]')">
                        <p class="font-medium text-white group-hover:text-white">3[abc]</p>
                        <p class="text-white/70 text-sm mt-1">→ abcabcabc</p>
                    </div>
                    <div class="bg-white/20 p-4 rounded-xl cursor-pointer hover:bg-white/30 transition-all duration-200 group" onclick="loadExample('2[3[a]b]')">
                        <p class="font-medium text-white group-hover:text-white">2[3[a]b]</p>
                        <p class="text-white/70 text-sm mt-1">→ aaabaaab</p>
                    </div>
                    <div class="bg-white/20 p-4 rounded-xl cursor-pointer hover:bg-white/30 transition-all duration-200 group" onclick="loadExample('2[ab]3[cd]')">
                        <p class="font-medium text-white group-hover:text-white">2[ab]3[cd]</p>
                        <p class="text-white/70 text-sm mt-1">→ ababcdcdcd</p>
                    </div>
                    <div class="bg-white/20 p-4 rounded-xl cursor-pointer hover:bg-white/30 transition-all duration-200 group" onclick="loadExample('10[a]')">
                        <p class="font-medium text-white group-hover:text-white">10[a]</p>
                        <p class="text-white/70 text-sm mt-1">→ aaaaaaaaaa</p>
                    </div>
                </div>
            </div>

            <!-- 历史记录区域 -->
            <div class="mt-8 glass-effect rounded-2xl shadow-xl p-6 fade-in">
                <h3 class="text-xl font-semibold text-white mb-4">
                    <i class="fas fa-history mr-2"></i>解压历史
                </h3>
                <div id="historyDisplay" class="space-y-3 max-h-64 overflow-y-auto">
                    <div class="text-white/60 text-center py-4">暂无历史记录</div>
                </div>
            </div>
        </div>
    </div>

    <script>
        class StringDecompressor {
            constructor() {
                this.processLog = [];
                this.history = JSON.parse(localStorage.getItem('decompressHistory')) || [];
            }

            decompress(s) {
                this.processLog = [];
                this.processLog.push({type: 'info', message: `开始解压字符串: "${s}"`});

                const result = this._decompressRecursive(s, 0);

                this.processLog.push({type: 'success', message: `解压完成!最终结果: "${result}"`});
                this.saveToHistory(s, result);
                return result;
            }

            _decompressRecursive(s, depth) {
                let current = '';
                let num = 0;
                const indent = '  '.repeat(depth);

                this.processLog.push({type: 'process', message: `${indent}深度 ${depth}: 处理 "${s}"`});

                for (let i = 0; i < s.length; i++) {
                    const char = s[i];

                    if (this.isDigit(char)) {
                        num = num * 10 + parseInt(char);
                        this.processLog.push({type: 'number', message: `${indent}读取数字: ${num}`});
                    } else if (char === '[') {
                        this.processLog.push({type: 'bracket', message: `${indent}遇到 '[', 开始递归处理`});

                        let j = i + 1;
                        let bracketCount = 1;

                        while (j < s.length && bracketCount > 0) {
                            if (s[j] === '[') bracketCount++;
                            if (s[j] === ']') bracketCount--;
                            j++;
                        }

                        const innerStr = s.substring(i + 1, j - 1);
                        const innerResult = this._decompressRecursive(innerStr, depth + 1);

                        this.processLog.push({type: 'recursion', message: `${indent}递归返回: "${innerResult}" × ${num}`});
                        current += innerResult.repeat(Math.max(1, num));
                        this.processLog.push({type: 'current', message: `${indent}当前结果: "${current}"`});

                        num = 0;
                        i = j - 1;
                    } else if (char === ']') {
                        break;
                    } else {
                        current += char;
                        this.processLog.push({type: 'character', message: `${indent}添加字符 "${char}", 当前: "${current}"`});
                    }
                }

                return current;
            }

            isDigit(char) {
                return char >= '0' && char <= '9';
            }

            getProcessLog() {
                return this.processLog;
            }

            saveToHistory(input, output) {
                const record = {
                    input,
                    output,
                    timestamp: new Date().toLocaleString(),
                    inputLength: input.length,
                    outputLength: output.length,
                    compressionRatio: output.length > 0 ? ((1 - input.length / output.length) * 100).toFixed(1) : 0
                };

                this.history.unshift(record);
                this.history = this.history.slice(0, 20);
                localStorage.setItem('decompressHistory', JSON.stringify(this.history));
            }

            getHistory() {
                return this.history;
            }

            clearHistory() {
                this.history = [];
                localStorage.removeItem('decompressHistory');
            }
        }

        // DOM 加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            const decompressor = new StringDecompressor();
            const decompressBtn = document.getElementById('decompressBtn');
            const clearBtn = document.getElementById('clearBtn');
            const inputString = document.getElementById('inputString');
            const processDisplay = document.getElementById('processDisplay');
            const resultDisplay = document.getElementById('resultDisplay');
            const statsDisplay = document.getElementById('statsDisplay');
            const copyBtn = document.getElementById('copyBtn');
            const saveBtn = document.getElementById('saveBtn');
            const historyDisplay = document.getElementById('historyDisplay');

            // 加载历史记录
            function loadHistory() {
                const history = decompressor.getHistory();
                if (history.length === 0) {
                    historyDisplay.innerHTML = '<div class="text-white/60 text-center py-4">暂无历史记录</div>';
                    return;
                }

                historyDisplay.innerHTML = history.map(record => `
                        <div class="bg-white/10 p-4 rounded-lg border border-white/20">
                            <div class="flex justify-between items-start mb-2">
                                <code class="text-white/90 bg-white/20 px-2 py-1 rounded text-sm">${record.input}</code>
                                <span class="text-white/60 text-xs">${record.timestamp}</span>
                            </div>
                            <div class="text-white text-sm">→ ${record.output}</div>
                            <div class="flex gap-4 text-white/60 text-xs mt-2">
                                <span>输入: ${record.inputLength} 字符</span>
                                <span>输出: ${record.outputLength} 字符</span>
                                <span>压缩比: ${record.compressionRatio}%</span>
                            </div>
                        </div>
                    `).join('');
            }

            // 执行解压
            decompressBtn.addEventListener('click', function() {
                const input = inputString.value.trim();
                if (!input) {
                    showNotification('请输入压缩字符串!', 'error');
                    return;
                }

                try {
                    const result = decompressor.decompress(input);
                    const processLog = decompressor.getProcessLog();

                    // 显示解压过程
                    processDisplay.innerHTML = processLog.map(log => {
                        const colorClass = {
                            info: 'text-blue-300',
                            success: 'text-green-300',
                            process: 'text-yellow-300',
                            number: 'text-purple-300',
                            bracket: 'text-pink-300',
                            recursion: 'text-indigo-300',
                            character: 'text-teal-300',
                            current: 'text-orange-300'
                        }[log.type] || 'text-white';

                        return `<div class="py-1 border-b border-white/10 ${colorClass}">${log.message}</div>`;
                    }).join('');

                    // 显示最终结果
                    resultDisplay.innerHTML = `<div class="text-green-300 font-mono text-lg break-all">${result}</div>`;

                    // 更新统计信息
                    const inputLen = input.length;
                    const outputLen = result.length;
                    const ratio = outputLen > 0 ? ((1 - inputLen / outputLen) * 100).toFixed(1) : 0;

                    statsDisplay.innerHTML = `
                        <span>输入长度: ${inputLen}</span>
                        <span class="mx-3">|</span>
                        <span>输出长度: ${outputLen}</span>
                        <span class="mx-3">|</span>
                        <span>压缩比: ${ratio}%</span>
                    `;

                    // 启用复制按钮
                    copyBtn.disabled = false;
                    copyBtn.classList.remove('bg-white/20');
                    copyBtn.classList.add('bg-green-500/30', 'hover:bg-green-500/40');

                    // 加载更新后的历史记录
                    loadHistory();

                    showNotification('解压成功!', 'success');

                } catch (error) {
                    processDisplay.innerHTML = `<div class="text-red-300">错误: ${error.message}</div>`;
                    resultDisplay.innerHTML = `<div class="text-red-300">解压失败</div>`;
                    statsDisplay.innerHTML = `<span class="text-red-300">解析错误</span>`;
                    showNotification('解压失败,请检查输入格式!', 'error');
                }
            });

            // 清空输入
            clearBtn.addEventListener('click', function() {
                inputString.value = '';
                inputString.focus();
                showNotification('输入已清空', 'info');
            });

            // 复制结果
            copyBtn.addEventListener('click', function() {
                const resultText = resultDisplay.textContent;
                if (!resultText || resultText === '解压结果将显示在这里') {
                    showNotification('没有可复制的内容!', 'error');
                    return;
                }

                navigator.clipboard.writeText(resultText).then(() => {
                        const originalText = copyBtn.innerHTML;
                        copyBtn.innerHTML = '<i class="fas fa-check mr-1"></i>已复制';
                        copyBtn.classList.remove('bg-green-500/30', 'hover:bg-green-500/40');
                        copyBtn.classList.add('bg-green-600', 'text-white');

                        setTimeout(() => {
                            copyBtn.innerHTML = originalText;
                            copyBtn.classList.remove('bg-green-600', 'text-white');
                            copyBtn.classList.add('bg-white/20');
                        }, 2000);

                        showNotification('结果已复制到剪贴板!', 'success');
                    }).catch(() => {
                        showNotification('复制失败!', 'error');
                    });
                });

            // 保存记录
            saveBtn.addEventListener('click', function() {
                const input = inputString.value.trim();
                const result = resultDisplay.textContent;

                if (!input || result === '解压结果将显示在这里') {
                    showNotification('没有可保存的记录!', 'error');
                    return;
                }

                showNotification('记录已保存!', 'success');
            });

            // 示例加载功能
            window.loadExample = function(example) {
                inputString.value = example;
                decompressBtn.click();
            };

            // 回车键触发解压
            inputString.addEventListener('keypress', function(e) {
                if (e.key === 'Enter' && e.ctrlKey) {
                    decompressBtn.click();
                }
            });

            // 通知函数
            function showNotification(message, type = 'info') {
                const notification = document.createElement('div');
                const bgColor = {
                    success: 'bg-green-500',
                    error: 'bg-red-500',
                    info: 'bg-blue-500'
                }[type];

                notification.className = `fixed top-4 right-4 ${bgColor} text-white px-6 py-3 rounded-xl shadow-lg transition-all duration-300 fade-in`;
                notification.innerHTML = `<i class="fas fa-${type === 'success' ? 'check' : type === 'error' ? 'exclamation-triangle' : 'info-circle'} mr-2"></i>${message}`;

                document.body.appendChild(notification);

                setTimeout(() => {
                    notification.style.opacity = '0';
                    notification.style.transform = 'translateX(100%)';
                    setTimeout(() => {
                        document.body.removeChild(notification);
                    }, 300);
                }, 3000);
            }

            // 初始化加载历史记录
            loadHistory();
        });
    </script>
</body>
</html>
<code_end>
原文