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

如何将现有C++整数数组操作程序修改为字符数组版本

修改方案

你只需要调整以下几处核心代码即可完成整数数组到字符数组的修改:

  • 全局数组类型替换:将int array[10];改为char array[10];
  • 空标记适配:原代码用整数-1作为未存储/已删除的标识,char类型无法直接用-1作为可打印的占位值,我们统一替换为可见占位符'_'(你也可以换成其他你想用来标记空位的字符)
  • 同步修改所有涉及空值赋值的逻辑:将SetDefaultValues和DeleteValues函数中array[i] = -1;的语句改为array[i] = '_';

其余输入、输出、更新逻辑无需改动,C++的cin和cout会自动适配char类型的读写。


修改后完整代码

#include <stdlib.h>
#include <iostream>
using namespace std;

// 数组类型修改为char
char array[10];

void DisplayArray() {
  for (int i = 0; i < 10; i++)
    cout << "Array [ " << i << " ] = " << array[i] << endl;
}

void SetDefaultValues() {
  cout << "Default Values :" << endl;
  for (int i = 0; i < 10; i++) {
    // 默认值改为占位符
    array[i] = '_';
    cout << "array [" << i << "]"
         << "= " << array[i] << endl;
  }
}

void InsertValues() {
  cout << "Enter 10 Values " << endl;
  for (int i = 0; i < 10; i++) {
    cin >> array[i];
  }
  cout << "\n\t\t\tArray Values Inserted...  Successfully " << endl;
}

void DeleteValues() {
  cout << "Enter the Index Number To Delete Value :";
  int index;
  cin >> index;
  if (index > 9 || index < 0) {
    cout << "Invalid Index Entered-> Valid Range(0-9)" << endl;
    DeleteValues();
  } else {
    // 删除后赋值为占位符
    array[index] = '_';
  }
  cout << "\n\t\t\tArray Value Deleted...  Successfully " << endl;
}

void UpdateValues() {
  cout << "Enter Index Number to Update Value :";
  int index;
  cin >> index;
  if (index > 9 || index < 0) {
    cout << "Invalid Index Entered-> Valid Range(0-9)" << endl;
    UpdateValues();
  } else {
    cout << "Enter the New Value For Index array[ " << index << " ] = ";
    cin >> array[index];
    cout << "\n\t\t\tArray Updated...  Successfully " << endl;
  }
}

int main() {
  char option;
  SetDefaultValues();

  do {
    cout << "\t\t\tEnter 1 to Enter  Values\n\t\t\tEnter 2 to Update "
            "Values\n\t\t\tEnter 3 to Delete Values\n\n\t\t\t or Enter E to "
            "EXIT\n\n\t\t\t  Enter Option: ->  ";
    cin >> option;
    if (option == '1') {
      cout << "Insert Function Called" << endl;
      InsertValues();
      cout << "Inserted Values :" << endl;
      DisplayArray();
    } else if (option == '2') {
      UpdateValues();
      cout << "Updated Array :" << endl;
      DisplayArray();
    } else if (option == '3') {
      DeleteValues();
      cout << "Array After Deleting Values :" << endl;
      DisplayArray();
    } else if (option != 'e' && option != 'E') {
      cout << "\n\n\t\t\tSelect A Valid Option From Below\n\n";
    }
  } while (option != 'e' && option != 'E');

  system("cls");
  cout << "\n\n\n\n\n\n\n\n\t\tProgram Ended Press Any Key To Exit "
          "Screen.....\n\n\n\n\n\n\n\n\n\n\n"
       << endl;
  return 0;
}

运行说明

输入时每个位置输入单个可见字符即可(支持字母、数字、符号等),不要输入多个连续字符,否则会自动拆分填充到后续的数组位置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 20:24:03