c# 封裝“度分秒”與弧度之間的轉換 以及datagridview控件的應用


代碼:

datagridview   計算

 private void button1_Click(object sender, EventArgs e)

 

        {
            string[] sd = new string[dataGridView1.RowCount];//新建一個字符串數組存放觀測角度的原始值
            double[] sdr = new double[dataGridView1.RowCount];//新建一個雙精度的數組存放觀測角度的弧度值
            double[] cr= new double[dataGridView1.RowCount];//新建一個雙精度的數組存放計算的坐標方位角
            double sum = 0;
            cr[0] = dmstorad(Convert.ToString(dataGridView1.Rows[0].Cells[2].Value));
            //獲取第一個坐標方位角,並將其轉換成弧度,放入cr[]數組第一個元素中
            dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].Value = "總和";
                for(int i=1;i<dataGridView1.RowCount-2;i++)//從第二行開始循環計算 觀測角度累加值 坐標方位角 直到倒數第三行
                {
                sd[i]=Convert .ToString (dataGridView1.Rows[i].Cells[1].Value);//獲取觀測角度的原始值,並放入sd[]數組中
                    sdr[i]=dmstorad(sd[i]);//將觀測角度的原始值轉換成弧度值
                    sum+=sdr[i];
                    cr[i]=cr[i-1]+sdr[i]-Math .PI;//計算坐標方位角
                    if(cr[i]>=Math .PI *2)
                    { cr[i]-=Math .PI *2;}
                    else if (cr[i]<0.0)
                    {cr[i]+=Math .PI *2;}
                    dataGridView1.Rows[i].Cells[2].Value =radtodms (cr[i]);//將計算出來的坐標方位角賦值給對應的表格元素
                }
            dataGridView1.Rows[dataGridView1.RowCount-1].Cells [1].Value =radtodms (sum);
        

}

  //封裝角度轉弧度

        public double dmstorad(string s)
        {
            string[] ss = s.Split(new char[3] { '°', '′', '″' }, StringSplitOptions.RemoveEmptyEntries);//分割字符串,刪除空字符
            double[] d = new double[ss.Length];//新建一個雙精度的數值數組
            for (int i = 0; i < d.Length; i++)
                d[i] = Convert.ToDouble(ss[i]);//將度分秒存入雙精度的數值數組中
            double sign = d[0] >= 0.0 ? 1.0 : -1.0;//判斷正負
            double rad = 0;
            if (d.Length == 1)//根據數組長度進行判斷計算
                rad = (Math.Abs(d[0]) )* Math.PI / 180;//將度取絕對值,並轉換成弧度
            else if (d.Length == 2)
                rad = ((Math.Abs(d[0])) + d[1] / 60) * Math.PI / 180;
            else
                rad = ((Math.Abs(d[0])) + d[1] / 60 + d[2] / 60 / 60) * Math.PI / 180;
            rad = sign * rad;//弧度前邊添加正負號
            return rad;//返回弧度值
        }


  //弧度轉角度
        public string radtodms(double rad)
        {
            double sign = rad >= 0.0 ? 1.0 : -1.0;//判斷正負
            rad = Math.Abs(rad) * 180 / Math.PI;//將弧度取絕對值並轉化為度
            double[] d = new double[3];//新建一個長度為3的數組
            d[0] = (int)rad;//取整獲取度
            d[1] = (int)((rad - d[0]) * 60);//取整獲取分
            d[2] = (rad - d[0] - d[1] / 60) * 60 * 60;//獲取秒不取整
            d[2] = Math.Round(d[2], 2);//將秒保留兩位小數
            if (d[2] == 60)
            {
                d[1] += 1;
                d[2] -= 60;
                if (d[1] == 60)
                {
                    d[0] += 1;
                    d[1] -= 60;
                }
            }
            d[0] = sign * d[0];//度前添加正負號
            string s = Convert.ToString(d[0]) + "°" + Convert.ToString(d[1]) + "′" + Convert.ToString(d[2]) + "″";
            //將度分秒賦值給文本框,並添加°′″
            return s;
        }

退出:

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        

 

 

 

 




 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM