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

C++行程长度解码问题:无法处理多位数(禁用字符串/数组)

Fixing Multi-Digit Number Handling in Your C++ Decoder

Got it, let's break down why your current code struggles with multi-digit counts like 12 in a12bhr, and fix it without using strings or arrays.

The Problem with Your Current Code

Your code only handles single-digit numbers. When it encounters 12 in a12bhr:

  1. It reads 1, sets counter=1, does nothing (since counter>1 is false), then updates vorigeKar to 1.
  2. Next it reads 2, sets counter=2, then outputs vorigeKar (which is now 1) once.
  3. Finally it reads b and outputs it.

The result is a1bhr instead of the expected aaaaaaaaaaaaabhr—totally wrong, because it treats 1 and 2 as separate counts, not the single number 12.

The Solution: Accumulate Multi-Digit Numbers On-the-Fly

Since you can't use strings or arrays, we'll build the full number using integer arithmetic. When we hit a digit, we keep reading consecutive digits and combine them into a single integer (e.g., 1 + 2 becomes 12). Then we use that integer to repeat the previous character the correct number of times.

Here's the corrected code:

#include <fstream>
using namespace std;

int main() {
    ifstream invoer("encoded.txt");
    ofstream uitvoer("decoded.txt");
    
    if (!invoer.is_open() || !uitvoer.is_open()) {
        return 1; // Handle file open failure
    }

    char kar, vorigeKar;
    invoer.get(kar); // Read first character before starting loop

    while (!invoer.eof()) {
        if (kar >= '0' && kar <= '9') {
            int count = 0;
            // Accumulate all consecutive digits into a single number
            while (kar >= '0' && kar <= '9') {
                count = count * 10 + (kar - '0');
                invoer.get(kar);
            }
            // Put the non-digit character back into the input stream
            invoer.unget();
            // Output the previous character (count-1) times
            // We subtract 1 because we already output the character once earlier
            for (int i = 1; i < count; ++i) {
                uitvoer.put(vorigeKar);
            }
            // Read the non-digit character to process in the next loop iteration
            invoer.get(kar);
        } else if (kar == '/') {
            // Handle escape character: read the next character and output it
            invoer.get(kar);
            uitvoer.put(kar);
            vorigeKar = kar;
            invoer.get(kar);
        } else {
            // Regular character: output it and track it as the previous character
            uitvoer.put(kar);
            vorigeKar = kar;
            invoer.get(kar);
        }
    }

    invoer.close();
    uitvoer.close();
    return 0;
}

Key Fixes Explained

  1. Multi-Digit Accumulation:
    • When we hit a digit, we enter an inner loop that keeps reading digits, updating count with count = count * 10 + (kar - '0'). This turns sequences like 1 followed by 2 into the integer 12.
  2. Unget Non-Digit Characters:
    • After reading all digits, we hit a non-digit character. We use invoer.unget() to put this character back into the input stream so the outer loop can process it normally.
  3. Correct Repetition:
    • We output count-1 copies of vorigeKar because we already output the original character once before hitting the number (e.g., a12 already had a output, so we add 11 more as to make 12 total).
  4. Loop Initialization:
    • We read the first character before starting the loop to avoid common eof() edge cases where the loop runs an extra time after the last character is read.

Testing with Your Examples

  • For a12bhr: The code accumulates 12 as the count, outputs 11 extra as, then processes b, h, r normally—resulting in aaaaaaaaaaaaabhr.
  • For hel2o: It accumulates 2, outputs 1 extra l, resulting in hello.
  • For a10jkle: It accumulates 10, outputs 9 extra as, resulting in aaaaaaaaaaajkle.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 15:07:31