internal static int[][] multiply(Matrix A, Matrix B) //AB矩阵相乘 { if (A == null || B == null || A.tuple == null || B.tuple == null) { throw new NullReferenceException("请先创建矩阵!"); } if (A.maxcol != B.maxrow) //A列数需要等于B行数 { throw new ArgumentException("这两个矩阵不可以相乘!"); } int[][] ArrayA = MatrixMethod.toArray(A); int[][] ArrayB = MatrixMethod.toArray(B); int[][] result = new int[A.maxrow][]; //根据矩阵定义可知相乘行数等于A行数,列数等于B列数 for (int i = 0; i < A.maxrow; i++) { int[] column = new int[B.maxcol]; for (int j = 0; j < B.maxcol; j++) { column[j] = 0; //所有元素初始化为0 } result[i] = column; } for (int i = 0; i < A.maxrow; i++) { for (int j = 0; j < B.maxcol; j++) { for (int k = 0; k < B.maxrow; k++) { result[i][j] += ArrayA[i][k] * ArrayB[k][j]; //对应元素A行乘B列相加 } } } return(result); }
internal void printmatrix() //打印矩阵 { if (tuple == null) { Console.WriteLine("请先创建矩阵"); return; } Console.WriteLine("打印矩阵"); int[][] matrix = MatrixMethod.toArray(this); //转换为二维数组 DataStruct5.printarray(matrix); //打印二维数组 }
static void Main(string[] args) { Console.WriteLine("稀疏矩阵运算器"); Console.WriteLine("1.输入矩阵A"); Console.WriteLine("2.输入矩阵B"); Console.WriteLine("3.输出矩阵A及其三元组表"); Console.WriteLine("4.输出矩阵B及其三元组表"); Console.WriteLine("5.输出A+B"); Console.WriteLine("6.输出A-B"); Console.WriteLine("7.输出B-A"); Console.WriteLine("8.输出A×B"); Console.WriteLine("9.输出B×A"); Console.WriteLine("输入(1-9)操作:"); Matrix A = null; Matrix B = null; while (true) { string i = Console.ReadLine(); switch (i) { case "1": Console.WriteLine("输入矩阵A(行列不超过20,非零元数不超过行×列)"); Console.Write("请输入最大行数:(1-20):"); reinput1 : try { int row = int.Parse(Console.ReadLine()); Console.Write("请输入最大列数:(1-20):"); int col = int.Parse(Console.ReadLine()); if (row < 1 || col < 1 || row > 20 || col > 20) { throw new Exception("行列数不合法!"); } int count = row * col; Console.Write("请输入非零元数:(1-" + count + "):"); count = int.Parse(Console.ReadLine()); if (count > row * col || count < 1) { throw new Exception("非零元数不合法!"); } A = new Matrix(row, col, count); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("重新输入"); goto reinput1; } break; case "2": Console.WriteLine("输入矩阵B(行列不超过20,非零元数不超过行×列)"); Console.Write("请输入最大行数:(1-20):"); reinput2 : try { int row = int.Parse(Console.ReadLine()); Console.Write("请输入最大列数:(1-20):"); int col = int.Parse(Console.ReadLine()); if (row < 1 || col < 1 || row > 20 || col > 20) { throw new Exception("行列数不合法!"); } int count = row * col; Console.Write("请输入非零元数:(1-" + count + "):"); count = int.Parse(Console.ReadLine()); if (count > row * col || count < 1) { throw new Exception("非零元数不合法!"); } B = new Matrix(row, col, count); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("重新输入"); goto reinput2; } break; case "3": Console.WriteLine("打印矩阵A及其三元组表"); A.print(); Console.WriteLine(); A.printmatrix(); Console.WriteLine(); break; case "4": Console.WriteLine("打印矩阵B及其三元组表"); B.print(); Console.WriteLine(); B.printmatrix(); Console.WriteLine(); break; case "5": Console.WriteLine("打印矩阵A+B"); try { int[][] m = MatrixMethod.addOrMinus(A, B, 1); printarray(m); } catch (Exception e) { Console.WriteLine(e.Message); } break; case "6": Console.WriteLine("打印矩阵A-B"); try { int[][] m = MatrixMethod.addOrMinus(A, B, 0); printarray(m); } catch (Exception e) { Console.WriteLine(e.Message); } break; case "7": Console.WriteLine("打印矩阵B-A"); try { int[][] m = MatrixMethod.addOrMinus(B, A, 0); printarray(m); } catch (Exception e) { Console.WriteLine(e.Message); } break; case "8": Console.WriteLine("打印矩阵A×B"); try { int[][] m = MatrixMethod.multiply(A, B); printarray(m); } catch (Exception e) { Console.WriteLine(e.Message); } break; case "9": Console.WriteLine("打印矩阵B×A"); try { int[][] m = MatrixMethod.multiply(B, A); printarray(m); } catch (Exception e) { Console.WriteLine(e.Message); } break; default: return; } Console.WriteLine("输入(1-9)继续执行操作:"); } }