什么都不说了直接上代码
主窗体函数
private static ABCMatrix m_ABCMatrixTest = new ABCMatrix(); Random ran = new Random(); //随机函数 private void btnJudgeABCMatrix_Click(object sender, EventArgs e) { try { GetABCMatrix("true"); MessageBox.Show(IsExist(5, m_ABCMatrixTest).ToString()); } catch (Exception) { } } private void btnGetWrongMartix_Click(object sender, EventArgs e) { try { GetABCMatrix("false"); MessageBox.Show(IsExist(5, m_ABCMatrixTest).ToString()); } catch (Exception ee) { } } ////// 获取ABCMatrix /// /// 矩阵类型 private void GetABCMatrix(string strMatrixState) { string strMatrixName = ""; int iSize = 0; try { iSize = Convert.ToInt32(txtMatrixSize.Text); strMatrixName = txtMatrixName.Text; } catch (Exception) { iSize = 5; } if (strMatrixName == "" || strMatrixName == null) { strMatrixName = "默认矩阵"; } ABCMatrix abcMatrixTest = new ABCMatrix(iSize, iSize, strMatrixName); int[,] iTestMatrix = abcMatrixTest.Members; if (strMatrixState == "true") { for (int i = 0; i < iSize; i++) { for (int j = 0; j < iSize; j++) { iTestMatrix[j, i] = i + iSize * j; } } } else { for (int i = 0; i < iSize; i++) { for (int j = 0; j < iSize; j++) { iTestMatrix[j, i] = ran.Next(0, 100); } } iTestMatrix[0, 0] = 2; iTestMatrix[0, 1] = 1; } m_ABCMatrixTest = abcMatrixTest; txtABCMatrix.Text = MatrixPrint(abcMatrixTest); } ////// 判别ABCMartix /// /// 矩阵大小 /// 传入矩阵 ///public bool IsExist(int iSize, ABCMatrix matrix) { int[,] iTestMatrix = matrix.Members; for (int i=0;i = iTestMatrix[j, i + 1]) { return false; } if (j + 1 < iSize && iTestMatrix[j, i] >= iTestMatrix[j + 1, i]) { return false; } } } return true; } /// /// 矩阵打印 /// /// 传入矩阵 ///矩阵拼接字符串 public static string MatrixPrint(ABCMatrix Ma) { string s; s = Ma.Name + ":\r\n"; int m = Ma.getM; int n = Ma.getN; int[,] a = Ma.Members; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { s += a[i, j].ToString() + "\t"; } s += "\r\n"; } return s; }
ABCMatrix.cs
int[,] abcMartix; //m行n列 int iRow, iLine; string name; public ABCMatrix() { } ////// 默认名字的矩阵 /// /// 行数 /// 列数 public ABCMatrix(int am, int an) { iRow = am; iLine = an; abcMartix = new int[iRow, iLine]; name = "Result"; } ////// 自定义名字矩阵 /// /// 行数 /// 列数 /// 矩阵名字 public ABCMatrix(int am, int an, string aName) { iRow = am; iLine = an; abcMartix = new int[iRow, iLine]; name = aName; } public int getM { get { return iRow; } } public int getN { get { return iLine; } } ////// 成员变量 /// public int[,] Members { get{return abcMartix;} set{abcMartix=value;} } ////// 矩阵的名字 /// public string Name { get { return name; } set { name = value; } }