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

如何将字符串数组中的数值存入二维int数组并按要求替换符号输出?

C++:提取字符串数字到二维数组并完成符号替换输出

任务要求

  • 将含符号的数字序列存入字符串数组
  • 提取序列中的一位数存入二维int数组
  • 输出时仅使用二维数组中的数字,并替换指定符号:
    • . → ;
    • | → //
    • - → =

输入输出示例

输入:

.1.2.3.|.4.5.6.|.7.8.9 
-------|-------|------- 

输出:

;1;2;3;//;4;5;6;//;7;8;9 
=======//=======//=======

当前问题

已实现输入原样输出,但无法完成数字存入二维数组及符号替换的逻辑,现有代码如下:

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
 
    int ArrayTwo[3][2] = {0}; //From Task
    string ArrayInput[2] = {""}; //From Task
 
    cout << "Input:" << endl;
 
    for (int i = 0; i < 2; i++)
    {
        cin >> ArrayInput[i];
    }
 
    cout << "Output" << endl;
    for (int i = 0; i < 2; i++)
    {
        cout << ArrayInput[i] << endl;
    }
 
 
    system("pause");
    return 0;
}

解决方案

1. 修正数组大小

原二维数组ArrayTwo[3][2]仅能存储6个数字,无法容纳输入中的9个一位数,需调整为int ArrayTwo[3][3] = {0};(对应3组,每组3个数字)。

2. 提取数字到二维数组

遍历输入字符串的每个字符,识别数字并转换为int类型存入二维数组,同时用指针跟踪当前存储位置:

#include <cctype> // 用于isdigit函数

// ... 原代码中的数组定义部分 ...

// 提取数字到二维数组
int numRow = 0; // 当前组的行索引
int numCol = 0; // 当前组的列索引
for (int i = 0; i < 2; i++) {
    for (char c : ArrayInput[i]) {
        if (isdigit(c)) {
            // 将字符数字转换为int
            ArrayTwo[numRow][numCol] = c - '0';
            numCol++;
            // 一组存满3个数字,切换到下一组
            if (numCol == 3) {
                numCol = 0;
                numRow++;
            }
        }
    }
}

3. 符号替换并输出(仅使用二维数组的数字)

重新遍历输入字符串,遇到符号则替换输出,遇到数字则从二维数组中取出对应值输出:

cout << "Output:" << endl;
// 重置指针,用于读取二维数组的数字
numRow = 0;
numCol = 0;
for (int i = 0; i < 2; i++) {
    for (char c : ArrayInput[i]) {
        if (c == '.') {
            cout << ';';
        } else if (c == '|') {
            cout << "//";
        } else if (c == '-') {
            cout << '=';
        } else if (isdigit(c)) {
            // 从二维数组取出数字输出
            cout << ArrayTwo[numRow][numCol];
            numCol++;
            if (numCol == 3) {
                numCol = 0;
                numRow++;
            }
        } else {
            // 其他字符(如空格)直接输出
            cout << c;
        }
    }
    cout << endl;
}

完整代码

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
 
int main()
{
    int ArrayTwo[3][3] = {0}; // 修正为3x3数组以容纳9个数字
    string ArrayInput[2] = {""};
 
    cout << "Input:" << endl;
    for (int i = 0; i < 2; i++)
    {
        cin >> ArrayInput[i];
    }
 
    // 提取数字到二维数组
    int numRow = 0;
    int numCol = 0;
    for (int i = 0; i < 2; i++) {
        for (char c : ArrayInput[i]) {
            if (isdigit(c)) {
                ArrayTwo[numRow][numCol] = c - '0';
                numCol++;
                if (numCol == 3) {
                    numCol = 0;
                    numRow++;
                }
            }
        }
    }
 
    // 替换符号并输出
    cout << "Output:" << endl;
    numRow = 0;
    numCol = 0;
    for (int i = 0; i < 2; i++) {
        for (char c : ArrayInput[i]) {
            if (c == '.') {
                cout << ';';
            } else if (c == '|') {
                cout << "//";
            } else if (c == '-') {
                cout << '=';
            } else if (isdigit(c)) {
                cout << ArrayTwo[numRow][numCol];
                numCol++;
                if (numCol == 3) {
                    numCol = 0;
                    numRow++;
                }
            } else {
                cout << c;
            }
        }
        cout << endl;
    }
 
    system("pause");
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 14:35:22