代码:
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();
}