測繪程序設計基礎 實驗4(2) CSU
實驗4 類的創建
(水準網設計)
(工具:VS2010)
一、 實驗目的
• 掌握面向對象編程基本思想
• 掌握VC++.net中創建類
• 掌握建立和使用對象
• 掌握運算符號重載
• 理解類的繼承和多態性
二、實驗內容與要求
要求:設計一個水準網類,注意水準的基本屬性。功能上要求:(1)能夠通過界面上的文本框按照以下格式輸入數據;(2)能夠計算未知點近似高程。
三、設計與實現:
3.1.1基本框架
3.1.2 成員關係
3.1.2 高程近似算法
3.2界面設計及屬性:
3.3主要代碼:
3.3.1文件:<Point2D.h>
/***************************************************************************
* 文件名:<Point2D.h> *
* *
* 描述:儲存已知點未知點屬性 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 代碼格式修改 *** *
* *
* 外部過程: *
* *
/**************************************************************************/
#pragma once
class CPoint2D
{
public:
CString strID;//點名
int bianhao;//程序中並沒實際作用,僅給點號賦予了一標記
double dH;//高度
int flag;//標識符,若某對象高程已知或已算出則爲1,否爲0
public:
CPoint2D(void);
~CPoint2D(void);
};
3.3.2文件:<Point2D.cpp>
#include "StdAfx.h"
#include "Point2D.h"
CPoint2D::CPoint2D(void)
{
strID=_T("");
dH=0;
flag=0;
}
CPoint2D::~CPoint2D(void)
{
}
3.3.3文件:<BasicData.h>
/***************************************************************************
* 文件名:<BasicData.h> *
* *
* 描述:儲存觀測值屬性 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 代碼格式修改 *** *
* *
* 外部過程: *
* *
/**************************************************************************/
#pragma once
#include"Point2D.h"
class CBasicData
{
public:
CString pFront;//前站點名
CString pBack;//後站點名
double dBasicH;//高度
double dDist;//路線長度
CBasicData(void);
~CBasicData(void);
};
3.3.4文件:<BasicData.cpp>
#include "StdAfx.h"
#include "BasicData.h"
CBasicData::CBasicData(void)
{
}
CBasicData::~CBasicData(void)
{
}
3.3.5文件:<Support.h>
#pragma once
/***************************************************************************
* 文件名:<CSupport.h> *
* *
* 描述:水準網類高程值近似算法 需要其餘兩個類:CBasicData、CPoint2D類 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 代碼格式修改 *** *
* *
* 外部過程: *
* *
/**************************************************************************/
#include"BasicData.h"
class CSupport
{
public:
CSupport(void);
~CSupport(void);
public:
CPoint2D *pK;
CPoint2D *pUK;
CBasicData *pBasic;
int iK;
int iUK;
int iBasic;
public://小函數
bool CSupport::compare(CString a,CString b);
int SplitStringArray(CString str, char split, CStringArray& aStr);
//CString SearchPoint(CPoint2D *pK,CPoint2D *pUK,CString ID);
int CSupport::SearchPoint1(CPoint2D *pK,CPoint2D *pUK,CString ID);
public:
void read(CString &strData);
void out(CPoint2D *pK,CPoint2D *pUK,CString &strResult);
void main(CString &strData,CString &strResult);
};
3.3.5文件:<Support.cpp>
#include "StdAfx.h"
#include "Support.h"
CSupport::CSupport(void)
{
}
/***************************************************************************
* 名字:CSupport::~CSupport(void) *
* *
* 描述:析構函數,釋放指針 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 創建該函數 *** *
* 返回值: *
* *
* 注: *
/**************************************************************************/
CSupport::~CSupport(void)
{
if(pK!=NULL)
{
delete[] pK;
pK=NULL;
}
if(pUK!=NULL)
{
delete[] pUK;
pUK=NULL;
}
if(pBasic!=NULL)
{
delete[] pBasic;
pBasic=NULL;
}
}
/***************************************************************************
* 名字:double CSupport::SplitStringArray(CString str, char split, CStringArray& aStr) *
* *
* 描述:字符串分割函數 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年3月20日 創建該函數 *** *
* 參數: 1.CString str *
* 2.char split *
* 3.CStringArray& aStr *
* 返回值:int類型數據 返回n *
* *
* 注: *
/**************************************************************************/
int CSupport::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();
}
/***************************************************************************
* 名字:CSupport::read(CString &strData) *
* *
* 描述:讀取函數,調用函數 SplitStringArray() *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 創建該函數 *** *
* 參數: 1.CString &strData *
* 返回值:讀取數據,pK、pUK、pBasic指針開啓數組中 *
* *
* 注: *
/**************************************************************************/
void CSupport::read(CString &strData)
{
int iLine;
CStringArray aStrLine;
iLine=SplitStringArray(strData,13,aStrLine);
if(iLine=0)
{
AfxMessageBox(_T("請輸入數據!"));
}
CStringArray aStrTmp;
int n;
//讀取數據1
int size1,size2,size3;
n=SplitStringArray(aStrLine[0],',',aStrTmp);
size1=_tstof(aStrTmp[0]);
pK=new CPoint2D[size1];//創建動態指針
iK=size1;
n=SplitStringArray(aStrLine[size1+1],',',aStrTmp);
size2=_tstof(aStrTmp[0]);
pUK=new CPoint2D[size2];//創建動態指針
iUK=size2;
n=SplitStringArray(aStrLine[size1+3],',',aStrTmp);
size3=_tstof(aStrTmp[0]);
pBasic=new CBasicData[size3];
iBasic=size3;
//讀取數據2
for(int i=0;i<size1;i++)
{
n=SplitStringArray(aStrLine[i+1],',',aStrTmp);
pK[i].bianhao=i;
pK[i].strID=aStrTmp[0];
pK[i].flag=1;
pK[i].dH=_tstof(aStrTmp[1]);
}
for(int i=0;i<size2;i++)
{
n=SplitStringArray(aStrLine[size1+2],',',aStrTmp);
//pUK[i].id2=(i+1)+1e6;
pK[i].bianhao=i+10000;
pUK[i].strID=aStrTmp[i];
pUK[i].flag=0;
pUK[i].dH=0;
}
for(int i=0;i<size3;i++)
{
n=SplitStringArray(aStrLine[size1+4+i],',',aStrTmp);
pBasic[i].pBack=aStrTmp[0];
pBasic[i].pFront=aStrTmp[1];
pBasic[i].dBasicH=_tstof(aStrTmp[2]);
pBasic[i].dDist =_tstof(aStrTmp[3]);
}
}
/***************************************************************************
* 名字:CSupport::SearchPoint1(CPoint2D *pK,CPoint2D *pUK,CString ID) *
* *
* 描述:尋求ID是否存在與pK、pUK數組中 調用了函數read() *
* 並返回位置 *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 創建該函數 *** *
* 參數: 1.CPoint2D *pK *
* 2.CPoint2D *pUK *
* 3.CString ID *
* 返回值:int 如果在pK數組找到返回原值i,在pUK找到返回i+10000 *
* 找不到返回-1 *
* 注: *
/**************************************************************************/
int CSupport::SearchPoint1(CPoint2D *pK,CPoint2D *pUK,CString ID)
{
for(int i=0;i<iK;i++)
{
if(compare(pK[i].strID,ID))
{
return i;
}
}
for(int i=0;i<iUK;i++)
{
if(compare(pUK[i].strID,ID))
{
return 10000+i;
}
}
return -1;
}
/***************************************************************************
* 名字:void CSupport::out(CPoint2D *pK,CPoint2D *pUK,CString &strResult) *
* *
* 描述:輸出函數將pK、pUK數組中的對象輸出 *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 創建該函數 *** *
* 參數: 1.CPoint2D *pK *
* 2.CPoint2D *pUK *
* 3.CString &strResult *
* 返回值:無 *
* 注: *
/**************************************************************************/
void CSupport::out(CPoint2D *pK,CPoint2D *pUK,CString &strResult)
{
strResult.Format(_T("%s\t%s\t%s\t%s\t\r\n"),
_T(""),
_T("--------------測量網高程近似值------------------- "),
_T(""),
_T("")
);
CString strOutput;
for(int z=0;z<iK;z++)
{
strOutput.Format(_T("%s\t%lf\r\n"),pK[z].strID,pK[z].dH);
strResult=strResult+strOutput;
}
for(int z=0;z<iUK;z++)
{
strOutput.Format(_T("%s\t%lf\r\n"),pUK[z].strID,pUK[z].dH);
strResult=strResult+strOutput;
}
}
/***************************************************************************
* 名字:bool CSupport::compare(CString a,CString b) *
* *
* 描述:比較CString數據 由於在CString 編輯框讀取時會將空格( )、回車(\n)*
* 等讀入,需要調用函數Remove()進行消除後,方可採取compare()函數比較 *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 創建該函數 *** *
* 參數: 1.CString a *
* 2.CString b *
* 返回值:bool型 返回1或0 *
* 注: *
/**************************************************************************/
bool CSupport::compare(CString a,CString b)
{
a.Remove('\r');
b.Remove('\r');
a.Remove('\n');
b.Remove('\n');
a.Remove(' ');
a.Remove(' ');
if(!a.Compare(b))
return 1;
else
return 0;
}
/***************************************************************************
* 名字:void CSupport::main(CString &strData,CString &strResult) *
* *
* 描述:主函數 調用了compare()輔助函數和函數read()、out() *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 創建該函數 *** *
* 參數: 1.CString &strData *
* 2.CString &strResult *
* 返回值:無 *
* 注: *
/**************************************************************************/
void CSupport::main(CString &strData,CString &strResult)
{
read(strData);
double ooo=0;//判斷循環次數
//創建3個CString 後文賦值 減少工作量
CString dda;
CString ddb;
CString ddc;
/*
dda=pUK[2].strID;
ddb=pBasic[3].pBack;
ddc=pBasic[2].pBack;
int d=compare(dda,ddb);
int d2=compare(dda,ddc);
*/
for(int i=0;i<iUK;i++)
{
ooo++;
if(pUK[i].flag==0)
{
for(int j=0;j<iBasic;j++)
{
int t1=SearchPoint1(pK,pUK,pBasic[j].pBack);
int t2=SearchPoint1(pK,pUK,pBasic[j].pFront);
dda=pUK[i].strID;
ddb=pBasic[j].pFront;
ddc=pBasic[j].pBack;
if(compare(dda,ddb)&&t1>-1)
{
if(t1<10000)
{
pUK[i].dH=pBasic[j].dBasicH+pK[t1].dH;
}
else
{
pUK[i].dH=pBasic[j].dBasicH+pUK[t1-10000].dH;
}
pUK[i].flag=1;
//double oooo=1;
break;
}
else if(compare(dda,ddc)&&(t2>-1))
{
if(t2<10000)
{
pUK[i].dH=pK[t2].dH-pBasic[j].dBasicH;
}
else
{
pUK[i].dH=pUK[t2-10000].dH-pBasic[j].dBasicH;
}
pUK[i].flag=1;
//double oooo=1;
break;
}
}
}
if(i==iUK-1||ooo>1000)//ooo變量避免程序循環次數過多陷入死循環
{
int jishiqi=0;
for(int m=0;m<iUK;m++)
{
if(pUK[m].flag==0)
{
i=-1;
break;
}
else
{
++jishiqi;
}
}
if(jishiqi==iUK-1)
{
break;//結束高程近似計算
}
}
}
/*
double mmm,mmm1,mmm2;
mmm=pUK[0].flag;
mmm1=pUK[1].flag;
mmm2=pUK[2].flag;*/
out(pK,pUK,strResult);
}
3.3.7文件:< CRS_110_***_實驗4_02.cpp > (僅摘取部分)
/***************************************************************************
* 文件名:< CRS_110_***_實驗4_02Dlg.cpp> *
* *
* 描述:水準網高程近似值計算對話框文件 *
* *
* 歷史:**日期** **理由** **簽名** *
* 2019年4月13日 代碼格式修改 *** *
* *
* 外部過程: *
* *
/**************************************************************************/
void CRS_110_***_實驗4_02Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知處理程序代碼
//CDialogEx::OnOK();
UpdateData(true);
CSupport k;
k.main(strData,strResult);
UpdateData(false);
}
void CRS_110_***_實驗4_02Dlg::OnBnClickedButton1Zeroing()
{
// TODO: 在此添加控件通知處理程序代碼
UpdateData(true);
strData.Format(_T(""));
strResult.Format(_T(""));
UpdateData(false);
}
3.4運行結果
3.5設計技巧:
創建多個類,層次分明。
使用指針動態開闢數組,較爲方便