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

C++如何在结构体中声明二维数组并传入函数完成数值输入

问题修复说明
  • 修正函数声明与定义不匹配的问题,统一函数名与参数列表
  • 补充switch语句中每个case的break关键字,避免执行逻辑串行
  • 调整结构体成员定义,矩阵运算结果(和、差、乘积、标量乘结果)均修改为2x2二维int数组,逆矩阵为浮点类型二维数组
  • 修正switch分支中的错误写法,将函数声明改为正确的函数调用
  • 调整函数参数为结构体引用,避免二维数组传参的麻烦,同时保证修改可以同步到结构体实例
  • 移除无意义的局部Array数组定义,统一使用传入的结构体实例操作矩阵数据
修正后可运行的基础版本代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Array
{
  string name1, name2;
  int scalar;
  int sum[2][2];
  int difference[2][2];
  int scalar_matrix[2][2];
  int product[2][2];
  double matrix_inverse[2][2];
  int matrix1[2][2];
  int matrix2[2][2];
};

// 函数声明
void get_matrix_info(Array &mat); 
void get_scalar(Array &mat);
void calc_sum(Array &mat);
void calc_diff(Array &mat);
void scalar_mult(Array &mat);
void calc_prod(Array &mat);
void calc_inv(Array &mat);

Array matrix;
fstream myfile;

int main() 
{
  int input;
  cout << "               数组程序菜单\n"
       << "请选择对应数字执行操作:\n";
  cout << " 1. 输入矩阵名称和元素\n";
  cout << " 2. 输入标量值\n";
  cout << " 3. 计算两个矩阵的和\n";
  cout << " 4. 计算两个矩阵的差\n";
  cout << " 5. 计算标量乘法\n";
  cout << " 6. 计算矩阵乘法\n";
  cout << " 7. 计算矩阵逆矩阵\n";
  cout << "输入选项: ";
  cin >> input;

  switch (input)
  {
    case 1: 
      get_matrix_info(matrix);
      break;
    case 2: 
      get_scalar(matrix);
      break;
    case 3:
      calc_sum(matrix);
      break;
    case 4:
      calc_diff(matrix);
      break;
    case 5:
      scalar_mult(matrix);
      break;
    case 6:
      calc_prod(matrix);
      break;
    case 7:
      calc_inv(matrix);
      break;
    default:
      cout << "输入选项无效" << endl;
  }
  return 0;
}

void get_matrix_info(Array &mat)
{
  cout << "输入矩阵1的名称: " << endl;
  cin >> mat.name1;
 
  cout << "输入矩阵1的4个元素: " << endl;
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
       cout << "输入第" << i+1 << "行第" << j+1 << "个元素: " << endl;
       cin >> mat.matrix1[i][j];
    }
  }
  
  cout << "输入矩阵2的名称: " << endl;
  cin >> mat.name2;

  cout << "输入矩阵2的4个元素: " << endl;
  for(int x=0;x<2;x++)
  {
    for(int y=0;y<2;y++)
    {
       cout << "输入第" << x+1 << "行第" << y+1 << "个元素: " << endl;
       cin >> mat.matrix2[x][y];
    }
  }
  cout << "矩阵信息录入完成" << endl;
}

void get_scalar(Array &mat)
{
  cout << "输入标量整数值: ";
  cin >> mat.scalar;
  cout << "标量录入完成" << endl;
}

void calc_sum(Array &mat)
{
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      mat.sum[i][j] = mat.matrix1[i][j] + mat.matrix2[i][j];
    }
  }
  // 输出结果
  cout << mat.name1 << " + " << mat.name2 << "的结果为: " << endl;
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      cout << mat.sum[i][j] << "\t";
    }
    cout << endl;
  }
}

void calc_diff(Array &mat)
{
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      mat.difference[i][j] = mat.matrix1[i][j] - mat.matrix2[i][j];
    }
  }
  // 输出结果
  cout << mat.name1 << " - " << mat.name2 << "的结果为: " << endl;
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      cout << mat.difference[i][j] << "\t";
    }
    cout << endl;
  }
}

void scalar_mult(Array &mat)
{
  // 默认和矩阵1做标量乘,可根据需求调整为矩阵2
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      mat.scalar_matrix[i][j] = mat.scalar * mat.matrix1[i][j];
    }
  }
  // 输出结果
  cout << mat.scalar << " * " << mat.name1 << "的结果为: " << endl;
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      cout << mat.scalar_matrix[i][j] << "\t";
    }
    cout << endl;
  }
}

void calc_prod(Array &mat)
{
  // 初始化乘积矩阵为0
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      mat.product[i][j] = 0;
      for(int k=0;k<2;k++)
      {
        mat.product[i][j] += mat.matrix1[i][k] * mat.matrix2[k][j];
      }
    }
  }
  // 输出结果
  cout << mat.name1 << " * " << mat.name2 << "的结果为: " << endl;
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      cout << mat.product[i][j] << "\t";
    }
    cout << endl;
  }
}

void calc_inv(Array &mat)
{
  // 默认计算矩阵1的逆矩阵,可调整为矩阵2
  int det = mat.matrix1[0][0] * mat.matrix1[1][1] - mat.matrix1[0][1] * mat.matrix1[1][0];
  if(det == 0)
  {
    cout << mat.name1 << "行列式为0,不存在逆矩阵" << endl;
    return;
  }
  double inv_det = 1.0 / det;
  mat.matrix_inverse[0][0] = inv_det * mat.matrix1[1][1];
  mat.matrix_inverse[0][1] = inv_det * (-mat.matrix1[0][1]);
  mat.matrix_inverse[1][0] = inv_det * (-mat.matrix1[1][0]);
  mat.matrix_inverse[1][1] = inv_det * mat.matrix1[0][0];
  // 输出结果
  cout << mat.name1 << "的逆矩阵为: " << endl;
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      printf("%.2f\t", mat.matrix_inverse[i][j]);
    }
    cout << endl;
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 21:00:05