测绘程序设计基础 实验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设计技巧:

 创建多个类,层次分明。
 使用指针动态开辟数组,较为方便

代码虽多不要贪杯~

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值