「已注销」 2022-11-09 22:14 采纳率: 66.7%
浏览 23
已结题

用Split 和二维数组实现矩阵相乘

问题遇到的背景

我是一个C#初学者,要求写一个实现矩阵相乘的程序,要求必须用Split,不知道该如何找出用户输入矩阵的列数

用代码块功能插入代码,请勿粘贴截图

Console.WriteLine("Please input m:");
string a1 = Console.ReadLine();//输入字符串表示的矩阵
string[] b1 = a1.Split(new char[] { ';'});//行数
string[] d1= a1.Split (new char[] { ','});//逗号个数加1
string[] e1 = a1.Split(new char[] { ';', ',' }); //总数
int N1=((d1.Length-1)/b1.Length)+1;//这个是计算列数的,但是错误的
int[,] m = new int[b1.Length, N1];
int k1 = 0;
for (int i = 0; i < m.GetLength(0); i++)
{
for (int j = 0; j < m.GetLength(1); j++)
{
m[i, j] = Convert.ToInt32(e1[k1]);
k1++;
}
}

        Console.WriteLine("Please input n:");
        string a2 = Console.ReadLine();
        string[] b2 = a2.Split(new char[] { ';' });
        string[] d2 = a2.Split(new char[] { ',' });
        string[] e2 = a1.Split(new char[] { ';', ',' }); 
        int N2=((d2.Length-1)/b2.Length)+1;
        int[,] n = new int[b2.Length, N2];            
        int k2 = 0;
        for (int i = 0; i < n.GetLength(0); i++)
        {
            for (int j = 0; j < n.GetLength(1); j++)
            {                    
                n[i, j] = Convert.ToInt32(e2[k2]);
                k2++;
            }  
        }
        int[,] c = new int[4, 2];//8个数
        for(int i = 0; i < 4; i++)
        {
            for(int j = 0; j < 2; j++)
            {
                for(int k = 0; k <3; k++)
                {
                    c[i, j]+= m[i, k] * n[k, j];
                }
            }
        }
        Console.WriteLine("The result of m*n:");
        if (m.GetLength(1) != n.GetLength(0))
        {
            Console.WriteLine("Illegal operation!");
        }
        else
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.Write(" {0}", c[i, j]);

                }
            }
        }          
        Console.ReadKey();
运行结果及报错内容

输出结果是错误的

我的解答思路和尝试过的方法
我想要达到的结果【样例输入】

输出的结果应如下:
Please input m:
5,2,4;3,8,2;6,0,4;0,1,6
Please input n:
2,4;1,3;3,2

【样例输出】
The result of m*n:
24 34
20 40
24 32
19 15

  • 写回答

1条回答 默认 最新

  • 浪客 2022-11-09 23:40
    关注
    
    Console.WriteLine("Please input m:");
    string a1 = Console.ReadLine();//输入字符串表示的矩阵
    string[] b1 = a1.Split(';');//行数
    string[] d1 = b1[0].Split(',');//逗号个数加1
    //string[] e1 = a1.Split(';', ','); //总数
    int N1 = d1.Length;//这个是计算列数的,但是错误的
    int[,] m = new int[b1.Length, N1];
    int k1 = 0;
    
    for (int i = 0; i < b1.Length; i++)
    {
        d1 = b1[i].Split(',');
        for (int j = 0; j < d1.Length; j++)
        {
            m[i, j] = Convert.ToInt32(d1[j]);
        }
    
    }
    
    for (int i = 0; i < m.GetLength(0); i++)
    {
        for (int j = 0; j < m.GetLength(1); j++)
        {
            Console.Write(m[i, j] + " ");
        }
        Console.WriteLine();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月18日
  • 已采纳回答 11月10日
  • 创建了问题 11月9日

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图