測繪程序設計基礎 實驗4 CSU
實驗4 類的創建
(大地四邊形類設計)
(工具:VS2010)
一、 實驗目的
• 掌握面向對象編程基本思想
• 掌握VC++.net中創建類
• 掌握建立和使用對象
• 掌握運算符號重載
• 理解類的繼承和多態性
二、實驗內容與要求
設計一個大地四邊形類,注意大地四邊形的基本屬性,功能上只要求能夠設置和返回已知點座標、8個觀測角度、待定點近似座標計算以及閉合差的計算。
三、設計與實現:
3.1 設計思路:
3.2 界面設計及屬性:
3.3主要代碼:
3.3.1文件:<Support.h>
#pragma once
#include "Angle.h"
#include "math.h"
#include "matrix.h"
class Support
{
private:
Angle Angles[8];
Angle Results[8];
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
double x4;
double y4;
public:
Support(void);
~Support(void);
void ForeIntersecPos(double Xa,double Ya, double Xb,double Yb,
double Alfa,double Beta, double &Xp,double &Yp);
int SplitStringArray(CString str, char split, CStringArray& aStr);
//讀取數據至數組
void read(CString strdate,Support &p);
//對數據進行處理
void out(CString &strResult,Support &p,double Sigma);
void pocess(CString strdate,CString &strResult);
//輸出
};
3.3.2文件:<Support.cpp>
/***************************************************************************
* 文件名:<Support.cpp> *
* *
* 描述:大地四邊形 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月5日 代碼格式修改 張 *
* *
* 外部過程: *
* *
/**************************************************************************/
/***************************************************************************
* 名字:void ForeIntersecPos *
* *
* 描述:前方交會函數,利用引用賦值 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月5日 引用該函數 張 *
* 參數: 1.double Xa *
* 2.double Ya *
* 3.double Xb *
* 4.double Yb *
* 5.double Alfa *
* 6.double Beta *
* 7.double &Xp *
* 8.double &Yp *
* 返回值:無返回值 *
* *
* 注: *
/**************************************************************************/
void Support::ForeIntersecPos(double Xa,double Ya, double Xb,double Yb, double Alfa,double Beta, double &Xp,double &Yp)
{
double ctgA, ctgB;
//計算角a,b的反正切值
ctgA = 1 / tan(Alfa);
ctgB = 1 / tan(Beta);
//計算前方交會定位值
Xp = ((Xa * ctgB + Xb * ctgA) + (Yb - Ya)) / (ctgA + ctgB);
Yp = ((Ya * ctgB + Yb * ctgA) + (Xa - Xb)) / (ctgA + ctgB);
}
/***************************************************************************
* 名字:double CSupport::SplitStringArray(CString str, char split, CStringArray& aStr) *
* *
* 描述:字符串分割函數 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年3月20日 創建該函數 張 *
* 參數: 1.CString str *
* 2.char split *
* 3.CStringArray& aStr *
* 返回值:int類型數據 返回n *
* *
* 注: *
/**************************************************************************/
int Support::SplitStringArray(CString str, char split, CStringArray& aStr)
{
int startIdx = 0;
int idx = str.Find(split, startIdx);
aStr.RemoveAll();//先清空
while (-1 != idx)
{
CString sTmp = str.Mid(startIdx, idx - startIdx);
aStr.Add(sTmp);
startIdx = idx + 1;
idx = str.Find(split, startIdx);
}
CString sTmp = str.Right(str.GetLength() - startIdx);
if (! sTmp.IsEmpty())
aStr.Add(sTmp);
return aStr.GetSize();
}
/***************************************************************************
* 名字:void read(CString strdate,Support &p) *
* *
* 描述:CString類型讀取函數 調用了函數SplitStringArray *
* 將點位座標放入對象p,角度放入Angles *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月4日 創建該函數 張 *
* 參數: 1.CString strdate *
* 2.Support &p *
* 返回值:無 *
* *
* 注: *
/**************************************************************************/
void Support::read(CString strdate,Support &p)
{
int iLine;
CStringArray aStrLine;
iLine=SplitStringArray(strdate,13,aStrLine);
if(iLine=0)
{
AfxMessageBox(_T("請輸入數據!"));
}
CStringArray aStrTmp;
int n;
n=SplitStringArray(aStrLine[0],',',aStrTmp);
p.x1=_tstof(aStrTmp[1]);
p.y1=_tstof(aStrTmp[2]);
n=SplitStringArray(aStrLine[1],',',aStrTmp);
p.x2=_tstof(aStrTmp[1]);
p.y2=_tstof(aStrTmp[2]);
for(int i=3;i<=10;i++)
{
n=SplitStringArray(aStrLine[i],',',aStrTmp);
Angles[i-3]=Angle(_tstof(aStrTmp[1]),DMS);
}
}
/***************************************************************************
* 名字:void out(CString &strResult,Support &p,double Sigma) *
* *
* 描述:CString類型輸出函數 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月4日 創建該函數 張 *
* 參數: 1.CString &strResult *
* 2.Support &p *
* 3.double Sigma //是單位權中誤差 *
* 返回值:無 *
* *
* 注: *
/**************************************************************************/
void Support::out(CString &strResult,Support &p,double Sigma)
{
strResult.Format(_T("%s\t%s\t%s\t%s\t\r\n"),
_T("點號"),
_T(" X(m) "),
_T("Y(m)"),
_T("")
);
CString strOutput;
strOutput.Format(_T("%s\r\n %s%.4lf\t%.4lf\t%s\r\n %s%.4lf\t%.4lf\t%s\r\n %s%.4lf\t%.4lf\t%s\r\n %s%.4lf\t%.4lf\t%s\r\n %s\r\n%.4lf%s\r\n %s\r\n %s\r\n"),
_T("大地四邊形條件平差結果"),
_T("(已知點)A:"),p.x1,p.y1,_T(""),
_T("(已知點)B:"),p.x2,p.y2,_T(""),
_T("(未知點)C:"),p.x3,p.y3,_T(""),
_T("(未知點)D:"),p.x4,p.y4,_T(""),
_T("單位權中誤差 "),
Sigma,_T("(”)"),
_T("觀測值平差結果:"),
_T("序號 平差後的角度(DMS)")
);
strResult=strResult+strOutput;
for(int z=0;z<8;z++)
{
strOutput.Format(_T("%d\t%.4f\r\n"),z+1,Results[z](DMS));
strResult=strResult+strOutput;
}
}
/***************************************************************************
* 名字:void pocess(CString strdate,CString &strResult) *
* *
* 描述:大地四邊形主函數,調用了函數read()、out()兩個主函數 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月4日 創建該函數 張 *
* 參數: 1.CString strdate *
* 2.CString &strResult *
* 返回值:無 *
* *
* 注: *
/**************************************************************************/
void Support::pocess(CString strdate,CString &strResult)
{
//建立一個對象
Support p;
read(strdate,p);
//定權
matrix P;
P.SetSize(8,8);
for(int i=0;i<8;i++)
{
P(i,i) = 1;
}
//條件方程
matrix A;
A.SetSize(4,8);
for(int i=0;i<8;i++)
{
A(0,i)=1;
}
for(int i=0;i<4;i++)
{
A(1,i)=1;
}
A(2,0)=1;
A(2,1)=1;
A(2,6)=1;
A(2,7)=1;
A(3,0)=1/tan(Angles[0](RAD)+Angles[7](RAD))-1/tan(Angles[0](RAD));
A(3,3)=1/tan(Angles[3](RAD))-1/tan(Angles[3](RAD)+Angles[4](RAD));
matrix W;
W.SetSize(4,1);
W(0,0) = ( Angles[0](DEG) + Angles[1](DEG) + Angles[2](DEG) + Angles[3](DEG)+ Angles[4](DEG) + Angles[5](DEG) + Angles[6](DEG) + Angles[7](DEG) - 360 ) * 3600;
W(1,0) = ( Angles[0](DEG) + Angles[1](DEG) + Angles[2](DEG) + Angles[3](DEG) - 180 ) * 3600;
W(2,0) = ( Angles[0](DEG) + Angles[1](DEG) + Angles[6](DEG) + Angles[7](DEG) - 180 ) * 3600;
W(3,0) = ( 1 - ( sin(Angles[0](RAD)) * sin(Angles[6](RAD)) * sin( Angles[3](RAD)+ Angles[4](RAD) ) )/ ( sin(Angles[3](RAD)) * sin( Angles[0](RAD)+ Angles[7](RAD) ) * sin(Angles[5](RAD)) ) ) * Ro;
// N(Naa) K(K聯繫數矩陣) V(V改正數)
matrix N,InvN;
matrix K;
matrix Zero(4,1);
matrix V;
N=A*P.Inv()*~A;
InvN=N.Inv();
//K=Zero-InvN*W;
K = Zero-InvN * W;
V=P.Inv()*~A*K;
//L=L'+V 計算改正後的觀測值
for(int i=0;i<8;i++)
{
Results[i](DEG)=Angles[i](DEG)+V(i,0)/3600;
}
//前方交會計算待定點座標
double dCAB = Angles[1](RAD) + Angles[2](RAD);
double dABC = Angles[0](RAD);
double dDAB = Angles[1](RAD);
double dABD = Angles[0](RAD) + Angles[7](RAD);
ForeIntersecPos(p.x1,p.y1,p.x2,p.y2,dCAB,dABC,p.x3,p.y3);
ForeIntersecPos(p.x1,p.y1,p.x2,p.y2,dDAB,dABD,p.x4,p.y4);
matrix Omiga;
Omiga =~V*P*V;
double Sigma;
Sigma=sqrt(Omiga(0,0)/4);
out(strResult,p,Sigma);
}
3.3.3文件:< RS_110_zhang_SY4_01Dlg.cpp >
/***************************************************************************
* 文件名:<RS_110_zhang_SY4_01Dlg.cpp> *
* *
* 描述:大地四邊形對話框文件 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月5日 代碼格式修改 張 *
* *
* 外部過程: *
* *
/**************************************************************************/
void CRS_110_zhang_SY4_01Dlg::OnBnClickedWork()
{
// TODO: 在此添加控件通知處理程序代碼
UpdateData(true);
Support k;
k.pocess(strdate,strResult);
UpdateData(false);
}
void CRS_110_zhang_SY4_01Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知處理程序代碼
UpdateData(true);
strdate.Format(_T(""));
strResult.Format(_T(""));
UpdateData(false);
}
3.4運行結果
3.5設計技巧:
通過類調用,調用了矩陣matrix、角度類,節省大量時間。