「已注销」 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日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度