C#数据区间合并问题

XJAVASunjava 2012-04-22 11:26:44
最近在做项目的时候遇到一个这样的问题:

list<point>类型中存入了很多区间,Point.x存入的是开始位置,Point.Y存入的是结束位置。list<point>中存入了很多这样的

点,例如1--4,3---8,9--20,17--25.

现在的问题是要把这些重合的段落从新合并成一个区间,例如上边四个区间应该合并为:1--8和9--25两个区间从新添加到

List<point>中,请问各位大侠有什么好的办法没?
...全文
220 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2012-04-22
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Point> data = new List<Point>()
{
new Point() { X = 1, Y = 4 },
new Point() { X = 3, Y = 8 },
new Point() { X = 9, Y = 20 },
new Point() { X = 17, Y = 25 }
};
data.ForEach(x =>
{
if (data.Where(y => y != x).Any(y => y.X <= x.Y && y.Y >= x.X))
{
var desc = data.Where(y => y != x).First(y => y.X <= x.Y && y.Y >= x.X);
x.X = desc.X > x.X ? x.X : desc.X;
x.Y = desc.Y > x.Y ? desc.Y : x.Y;
}
});
data = data.GroupBy(x => new { x.X, x.Y }).Select(x => new Point() { X = x.Key.X, Y = x.Key.Y }).ToList();
foreach (var item in data)
{
Console.WriteLine(item);
}
}
}
class Point
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return string.Format("X = {0}, Y = {1}.", X, Y);
}
}
}


X = 1, Y = 8.
X = 9, Y = 25.
Press any key to continue . . .
XJAVASunjava 2012-04-22
  • 打赏
  • 举报
回复
膜拜大侠,谢谢了...
基于c#CP3平面网严密平差数据处理 using System; using System.Collections.Generic; using System.Collections;//使用动态数组需要添加的语句 using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using ParaSet; using CSAccelerateMatrix; using Trans; using CSUsualFun; namespace FreeStaAdj { public partial class Form1 : Form { static string Path = "C:\\Documents and Settings\\HB\\桌面\\春儿数据-120907"; string[] KPName;//存储已知点点号 double[,] KPXYZ;//存储已知点三维坐标 string[] StaKName;//存储测站点点号 double[,] StaKXYZ;//存储测站点已知三维坐标 public class StaData { public string StaName;//测站名 public int Num;//观测的CPIII点数 public string[] CP3Name = new string[12];//CPIII自由测站学习点数一般不会超过12个,此处也可用动态数组存储 public double[,] LVS = new double[12, 3]; }; ArrayList StaList = new ArrayList(); public struct DLVS//用于存储方差分量估计各次的方差估值 { public double DL,DV,DS; }; //以下为默认先验平差参数 double PriLm=0.5,PriSa=1.0,PriSb=1.0;//水平方向中误差,测距固定误差和比例误差 int DMaxNum = 10;//最大方差分量估计次数 double DLimitValue = 0.01;//方差分量估计默认收敛阀值 bool IsD=true;//true表示进行方差分量估计 bool SP = true;//true表示A+B*S bool Ma = true;//true表示验后单位权中误差 double LVRatio = 2.0;//水平方向 和 天顶距 权的比值 string AdjMethord = "普通";//三维平差方法 const double Pe = 0.0174532925199433;//Math.PI/180.0,便于后面简化运算 public Form1() { InitializeComponent(); } public double Cal_DRatio(double a,double b,double c) { double x, y; if (a>=b) { x=a; y =b; } else { x = b; y=a; } if (xc) { y=c; } return x / y;//三个数中的最大值:最小值 } public double atan(double y, double x)//Math.Atan2的返回值区间(-PI,PI] { double p = Math.Atan2(y, x); if (p<0) { return p+2*Math.PI; } else { return p; } } public double SubRad(double y, double x)//返回两个弧度(角度)的差值 { double p =y-x; if (p < 0) { return p + 6.28318530717959; } else { return p; } } public void GetXYZ(string PName,ref double x,ref double y,ref double z,string[] KPName, double[,] KPXYZ) { for (int i=0;i3.14)//出现359 59 59的情况 { ljk -= 2 * Math.PI; } return ljk * 206264.8062471; } public void Cal_LVk(double[] LV, double[,] BL, double[] lL, double[] X, int n) { for (int i = 0; i < n; i++) { LV[i] = -1.0 + BL[i, 0] * X[2] + BL[i, 1] * X[3] - lL[i]; } } public void Cal_LV(double[] LV,double[,] BL, double[] lL,double[] X,int n) { for(int i=0;i数据,需要强制类型转换获取StaList中的数据 richTextBox1.Text += string.Format("{0,4}", D.StaName) + string.Format("{0,17:f4}", StaAppZ[i]) + string.Format("{0,23:f4}", StaZ[i]) + string.Format("{0,17:f2}", (StaZ[i] - StaAppZ[i]) * 10000) + "\n"; } richTextBox1.Text += "\n-------------------------------------------------\n\n"; //输出大气折光系数估值 richTextBox1.Text += "测站 大气折光系数估值\n"; for (i = 0; i < StaList.Count; i++) { StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 richTextBox1.Text += string.Format("{0,4}", D.StaName) + string.Format("{0,14:f2}", K[i]) +"\n"; } richTextBox1.Text += "\n-------------------------------------------------\n\n"; richTextBox1.Text += " 既有坐标(0) | 近似坐标(1) | 平差坐标(2) | 坐标较差[(1)-(0)] | 坐标较差[(2)-(0)]\n"; richTextBox1.Text += "点名 | X(m) Y(m) Z(m) | X(m) Y(m) Z(m) | X(m) Y(m) Z(m) | dx(mm) dy(mm) dz(mm) | dx(mm) dy(mm) dz(mm)\n"; for (i = 0; i < StaKName.Length; i++) { richTextBox1.Text += string.Format("{0,4}", StaKName[i]) + " | " + string.Format("{0:0.0000}", StaKXYZ[i, 0]) + " " + string.Format("{0:0.0000}", StaKXYZ[i, 1]) + " " + string.Format("{0:0.0000}", StaKXYZ[i, 2]) + " | "; for (j = 0; j < StaList.Count; j++) { StaData D = (StaData)StaList[j]; if (D.StaName == StaKName[i]) { richTextBox1.Text += string.Format("{0:0.0000}", AppXYZ[j, 0]) + " " + string.Format("{0:0.0000}", AppXYZ[j, 1]) + " " + string.Format("{0:0.0000}", AppXYZ[j, 2]) + " | "; richTextBox1.Text += string.Format("{0:0.0000}", AdjXYZ[j, 0]) + " " + string.Format("{0:0.0000}", AdjXYZ[j, 1]) + " " + string.Format("{0:0.0000}", AdjXYZ[j, 2]) + " |"; richTextBox1.Text += string.Format("{0,6:f1}", (AppXYZ[j, 0] - StaKXYZ[i, 0]) * 1000) + string.Format("{0,7:f1}", (AppXYZ[j, 1] - StaKXYZ[i, 1]) * 1000) + string.Format("{0,7:f1}", (AppXYZ[j, 2] - StaKXYZ[i, 2]) * 1000) + " |"; richTextBox1.Text += string.Format("{0,6:f1}", (AdjXYZ[j, 0] - StaKXYZ[i, 0]) * 1000) + string.Format("{0,7:f1}", (AdjXYZ[j, 1] - StaKXYZ[i, 1]) * 1000) + string.Format("{0,7:f1}", (AdjXYZ[j, 2] - StaKXYZ[i, 2]) * 1000) + "\n"; break; } } } //以下输出方差分量估计信息 richTextBox1.Text += "\n-------------------------------------------------------------\n"; if (IsD0) { for (i = 0; i < D0List.Count; i++) { StaData D = (StaData)StaList[i]; richTextBox1.Text += "测站 " + D.StaName + " 各次方差分量估计 各类观测值(L V S)的方差估值如下:\n"; richTextBox1.Text += "第*次 水平方向L 天顶距V 斜距S\n"; ArrayList Di = (ArrayList)D0List[i]; for (j = 0; j < Di.Count; j++) { DLVS dvs = (DLVS)Di[j]; richTextBox1.Text += string.Format("{0,3}", j + 1); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DL); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DV); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DS) + "\n"; } richTextBox1.Text += "\n------------------------------------------------------\n"; } } else { richTextBox1.Text += "未进行方差分量估计!\n"; } } public void PrintResultabk( double[,] AppXYZ, double[,] AdjXYZ, double[] StaZ, double[] StaAppZ, double[,] AB, double[] K, ArrayList StaList, ArrayList D0List, bool IsD0, string[] StaKName, double[,] StaKXYZ) { richTextBox1.Clear(); int i, j; //先输出定向角未知数计算结果 richTextBox1.Text += "测站 定向角近似值(°′″) 定向角平差值(°′″) 改正数(″)\n"; for (i = 0; i < StaList.Count; i++) { StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 richTextBox1.Text += string.Format("{0,4}", D.StaName) + string.Format("{0,17:f4}", StaAppZ[i]) + string.Format("{0,23:f4}", StaZ[i]) + string.Format("{0,17:f2}", (StaZ[i] - StaAppZ[i]) * 10000) + "\n"; } richTextBox1.Text += "\n-------------------------------------------------\n\n"; //输出 固定误差 和 比例误差 richTextBox1.Text += "测站 斜距固定误差(mm) 斜距比例误差(mm/km)\n"; for (i = 0; i < StaList.Count; i++) { StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 richTextBox1.Text += string.Format("{0,4}", D.StaName) + string.Format("{0,13:f2}", AB[i, 0]) + string.Format("{0,19:f2}", AB[i, 1]) + "\n"; } richTextBox1.Text += "\n-------------------------------------------------\n\n"; //输出大气折光系数估值 richTextBox1.Text += "测站 大气折光系数估值\n"; for (i = 0; i < StaList.Count; i++) { StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 richTextBox1.Text += string.Format("{0,4}", D.StaName) + string.Format("{0,14:f2}", K[i]) + "\n"; } richTextBox1.Text += "\n-------------------------------------------------\n\n"; richTextBox1.Text += " 既有坐标(0) | 近似坐标(1) | 平差坐标(2) | 坐标较差[(1)-(0)] | 坐标较差[(2)-(0)]\n"; richTextBox1.Text += "点名 | X(m) Y(m) Z(m) | X(m) Y(m) Z(m) | X(m) Y(m) Z(m) | dx(mm) dy(mm) dz(mm) | dx(mm) dy(mm) dz(mm)\n"; for (i = 0; i < StaKName.Length; i++) { richTextBox1.Text += string.Format("{0,4}", StaKName[i]) + " | " + string.Format("{0:0.0000}", StaKXYZ[i, 0]) + " " + string.Format("{0:0.0000}", StaKXYZ[i, 1]) + " " + string.Format("{0:0.0000}", StaKXYZ[i, 2]) + " | "; for (j = 0; j < StaList.Count; j++) { StaData D = (StaData)StaList[j]; if (D.StaName == StaKName[i]) { richTextBox1.Text += string.Format("{0:0.0000}", AppXYZ[j, 0]) + " " + string.Format("{0:0.0000}", AppXYZ[j, 1]) + " " + string.Format("{0:0.0000}", AppXYZ[j, 2]) + " | "; richTextBox1.Text += string.Format("{0:0.0000}", AdjXYZ[j, 0]) + " " + string.Format("{0:0.0000}", AdjXYZ[j, 1]) + " " + string.Format("{0:0.0000}", AdjXYZ[j, 2]) + " |"; richTextBox1.Text += string.Format("{0,6:f1}", (AppXYZ[j, 0] - StaKXYZ[i, 0]) * 1000) + string.Format("{0,7:f1}", (AppXYZ[j, 1] - StaKXYZ[i, 1]) * 1000) + string.Format("{0,7:f1}", (AppXYZ[j, 2] - StaKXYZ[i, 2]) * 1000) + " |"; richTextBox1.Text += string.Format("{0,6:f1}", (AdjXYZ[j, 0] - StaKXYZ[i, 0]) * 1000) + string.Format("{0,7:f1}", (AdjXYZ[j, 1] - StaKXYZ[i, 1]) * 1000) + string.Format("{0,7:f1}", (AdjXYZ[j, 2] - StaKXYZ[i, 2]) * 1000) + "\n"; break; } } } //以下输出方差分量估计信息 richTextBox1.Text += "\n-------------------------------------------------------------\n"; if (IsD0) { for (i = 0; i < D0List.Count; i++) { StaData D = (StaData)StaList[i]; richTextBox1.Text += "测站 " + D.StaName + " 各次方差分量估计 各类观测值(L V S)的方差估值如下:\n"; richTextBox1.Text += "第*次 水平方向L 天顶距V 斜距S\n"; ArrayList Di = (ArrayList)D0List[i]; for (j = 0; j < Di.Count; j++) { DLVS dvs = (DLVS)Di[j]; richTextBox1.Text += string.Format("{0,3}", j + 1); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DL); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DV); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DS) + "\n"; } richTextBox1.Text += "\n------------------------------------------------------\n"; } } else { richTextBox1.Text += "未进行方差分量估计!\n"; } } public void PrintResultab( double[,] AppXYZ, double[,] AdjXYZ, double[] StaZ, double[] StaAppZ,double[,] AB, ArrayList StaList, ArrayList D0List, bool IsD0, string[] StaKName, double[,] StaKXYZ) { richTextBox1.Clear(); int i, j; //先输出定向角未知数计算结果 richTextBox1.Text += "测站 定向角近似值(°′″) 定向角平差值(°′″) 改正数(″)\n"; for (i = 0; i < StaList.Count; i++) { StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 richTextBox1.Text += string.Format("{0,4}", D.StaName) + string.Format("{0,17:f4}", StaAppZ[i]) + string.Format("{0,23:f4}", StaZ[i]) + string.Format("{0,17:f2}", (StaZ[i] - StaAppZ[i]) * 10000) + "\n"; } richTextBox1.Text += "\n-------------------------------------------------\n\n"; //输出 固定误差 和 比例误差 richTextBox1.Text += "测站 斜距固定误差(mm) 斜距比例误差(mm/km)\n"; for (i = 0; i < StaList.Count; i++) { StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 richTextBox1.Text += string.Format("{0,4}", D.StaName) + string.Format("{0,13:f2}", AB[i,0]) + string.Format("{0,19:f2}", AB[i,1]) +"\n"; } richTextBox1.Text += "\n-------------------------------------------------\n\n"; richTextBox1.Text += " 既有坐标(0) | 近似坐标(1) | 平差坐标(2) | 坐标较差[(1)-(0)] | 坐标较差[(2)-(0)]\n"; richTextBox1.Text += "点名 | X(m) Y(m) Z(m) | X(m) Y(m) Z(m) | X(m) Y(m) Z(m) | dx(mm) dy(mm) dz(mm) | dx(mm) dy(mm) dz(mm)\n"; for (i = 0; i < StaKName.Length; i++) { richTextBox1.Text += string.Format("{0,4}", StaKName[i]) + " | " + string.Format("{0:0.0000}", StaKXYZ[i, 0]) + " " + string.Format("{0:0.0000}", StaKXYZ[i, 1]) + " " + string.Format("{0:0.0000}", StaKXYZ[i, 2]) + " | "; for (j = 0; j < StaList.Count; j++) { StaData D = (StaData)StaList[j]; if (D.StaName == StaKName[i]) { richTextBox1.Text += string.Format("{0:0.0000}", AppXYZ[j, 0]) + " " + string.Format("{0:0.0000}", AppXYZ[j, 1]) + " " + string.Format("{0:0.0000}", AppXYZ[j, 2]) + " | "; richTextBox1.Text += string.Format("{0:0.0000}", AdjXYZ[j, 0]) + " " + string.Format("{0:0.0000}", AdjXYZ[j, 1]) + " " + string.Format("{0:0.0000}", AdjXYZ[j, 2]) + " |"; richTextBox1.Text += string.Format("{0,6:f1}", (AppXYZ[j, 0] - StaKXYZ[i, 0]) * 1000) + string.Format("{0,7:f1}", (AppXYZ[j, 1] - StaKXYZ[i, 1]) * 1000) + string.Format("{0,7:f1}", (AppXYZ[j, 2] - StaKXYZ[i, 2]) * 1000) + " |"; richTextBox1.Text += string.Format("{0,6:f1}", (AdjXYZ[j, 0] - StaKXYZ[i, 0]) * 1000) + string.Format("{0,7:f1}", (AdjXYZ[j, 1] - StaKXYZ[i, 1]) * 1000) + string.Format("{0,7:f1}", (AdjXYZ[j, 2] - StaKXYZ[i, 2]) * 1000) + "\n"; break; } } } //以下输出方差分量估计信息 richTextBox1.Text += "\n-------------------------------------------------------------\n"; if (IsD0) { for (i = 0; i < D0List.Count; i++) { StaData D = (StaData)StaList[i]; richTextBox1.Text += "测站 " + D.StaName + " 各次方差分量估计 各类观测值(L V S)的方差估值如下:\n"; richTextBox1.Text += "第*次 水平方向L 天顶距V 斜距S\n"; ArrayList Di = (ArrayList)D0List[i]; for (j = 0; j < Di.Count; j++) { DLVS dvs = (DLVS)Di[j]; richTextBox1.Text += string.Format("{0,3}", j + 1); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DL); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DV); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DS) + "\n"; } richTextBox1.Text += "\n------------------------------------------------------\n"; } } else { richTextBox1.Text += "未进行方差分量估计!\n"; } } public void PrintResult( double[,] AppXYZ, double[,] AdjXYZ, double[] StaZ, double[] StaAppZ, ArrayList StaList,ArrayList D0List,bool IsD0, string[] StaKName, double[,] StaKXYZ) { richTextBox1.Clear(); int i,j; //先输出定向角未知数计算结果 richTextBox1.Text += "测站 定向角近似值(°′″) 定向角平差值(°′″) 改正数(″)\n"; for (i = 0; i < StaList.Count; i++) { StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 richTextBox1.Text += string.Format("{0,4}",D.StaName) + string.Format("{0,17:f4}",StaAppZ[i])+ string.Format("{0,23:f4}",StaZ[i]) + string.Format("{0,17:f2}",(StaZ[i]-StaAppZ[i])*10000)+"\n"; } richTextBox1.Text +="\n-------------------------------------------------\n\n"; richTextBox1.Text += " 既有坐标(0) | 近似坐标(1) | 平差坐标(2) | 坐标较差[(1)-(0)] | 坐标较差[(2)-(0)]\n"; richTextBox1.Text += "点名 | X(m) Y(m) Z(m) | X(m) Y(m) Z(m) | X(m) Y(m) Z(m) | dx(mm) dy(mm) dz(mm) | dx(mm) dy(mm) dz(mm)\n"; for (i = 0; i < StaKName.Length; i++) { richTextBox1.Text += string.Format("{0,4}", StaKName[i]) + " | " + string.Format("{0:0.0000}", StaKXYZ[i, 0]) + " " + string.Format("{0:0.0000}", StaKXYZ[i, 1]) + " " + string.Format("{0:0.0000}", StaKXYZ[i, 2]) + " | "; for (j = 0; j < StaList.Count;j++) { StaData D = (StaData)StaList[j]; if (D.StaName == StaKName[i]) { richTextBox1.Text += string.Format("{0:0.0000}", AppXYZ[j, 0]) + " " + string.Format("{0:0.0000}", AppXYZ[j, 1]) + " " + string.Format("{0:0.0000}", AppXYZ[j, 2]) + " | "; richTextBox1.Text += string.Format("{0:0.0000}", AdjXYZ[j, 0]) + " " + string.Format("{0:0.0000}", AdjXYZ[j, 1]) + " " + string.Format("{0:0.0000}", AdjXYZ[j, 2]) + " |"; richTextBox1.Text += string.Format("{0,6:f1}", (AppXYZ[j, 0] - StaKXYZ[i, 0]) * 1000) + string.Format("{0,7:f1}", (AppXYZ[j, 1] - StaKXYZ[i, 1]) * 1000) + string.Format("{0,7:f1}", (AppXYZ[j, 2] - StaKXYZ[i, 2]) * 1000) + " |"; richTextBox1.Text += string.Format("{0,6:f1}", (AdjXYZ[j, 0] - StaKXYZ[i, 0]) * 1000) + string.Format("{0,7:f1}", (AdjXYZ[j, 1] - StaKXYZ[i, 1]) * 1000) + string.Format("{0,7:f1}", (AdjXYZ[j, 2] - StaKXYZ[i, 2]) * 1000) + "\n"; break; } } } //以下输出方差分量估计信息 richTextBox1.Text += "\n-------------------------------------------------------------\n"; if (IsD0) { for (i = 0; i < D0List.Count;i++ ) { StaData D = (StaData)StaList[i]; richTextBox1.Text+="测站 "+D.StaName+" 各次方差分量估计 各类观测值(L V S)的方差估值如下:\n"; richTextBox1.Text += "第*次 水平方向L 天顶距V 斜距S\n"; ArrayList Di = (ArrayList)D0List[i]; for (j = 0; j < Di.Count;j++ ) { DLVS dvs = (DLVS)Di[j]; richTextBox1.Text +=string.Format("{0,3}",j + 1); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DL); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DV); richTextBox1.Text += string.Format("{0,12:f3}", dvs.DS) + "\n"; } richTextBox1.Text +="\n------------------------------------------------------\n"; } } else { richTextBox1.Text += "未进行方差分量估计!\n"; } } public void FillNSk(double[] NS, double[] WS, int Index, StaData D,//Index表示第几个测站 double[] PS, string[] KPName, double[,] KPXYZ, double[,] StaAppXYZ, double[,] BS, double[] lS) { //用距离观测值填充NS和WS矩阵 double Stax = StaAppXYZ[Index, 0]; double Stay = StaAppXYZ[Index, 1]; double Staz = StaAppXYZ[Index, 2]; double x = 0, y = 0, z = 0; double a, b, c, S0, ls;//dx,dy,dz误差方程系数,近似距离,距离常数项 for (int i = 0; i < D.Num; i++) { GetXYZ(D.CP3Name[i], ref x, ref y, ref z, KPName, KPXYZ); S0 = Math.Sqrt(Math.Pow(x - Stax, 2) + Math.Pow(y - Stay, 2) + Math.Pow(z - Staz, 2)); ls = D.LVS[i, 2] - S0; a = (Stax - x) / S0; b = (Stay - y) / S0; c = (Staz - z) / S0; BS[i, 0] = a; BS[i, 1] = b; BS[i, 2] = c; NS[5] += PS[i] * a * a; NS[8] += PS[i] * a * b; NS[9] += PS[i] * b * b; NS[12] += PS[i] * a * c; NS[13] += PS[i] * b * c; NS[14] += PS[i] * c * c; ls *= 1000; lS[i] = ls; ls *= PS[i]; WS[2] += a * ls; WS[3] += b * ls; WS[4] += c * ls; } } public void FillNSab(double[] NS, double[] WS, int Index, StaData D,//Index表示第几个测站 double[] PS, string[] KPName, double[,] KPXYZ, double[,] StaAppXYZ, double[,] BS, double[] lS) { //用距离观测值填充NS和WS矩阵 double Stax = StaAppXYZ[Index, 0]; double Stay = StaAppXYZ[Index, 1]; double Staz = StaAppXYZ[Index, 2]; double x = 0, y = 0, z = 0; double a, b, c, S0, S0Km,ls;//dx,dy,dz误差方程系数,近似距离,距离常数项 for (int i = 0; i < D.Num; i++) { GetXYZ(D.CP3Name[i], ref x, ref y, ref z, KPName, KPXYZ); S0 = Math.Sqrt(Math.Pow(x - Stax, 2) + Math.Pow(y - Stay, 2) + Math.Pow(z - Staz, 2)); ls = D.LVS[i, 2] - S0; a = (Stax - x) / S0; b = (Stay - y) / S0; c = (Staz - z) / S0; BS[i, 0] = a; BS[i, 1] = b; BS[i, 2] = c; S0Km = S0 / 1000;//将斜距单位从m转换为km BS[i, 3] = S0Km; NS[2] += PS[i] * a * a; NS[4] += PS[i] * a * b; NS[5] += PS[i] * b * b; NS[7] += PS[i] * a * c; NS[8] += PS[i] * b * c; NS[9] += PS[i] * c * c; NS[11] -= PS[i] * a; NS[12] -= PS[i] * b; NS[13] -= PS[i] * c; NS[14] += PS[i]; NS[16] -= PS[i] * a * S0Km; NS[17] -= PS[i] * b * S0Km; NS[18] -= PS[i] * c * S0Km; NS[19] += PS[i] * S0Km; NS[20] += PS[i] * S0Km * S0Km; ls *= 1000; lS[i] = ls; ls *= PS[i]; WS[1] += a * ls; WS[2] += b * ls; WS[3] += c * ls; WS[4] -= ls; WS[5] -= S0Km * ls; } } public void FillNS(double[] NS, double[] WS, int Index,StaData D,//Index表示第几个测站 double[] PS,string[] KPName, double[,] KPXYZ, double[,] StaAppXYZ, double[,] BS,double[] lS) { //用距离观测值填充NS和WS矩阵 double Stax = StaAppXYZ[Index, 0]; double Stay = StaAppXYZ[Index, 1]; double Staz = StaAppXYZ[Index, 2]; double x = 0, y = 0, z = 0; double a, b, c,S0,ls;//dx,dy,dz误差方程系数,近似距离,距离常数项 for (int i = 0; i < D.Num; i++) { GetXYZ(D.CP3Name[i], ref x, ref y, ref z, KPName, KPXYZ); S0=Math.Sqrt(Math.Pow(x-Stax,2)+Math.Pow(y-Stay,2)+Math.Pow(z-Staz,2)); ls = D.LVS[i, 2] - S0; a = (Stax - x) / S0; b = (Stay - y) / S0; c = (Staz - z) / S0; BS[i, 0] = a; BS[i, 1] = b; BS[i, 2] = c; NS[2] += PS[i] * a * a; NS[4] += PS[i] * a * b; NS[5] += PS[i] * b * b; NS[7] += PS[i] * a * c; NS[8] += PS[i] * b * c; NS[9] += PS[i] * c * c; ls *= 1000; lS[i] = ls; ls *=PS[i]; WS[1] += a * ls; WS[2] += b * ls; WS[3] += c * ls; } } public void CalPolXYZ(StaData D,double[] XY,double[] dH)//计算测站坐标系下各点的坐标 { int i; double L,V,S,Sij; for (i=0;i数据ToolStripMenuItem1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Multiselect = false; openFileDialog1.Title = "导入CPIII已知坐标"; openFileDialog1.Filter = "文本文档(*.txt;*.csv)|*.txt;*.csv" + "|所有文件(*.*)|*.*"; openFileDialog1.InitialDirectory = @Path; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.Clear(); string fname = openFileDialog1.FileName; StreamReader sr = new StreamReader(fname, Encoding.Default); richTextBox1.Text = sr.ReadToEnd().ToString(); sr.Close(); String line; int linecount = 0; StreamReader A0 = new StreamReader(fname); while ((line = A0.ReadLine())!= null) { if (line.Trim().Length>0) { linecount++; } } A0.Close(); KPName = new string[linecount]; KPXYZ = new double[linecount, 3]; int k = 0, j = 0;//j记录逗号下标+1 StreamReader A1 = new StreamReader(fname); while ((line = A1.ReadLine()) != null) { if (line.Trim().Length>0) { KPName[k] = GetData(line, ref j); KPXYZ[k, 1] =double.Parse(GetData(line, ref j));//原文件格式是YXZ KPXYZ[k, 0] =double.Parse(GetData(line, ref j));//原文件格式是YXZ KPXYZ[k, 2] = double.Parse(GetData(line, ref j)); k++; j = 0; } } A1.Close(); } } private void 导入观测数据ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Multiselect = false; openFileDialog1.Title = "导入观测文件"; openFileDialog1.Filter = "文本文档(*.txt;*.csv)|*.txt;*.csv" + "|所有文件(*.*)|*.*"; openFileDialog1.InitialDirectory = @Path; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.Clear(); string fname = openFileDialog1.FileName; StreamReader sr = new StreamReader(fname, Encoding.UTF8); richTextBox1.Text = sr.ReadToEnd().ToString(); sr.Close(); String line; int linecount = 0; StreamReader A0 = new StreamReader(fname); while ((line = A0.ReadLine()) != null) { if (line.Trim().Length > 0) { linecount++; } } A0.Close(); int k = 0; StaList.Clear();//清空原来的数据 StreamReader A1 = new StreamReader(fname); line = A1.ReadLine(); while (line!=null) { if (line.Trim().Length > 0) { if (line[3]=='-') { StaData D=new StaData(); D.StaName=GetStaName(line); line = A1.ReadLine();//跳过"序号 水平方向观测值 天顶距 斜距" while ((line = A1.ReadLine()) != null) { line = line.Trim(); if (line.Length > 0) { if (line[3]!='-') { GetLVS(line, ref D.CP3Name[k], ref D.LVS[k, 0], ref D.LVS[k, 1], ref D.LVS[k, 2]); D.LVS[k, 0] *= 0.9; //gon -> ″ D.LVS[k, 1] *= 0.9; //gon -> ″ D.LVS[k,2]-=0.0344;//减去棱镜常数 k++; } else { D.Num=k; StaList.Add(D); k = 0; break; } } } if (k>0)//最后一个测站的数据 { D.Num = k; StaList.Add(D); } } } else { line = A1.ReadLine(); } } A1.Close(); } } private void 导入测站ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Multiselect = false; openFileDialog1.Title = "导入既有测站坐标"; openFileDialog1.Filter = "文本文档(*.txt;*.dat)|*.txt;*.dat" + "|所有文件(*.*)|*.*"; openFileDialog1.InitialDirectory = @Path; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.Clear(); string fname = openFileDialog1.FileName; StreamReader sr = new StreamReader(fname, Encoding.UTF8); richTextBox1.Text = sr.ReadToEnd().ToString(); sr.Close(); String line; int linecount = 0; StreamReader A0 = new StreamReader(fname); while ((line = A0.ReadLine()) != null) { if (line.Trim().Length > 0) { linecount++; } } A0.Close(); StaKName = new string[linecount]; StaKXYZ = new double[linecount, 3]; int k = 0; StreamReader A1 = new StreamReader(fname); while ((line = A1.ReadLine())!= null) { //原文件格式是YXZ GetLVS(line, ref StaKName[k], ref StaKXYZ[k, 1], ref StaKXYZ[k, 0], ref StaKXYZ[k, 2]); k++; } A1.Close(); } } private void 参数设置ToolStripMenuItem_Click(object sender, EventArgs e) { SetPa f1=new SetPa(); f1.ShowDialog(this); this.PriLm = double.Parse(f1.textBox1.Text); this.PriSa = double.Parse(f1.textBox2.Text); this.PriSb = double.Parse(f1.textBox3.Text); this.DMaxNum = int.Parse(f1.textBox4.Text); this.DLimitValue = double.Parse(f1.textBox5.Text); this.IsD =f1.checkBox1.Checked; this.SP = f1.radioButton1.Checked; this.Ma = f1.radioButton4.Checked; this.AdjMethord = f1.comboBox1.Text; this.LVRatio = double.Parse(f1.comboBox2.Text); } private void 三维平差ToolStripMenuItem_Click(object sender, EventArgs e) { //以下进行测站三维平差-只针对CPIII自由测站的三维解算 double[,] StaAppXYZ=new double[StaList.Count,3];//测站概略坐标数组 double[,] StaAdjXYZ = new double[StaList.Count, 3];//测站概略坐标数组 double[] StaZ = new double[StaList.Count];//测站定向角未知数数组 double[] StaAppZ = new double[StaList.Count];//测站定向角未知数数组 double[] K = new double[StaList.Count];//大气折光系数k数组 double[,] AB = new double[StaList.Count, 2];//固定误差 和 比例误差数组 CalStaAppXY(StaAppXYZ,StaList,KPName,KPXYZ);//计算测站点近似坐标 CopyStaAppXY(StaAppXYZ, StaAdjXYZ);//将近似坐标拷贝一个副本 double z0=0;//z0为某测站定向角未知数近似值,单位为弧度 ArrayList D0List = new ArrayList();//存储各次方差分量估值的数组 //平差方法包括:普通 球气差 距离误差 球气差&距离误差 四种 switch (AdjMethord) { case "普通": { //未知参数改正数顺序为 定向角z,X,Y,Z int i; for (i = 0; i < StaList.Count; i++) { double[] NL = new double[10];//水平方向 法方程系数矩阵 double[] NV = new double[10];//天顶距 法方程系数矩阵 double[] NS = new double[10];//边长 法方程系数矩阵 double[] N = new double[10];//法方程系数矩阵 double[] WL = new double[4];//水平方向 法方程常数项 double[] WV = new double[4];//天顶距 法方程常数项 double[] WS = new double[4];//天顶距 法方程常数项 double[] W = new double[4];//法方程常数项 double[] X = new double[4];//未知参数改正数顺序为z,X,Y,Z StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 //以下对边长定权 double[] PS = new double[D.Num]; Cal_SWeight(PS, PriLm, PriSa, PriSb, D, SP); double[,] BL=new double[D.Num,2];//用于v=Bx-l计算v double[,] BV=new double[D.Num,3]; double[,] BS=new double[D.Num,3]; double[] lL=new double[D.Num];//用于v=Bx-l计算l double[] lV=new double[D.Num]; double[] lS=new double[D.Num]; double[] LV=new double[D.Num];//用于存储残差 double[] VV=new double[D.Num]; double[] SV=new double[D.Num]; int IterNum = 0;//记录迭代次数 double DL, DV, DS,RLS; ArrayList TempD0 = new ArrayList();//存储各测站方差分量估计各次各分量的方差值 do { IterNum++; if (IterNum > DMaxNum) { break;//超过自定义迭代次数则跳出循环 } //以下填充N和W,此处的三个函数实际上合并成一个函数可以简化运算!但时间限制,有待继续完善 FillNS(NS,WS,i,D,PS,KPName,KPXYZ,StaAdjXYZ,BS,lS); FillNL(NL, WL, i, D, ref z0, KPName, KPXYZ, StaAdjXYZ, BL, lL); FillNV(NV, WV, i, D, LVRatio, KPName, KPXYZ, StaAdjXYZ, BV, lV); AccelerateMatrix A = new AccelerateMatrix(); A.AddN(N, NL, NS, 10); A.AddN(N, NV, 10); A.AddN(W, WL, WS, 4); A.AddN(W, WV, 4); A.SPDMatrixInverse(ref N, 4); A.Cal_InvNW(N, W, 4, ref X); Cal_LV(LV,BL,lL,X,D.Num); Cal_SV(SV,BS,lS,X,D.Num); Cal_VV(VV,BV,lV,X,D.Num); UsualFun UF = new UsualFun(); StaAppZ[i] = UF.ChangeRadToDMS(z0);//存储定向角未知数近似值 StaZ[i] = UF.ChangeRadToDMS(z0 + X[0] / 206264.8062471);//将定向角从rad转换为dms StaAdjXYZ[i, 0] += X[1] / 1000; StaAdjXYZ[i, 1] += X[2] / 1000; StaAdjXYZ[i, 2] += X[3] / 1000; if(!IsD) { break;//不进行Helmert方差分量估计就跳出循环 } DL=Cal_LD0(LV,D.Num,N,NL,4); DV=Cal_VD0(VV,LVRatio,D.Num,N,NV,4); DS=Cal_SD0(SV,PS,D.Num,N,NS,4); DLVS Di=new DLVS(); Di.DL = DL; Di.DV = DV; Di.DS = DS; TempD0.Add(Di); RLS=DL/ DS; for (int j = 0; j < D.Num;j++) { PS[j] *=RLS; } LVRatio*=DV/DL;//此处不理解可以推导 } while ((Math.Abs(Cal_DRatio(DL, DV, DS)- 1)) > DLimitValue); D0List.Add(TempD0); } PrintResult(StaAppXYZ, StaAdjXYZ, StaZ,StaAppZ,StaList, D0List, IsD, StaKName, StaKXYZ); break; } case "球气差": { //未知参数改正数顺序为 定向角z,k,X,Y,Z int i; for (i = 0; i < StaList.Count; i++) { double[] NL = new double[15];//水平方向 法方程系数矩阵 double[] NV = new double[15];//天顶距 法方程系数矩阵 double[] NS = new double[15];//边长 法方程系数矩阵 double[] N = new double[15];//法方程系数矩阵 double[] WL = new double[5];//水平方向 法方程常数项 double[] WV = new double[5];//天顶距 法方程常数项 double[] WS = new double[5];//天顶距 法方程常数项 double[] W = new double[5];//法方程常数项 double[] X = new double[5];//未知参数改正数顺序为z,X,Y,Z StaData D = (StaData)StaList[i];//获取各测站的数据,需要强制类型转换获取StaList中的数据 //以下对边长定权 double[] PS = new double[D.Num]; Cal_SWeight(PS, PriLm, PriSa, PriSb, D, SP); double[,] BL = new double[D.Num, 2];//用于v=Bx-l计算v double[,] BV = new double[D.Num, 4];//k,X,Y,Z double[,] BS = new double[D.Num, 3]; double[] lL = new double[D.Num];//用于v=Bx-l计算l double[] lV = new double[D.Num]; double[] lS = new double[D.Num]; double[] LV = new double[D.Num];//用于存储残差 double[] VV = new double[D.Num]; double[] SV = new double[D.Num]; int IterNum = 0;//记录迭代次数 double DL, DV, DS, RLS; ArrayList TempD0 = new ArrayList();//存储各测站方差分量估计各次各分量的方差值 do { IterNum++; if (IterNum > DMaxNum) {
1.简单形式: var q = ( from c in db.Customers select c.Phone ).Concat( from c in db.Customers select c.Fax ).Concat( from e in db.Employees select e.HomePhone ); 语句描述:返回所有消费者和雇员的电话和传真。 2.复合形式: var q = ( from c in db.Customers select new { Name = c.CompanyName, c.Phone } ).Concat( from e in db.Employees select new { Name = e.FirstName + " " + e.LastName, Phone = e.HomePhone } ); 语句描述:返回所有消费者和雇员的姓名和电话。 Union(合并) 说明:连接不同的集合,自动过滤相同项;延迟。即是将两个集合进行合并操作,过滤相同的项。 var q = ( from c in db.Customers select c.Country ).Union( from e in db.Employees select e.Country ); 语句描述:查询顾客和职员所在的国家。 Intersect(相交) 说明:取相交项;延迟。即是获取不同集合的相同项(交集)。即先遍历第一个集合,找出所有唯一的元素,然后遍历第二个集合,并将每个元素与前面找出的元素作对比,返回所有在两个集合内都出现的元素。 var q = ( from c in db.Customers select c.Country ).Intersect( from e in db.Employees select e.Country ); 语句描述:查询顾客和职员同在的国家。 Except(与非) 说明:排除相交项;延迟。即是从某集合中删除与另一个集合中相同的项。先遍历第一个集合,找出所有唯一的元素,然后再遍历第二个集合,返回第二个集合中所有未出现在前面所得元素集合中的元素。 var q = ( from c in db.Customers select c.Country ).Except( from e in db.Employees select e.Country ); 语句描述:查询顾客和职员不同的国家。 Top/Bottom操作 适用场景:适量的取出自己想要的数据,不是全部取出,这样性能有所加强。 Take 说明:获取集合的前n个元素;延迟。即只返回限定数量的结果集。 var q = ( from e in db.Employees orderby e.HireDate select e) .Take(5); 语句描述:选择所雇用的前5个雇员。 Skip 说明:跳过集合的前n个元素;延迟。即我们跳过给定的数目返回后面的结果集。 var q = ( from p in db.Products orderby p.UnitPrice descending select p) .Skip(10); 语句描述:选择10种最贵产品之外的所有产品。 TakeWhile 说明:直到某一条件成立就停止获取;延迟。即用其条件去依次判断源序列中的元素,返回符合判断条件的元素,该判断操作将在返回false或源序列的末尾结束 。 SkipWhile 说明:直到某一条件成立就停止跳过;延迟。即用其条件去判断源序列中的元素并且跳过第一个符合判断条件的元素,一旦判断返回false,接下来将不再进行判断并返回剩下的所有元素。 Paging(分页)操作 适用场景:结合Skip和Take就可实现对数据分页操作。 1.索引 var q = ( from c in db.Customers orderby c.ContactName select c) .Skip(50) .Take(10); 语句描述:使用Skip和Take运算符进行分页,跳过前50条记录,然后返回接下来10条记录,因此提供显示Products表第6页的数据。 2.按唯一键排序 var q = ( from p in db.Products where p.ProductID > 50 orderby p.ProductID select p) .Take(10); 语句描述:使用Where子句和Take运算符进行分页,首先筛选得到仅50 (第5页最后一个ProductID)以上的ProductID,然后按ProductID排序,最后取前10个结果,因此提供Products表第6页的数据。请注意,此方法仅适用于按唯一键排序的情况。 SqlMethods操作 在LINQ to SQL语句中,为我们提供了SqlMethods操作,进一步为我们提供了方便,例如Like方法用于自定义通配表达式,Equals用于相比较是否相等。 Like 自定义的通配表达式。%表示零长度或任意长度的字符串;_表示一个字符;[]表示在某范围区间的一个字符;[^]表示不在某范围区间的一个字符。比如查询消费者ID以“C”开头的消费者。 var q = from c in db.Customers where SqlMethods.Like(c.CustomerID, "C%") select c; 比如查询消费者ID没有“AXOXT”形式的消费者: var q = from c in db.Customers where !SqlMethods.Like(c.CustomerID, "A_O_T") select c; DateDiffDay 说明:在两个变量之间比较。分别有:DateDiffDay、DateDiffHour、DateDiffMillisecond、DateDiffMinute、DateDiffMonth、DateDiffSecond、DateDiffYear var q = from o in db.Orders where SqlMethods .DateDiffDay(o.OrderDate, o.ShippedDate) < 10 select o; 语句描述:查询在创建订单后的 10 天内已发货的所有订单。 已编译查询操作(Compiled Query) 说明:在之前我们没有好的方法对写出的SQL语句进行编辑重新查询,现在我们可以这样做,看下面一个例子: //1.创建compiled query NorthwindDataContext db = new NorthwindDataContext(); var fn = CompiledQuery.Compile( (NorthwindDataContext db2, string city) => from c in db2.Customers where c.City == city select c); //2.查询城市为London的消费者,用LonCusts集合表示,这时可以用数据控件绑定 var LonCusts = fn(db, "London"); //3.查询城市为Seattle的消费者 var SeaCusts = fn(db, "Seattle"); 语句描述:这个例子创建一个已编译查询,然后使用它检索输入城市的客户。

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧