C# 矩阵作业

什么都不说了直接上代码

主窗体函数

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)
            {

            }
        }

        /// <summary>
        /// 获取ABCMatrix
        /// </summary>
        /// <param name="strMatrixState">矩阵类型</param>
        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);
        }

        /// <summary>
        /// 判别ABCMartix
        /// </summary>
        /// <param name="iSize">矩阵大小</param>
        /// <param name="matrix">传入矩阵</param>
        /// <returns></returns>
        public bool IsExist(int iSize, ABCMatrix matrix)
        {
            int[,] iTestMatrix = matrix.Members;
            for (int i=0;i<iSize ;i++ )
            {
                for (int j = 0; j < iSize; j++)
                {
                    if (i + 1 < iSize && iTestMatrix[j, i] >= iTestMatrix[j, i + 1])
                    {
                        return false;
                    }
                    if (j + 1 < iSize && iTestMatrix[j, i] >= iTestMatrix[j + 1, i])
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        
        /// <summary>
        /// 矩阵打印
        /// </summary>
        /// <param name="Ma">传入矩阵</param>
        /// <returns>矩阵拼接字符串</returns>
        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()
        {

        }

        /// <summary>
        /// 默认名字的矩阵
        /// </summary>
        /// <param name="am">行数</param>
        /// <param name="an">列数</param>
        public ABCMatrix(int am, int an)
        {
            iRow = am;
            iLine = an;
            abcMartix = new int[iRow, iLine];
            name = "Result";
        }

        /// <summary>
        /// 自定义名字矩阵
        /// </summary>
        /// <param name="am">行数</param>
        /// <param name="an">列数</param>
        /// <param name="aName">矩阵名字</param>
        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; }
        }

        /// <summary>
        /// 成员变量
        /// </summary>
        public int[,] Members
        {
            get{return abcMartix;}
            set{abcMartix=value;}
        }

        /// <summary>
        /// 矩阵的名字
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

  

posted on 2012-05-14 17:35  Jarrett.zhou  阅读(303)  评论(0编辑  收藏  举报