阿宽

Nothing is more powerful than habit!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#实现矩阵转换功能

Posted on 2008-11-06 23:53  宽田  阅读(1206)  评论(0编辑  收藏  举报

    实现下图矩阵转换功能。

    矩阵由一个4*4的文本框数据表示,在相应文本框中输入矩阵元的值,单击“转换”按钮,完成矩阵的转换。

 

 代码如下:

public partial class Form1 : Form
    {
        TextBox[,] tbList 
= null;
        
public Form1()
        {
            InitializeComponent();
            
//得到界面的矩型TexbBox控件
            tbList = new TextBox[44] {
                {txb11,txb12,txb13,txb14},
                {txb21,txb22,txb23,txb24},
                {txb31,txb32,txb33,txb34},
                {txb41,txb42,txb43,txb44},
            };
        }
        
/// <summary>
        
/// 转换开关
        
/// </summary>
        string sSwitch = "0";

        
private void Convert()
        {   
            
if (sSwitch=="0")
            {
                sSwitch 
= "1";
                btnConvert.Text 
= "返原";
            }
            
else
            {
                sSwitch 
= "0";
                btnConvert.Text 
= "转换";
            }

            
string[,] sList = new string[44];
            
//得到界面所有值
            for (int iRow = 0; iRow < 4; iRow++)
            {
                
for (int iCol = 0; iCol < 4; iCol++)
                {
                    sList[iRow, iCol] 
= tbList[iRow, iCol].Text;
                }
            }

            
string sTemp = "";
            
//得到倒置结果
            for (int iRow = 0; iRow < 4; iRow++)
            {
                
for (int iCol = iRow; iCol < 4; iCol++)
                {
                    sTemp 
= sList[iRow, iCol];
                    sList[iRow, iCol] 
= sList[iCol, iRow];
                    sList[iCol, iRow] 
= sTemp;
                }
            }
            
//界面显示
            for (int iRow = 0; iRow < 4; iRow++)
            {
                
for (int iCol = 0; iCol < 4; iCol++)
                {
                    tbList[iRow, iCol].Text 
= sList[iRow, iCol];
                }
            }
        }

        
private void btnConvert_Click(object sender, EventArgs e)
        {
            Convert();
        }

        
private void btnCancel_Click(object sender, EventArgs e)
        {
            
//清除所有值
            for (int iRow = 0; iRow < 4; iRow++)
            {
                
for (int iCol = 0; iCol < 4; iCol++)
                {
                    tbList[iRow, iCol].Text 
= "";
                }
            }
        }
    }