《程序设计实验报告(16页).doc》由会员分享,可在线阅读,更多相关《程序设计实验报告(16页).doc(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-程序设计实验报告-第 16 页学 生 实 验 报 告院 系: 测绘学院 专业班级: 测绘13级3班 学 号: 2013305517 学生姓名: 王 泽 指导教师: 郭辉老师 2016年05月20日安徽理工大学实验报告实验课程名称: 数据结构与软件开发上机实验 开课院系及实验室: 测绘学院 红楼二楼机房 院系测绘学院专业班级测绘13-3班姓 名王泽成 绩实验项目名称测量程序设计课程实验指导教师郭辉老师实验1 编程基本知识练习实验目的: 通过该实验课内容的练习,学生应掌握VB 编程的基本语法、变量的定义、数组(动态数组)的定义 、VB 语言中子过程与函数的定义以及文本文件的读写等知识。实验内容:
2、1)变量的定义 动态数组的定义与应用;2)矩阵的加、减、乘运算 (定义Sub()子过程或Function()来实现);3)数据文件的建立、数据的读取与写入。实验步骤:1.编辑界面1.1 打开VB 编程工具,进入编程主界面。1.2 在窗体上新建 “读入数据”和“输出数据”两个按钮。1.3 双击“窗体”进入代码输入界面,进行代码编辑。2.用VB 编写的源代码2.1 矩阵基本运算源码详见附录一。(1)两矩阵相加(2)两矩阵相减(3)矩阵转置(4)两矩阵相乘(5)矩阵求逆2.2 文本文件(本实验中data.txt)的读取源代码(1)建立文本文件并输入数据在桌面上新建一“data.txt” ( 文本文件
3、路径为C:Users WHDesktop练习data.txt”)。输入以下内容:6,7,4,0.005A,35.418B,45.712C,25.270D,24.678在桌面上新建一“result.txt” ( 文本文件路径为C:Users WHDesktop练习result.txt”)。(2)从文本文件中读数据Dim linedata as string, m_GaochaN as integer,m_Pnumber asinteger,m_knPnumber as integer,M as Double,k1 as integerlinedata 为存储文本文件一行信息的字符串变量Dim a
4、() as String,H() as Doublea()为存储点名,H()存储高程Open“C:Users WHDesktop练习data.txt”For Input As #1Line Input #1, linedatak = Split(linedata, ,)m_GaochaN = Val(k(0)m_Pnumber = Val(k(1)m_knPnumber = Val(k(2)M = CDbl(k(3)For k1 = 1 To m_knPnumberLine Input #1, linedatak = Split(linedata, ,)a( k1) = k(0)Getstat
5、ionNumber (a)H(k1) = CDbl(k(1)NextClose #1(3)将读入点名存储到点名数组中,且返回该点名所对应编号Function GetstationNumber(name As String)Dim i As IntegerFor i = 1 To m_PnumberIf P_Name(i) Then将待查点名与已经存入点名数组的点比较If P_Name(i) = name ThenGetstationNumber = iExit ForEnd IfElse 待查点是新的点名,将新点名放到 P_Name 数组中P_Name(i) = nameGetstationN
6、umber = iExit ForEnd IfNext iEnd Function(4)从文本文件中写数据(将从data.txt 读入的数据,写入到result.txt 文件中)Open“C:Users WHDesktop 练习result.txt” For Output As #1outstring = outstring + str(m_GaochaN) +,outstring = outstring + str(m_Pnumber) + ,outstring = outstring + str(m_knPnumber) + ,outstring = outstring + str(M)
7、+ vbCrLfFor k1 = 1 To m_knPnumberoutstring = outstring +a(k1)+ ,+Format(H(k1), #0.000) +vbCrLfNextPrint #1, outstring实验 2 水准网平差程序设计实验目的: 通过本次实验的练习,使学生掌握水准网平差中近似高程计算算法、组成误差方程系数阵B、组成误差方程常数项L及权阵生成算法。掌握法方程的组建 参数改正值的计算以及单位权中误差的计算。掌握水准网平差中输出高程平差值和高程中误差、 输出观测值及其改正数与平差值等信息并保存为平差成果文件的方法。实验内容:1)在读入水准网数据后,进行待定
8、点近似坐标的计算;2)基于间接平差的思想,以观测的测段高差为观测值,组成误差方程系数矩阵B 及常数项矩阵L;3)基于水准网平差中定权的方法(测段长或测站数定权) ,生成权阵P。4) 利用矩阵的基本运算(调用矩阵运算函数即可)得到法方程系数矩阵及常数项矩阵。 该实验的内容其实质就是调用矩阵运算函数。5)熟悉VB 中输出函数Print 的用法,及控制输出格式的函数的用法,如Space()、 String()6)水准网平差结束后,将变量及数组中的数据输出到平差结果文件中。实验步骤:1. 读入水准网数据(1)建立文本文件并输入数据在桌面上新建一“data.txt” ( 文本文件路径为C:Users W
9、HDesktop练习1data.txt”)。输入以下内容:10,7,2,0.001 A,0.000 F,11.414 A,B,73.795,20.4 A,D,14.005,18.8 A,G,14.167,15.4C,B,71.949,8.9 D,B,59.780,14.2 C,D,12.159,12.8 C,E,15.364,9.8 F,E,5.797,19.6 G,E,3.044,15.1 D,G,0.169,10.0在桌面上新建一“result.txt” ( 文本文件路径为C:Users WHDesktop练习1 out.txt”)。Private Sub Command1_Click()
10、(2)读入水准网数据Open C:UsersWHDesktop练习1data.txt For Input As #1 Line Input #1, linedata k = Split(linedata, ,) m_GaochaN = Val(k(0) m_Pnumber = Val(k(1) m_knPnumber = Val(k(2) M = CDbl(k(3)ReDim A(1 To m_knPnumber), H(1 To m_knPnumber)For k1 = 1 To m_knPnumber Line Input #1, linedata k = Split(linedata,
11、,) A(k1) = k(0) H(k1) = CDbl(k(1)NextReDim StartP(1 To m_GaochaN), EndP(1 To m_GaochaN), gaocha(1 To m_GaochaN), Li(1 To m_GaochaN)For k2 = 1 To m_GaochaN Line Input #1, linedata k = Split(linedata, ,) StartP(k2) = k(0) EndP(k2) = k(1) gaocha(k2) = CDbl(k(2) Li(k2) = CDbl(k(3)NextClose #1End Sub2. 编
12、辑界面打开VB 编程工具,进入编程主界面。在窗体上利用控件新建 “载入数据”、“开始计算”、“导出结果”、“清空”四个按钮和一个文本框。3.定义相应变量、数组和实现各功能的函数(过程)详见附录二(1)水准网中近似坐标计算(2)生成误差方程系数矩阵B 及常数项矩阵L(3)生成权阵P(4)残差及中误差计算函数(5)组成法方程及平差计算(6)平差结果输出附件1 矩阵基本运算源码(1)两矩阵相加Private Function Msum(M As Integer, n As Integer, Matrixsum() As Double, Matrix1() As Double, Matrix2() A
13、s Double)矩阵求和函数Dim i1 As Integer, i2 As IntegerReDim Matrixsum(1 To M, 1 To n)For i1 = 1 To MFor i2 = 1 To nMatrixsum(i1, i2) = Matrix1(i1, i2) + Matrix2(i1, i2)Next i2Next i1End Function(2)两矩阵相减Function Mminus(M As Integer, n As Integer, MatrixMinus() AsDouble, Matrix1() As Double, Matrix2() As Dou
14、ble)矩阵求差函数Dim i1 As Integer, i2 As IntegerReDim MatrixMinus(1 To M, 1 To n)For i1 = 1 To MFor i2 = 1 To nMatrixMinus(i1, i2) = Matrix1(i1, i2) - Matrix2(i1, i2)Next i2Next i1End Function(3)矩阵转置Function Mchange(M As Integer, n As Integer, Matrixchange() As Double, Matrix1() As Double)矩阵转置函数Matrix1()需
15、转置的矩阵,Matrixchange()为转置后的矩阵Dim i1 As Integer, i2 As IntegerReDim Matrixchange(1 To n, 1 To M) 动态分配用来存储转置后的矩阵For i1 = 1 To MFor i2 = 1 To nMatrixchange(i2, i1) = Matrix1(i1, i2)Next i2Next i1End Function(4)两矩阵相乘矩阵相乘函数, MatrixMultiply()为存储AB 后的矩阵, Matrix1()为A矩阵, Matrix2()为B 矩阵,注意矩阵相乘的顺序Mi 为行, Ni 为列,i=
16、1,2Function Mmultiply(M1 As Integer, n1 As Integer, M2 As Integer,n2 As Integer, MatrixMultiply() As Double, Matrix1() As Double,Matrix2() As Double)Dim i1 As Integer, i2 As Integer, i3 As IntegerIf n1 M2 ThenMsgBox 两矩阵不能相乘,请检查!, vbOKCancel + vbCritical + vbDefaultButton1Exit FunctionEnd IfReDim Mat
17、rixMultiply(1 To M1, 1 To n2)For i1 = 1 To M1For i2 = 1 To n2MatrixMultiply(i1, i2) = 0For i3 = 1 To n1MatrixMultiply(i1, i2) = MatrixMultiply(i1, i2) +Matrix1(i1, i3) * Matrix2(i3, i2)Next i3Next i2Next i1End Function(5)矩阵求逆Function MRinv(n As Integer, mtxA() As Double) As Boolean功能:实矩阵求逆的全选主元高斯约当法
18、参数:n - Integer 型变量,矩阵的阶数mtxA - Double 型二维数组,体积为n x n。存放原矩阵A;返回时存放其逆矩阵A-1。返回值: Boolean型,失败为False,成功为TrueReDim nIs(n) As Integer, nJs(n) As IntegerDim i As Integer, j As Integer, k As IntegerDim D As Double, P As Double全选主元,消元For k = 1 To nD = 0#For i = k To nFor j = k To nP = Abs(mtxA(i, j)If (P D) T
19、henD = PnIs(k) = inJs(k) = jEnd IfNext jNext i求解失败If (D + 1# = 1#) ThenMRinv = FalseExit FunctionEnd IfIf (nIs(k) k) ThenFor j = 1 To nP = mtxA(k, j)mtxA(k, j) = mtxA(nIs(k), j)mtxA(nIs(k), j) = PNext jEnd IfIf (nJs(k) k) ThenFor i = 1 To nP = mtxA(i, k)mtxA(i, k) = mtxA(i, nJs(k)mtxA(i, nJs(k) = PN
20、ext iEnd IfmtxA(k, k) = 1# / mtxA(k, k)For j = 1 To nIf (j k) Then mtxA(k, j) = mtxA(k, j) * mtxA(k, k)Next jFor i = 1 To nIf (i k) ThenFor j = 1 To nIf (j k) Then mtxA(i, j) = mtxA(i, j) - mtxA(i, k) *mtxA(k, j)Next jEnd IfNext iFor i = 1 To nIf (i k) Then mtxA(i, k) = -mtxA(i, k) * mtxA(k, k)Next
21、iNext k调整恢复行列次序For k = n To 1 Step -1If (nJs(k) k) ThenFor j = 1 To nP = mtxA(k, j)mtxA(k, j) = mtxA(nJs(k), j)mtxA(nJs(k), j) = PNext jEnd IfIf (nIs(k) k) ThenFor i = 1 To nP = mtxA(i, k)mtxA(i, k) = mtxA(i, nIs(k)mtxA(i, nIs(k) = PNext iEnd IfNext k求解成功MRinv = TrueEnd Function附件2 水准网平差程序计算源码(1)水准网
22、中近似坐标计算Sub ca_H() 水准网中待定点近似高程计算Dim k1 As Integer, H_N As Integer H_N 为计算出近似高程点个数Dim c1 As Integer, c2 As IntegerH_N = 0Do While H_N (m_Pnumber - m_knPnumber)For k1 = 1 To m_GaochaNc1 = StartP(k1) 观测高差起点c2 = EndP(k1) 观测高差终点If H(c1) = 0 And H(c2) 0 ThenH(c1) = H(c2) - gaocha(k1)H_N = H_N + 1End IfIf H
23、(c1) 0 And H(c2) = 0 ThenH(c2) = H(c1) + gaocha(k1)H_N = H_N + 1End IfNextLoopEnd Sub(2)生成误差方程系数矩阵B 及常数项矩阵LSub GouJianBL(H() As Double, StartP() As Integer, EndP() AsInteger) 构建误差方程系数矩阵B 与LH()为近似高程数组, StatP()为起点点号数组, EndP()为终点点号数组Dim k1 As Integer, k2 As Integer, k3 As Integer, c1 As Integer, c2As I
24、ntegerFor k1 = 1 To m_GaochaNFor k2 = 1 To (m_Pnumber - m_knPnumber)B(k1, k2) = 0NextNextFor k1 = 1 To m_GaochaNc1 = StartP(k1) 观测高差起点c2 = EndP(k1) 观测高差终点If c1 m_knPnumber And c2 m_knPnumber ThenB(k1, c2 - m_knPnumber) = 1B(k1, c1 - m_knPnumber) = -1End IfIf c1 m_knPnumber And c2 = m_knPnumber ThenB
25、(k1, c1 - m_knPnumber) = -1End IfIf c1 m_knPnumber ThenB(k1, c2 - m_knPnumber) = 1End IfL(k1, 1) = gaocha(k1) - (H(c2) - H(c1)NextEnd Sub(3)生成权阵PSub goujianP() 构建PDim k1 As Integer, k2 As IntegerFor k1 = 1 To m_GaochaNFor k2 = 1 To m_GaochaNP(k1, k2) = 0NextNextFor k1 = 1 To m_GaochaNFor k2 = 1 To m
26、_GaochaNIf Li(k1) = 0 ThenMsgBox (第 + Str(k1) + 测段长为0! )End IfIf k1 = k2 ThenP(k1, k2) = 1 / Sqr(Li(k1)End IfNextNextEnd Sub(4)残差及中误差计算函数Sub ca_V() 残差计算Dim k1 As Integer, vv As Double, c1 As Integer, c2 As Integerp_vv = 0For k1 = 1 To m_GaochaNc1 = StartP(k1)c2 = EndP(k1)vv = H(c2) - H(c1) - gaocha(
27、k1)v(k1, 1) = vvp_vv = p_vv + P(k1, k1) * vv * vvm_mu = Sqr(p_vv / (m_GaochaN - (m_Pnumber -m_knPnumber)NextEnd Sub(5)组成法方程及平差计算Private Sub Command2_Click() 平差计算及结果输出按钮ca_HGouJianBL H, StartP, EndPgoujianPMchange m_GaochaN, m_Pnumber - m_knPnumber, Matrixchange,B B 转置的计算Mmultiply (m_Pnumber - m_knPn
28、umber), m_GaochaN,m_GaochaN, m_GaochaN, MatrixMultiply, Matrixchange, PMmultiply (m_Pnumber - m_knPnumber), m_GaochaN,m_GaochaN, (m_Pnumber - m_knPnumber), Q, MatrixMultiply, BMRinv (m_Pnumber - m_knPnumber), MatrixMultiply1MRinv (m_Pnumber - m_knPnumber), QMmultiply (m_Pnumber - m_knPnumber), (m_Pn
29、umber -m_knPnumber), (m_Pnumber - m_knPnumber), m_GaochaN,MatrixMultiply2, Q, MatrixchangeMmultiply (m_Pnumber - m_knPnumber), m_GaochaN,m_GaochaN, m_GaochaN, MatrixMultiply3, MatrixMultiply2, PMmultiply (m_Pnumber - m_knPnumber), m_GaochaN,m_GaochaN, 1, dx, MatrixMultiply3, Lca_VPrintResultEnd Sub(
30、6)平差结果输出Sub PrintResult() 平差结果输出Dim outstring As String 存储保存的信息Dim qii As Double, qjj As Double, qij As Double 存储法方程系数矩阵对交线上元素Dim k1 As Integer, c1 As Integer, c2 As Integer, ml As Doubleml存储观测高程中误差,m_mu 单位权中误差CommonDialog1.Filter = All Files (*.*)|*.*|Text Files(*.txt)|*.txt|Batch Files (*.bat)|*.b
31、atCommonDialog1.ShowSaveIf CommonDialog1.FileName *.txt AndCommonDialog1.FileName ThenOpen CommonDialog1.FileName For Output As #1outstring = Space(10) & String(4, =) + 高程平差值及其精度+ String(4, =) + vbCrLfPrint #1, outstringPrint #1, 点名 近似高程 改正数 高程平差值 中误差+ vbCrLfoutstring = For k1 = 1 To m_Pnumber - m_k
32、nPnumberoutstring = outstring + P_Name(k1 + m_knPnumber) + Space(2)outstring = outstring + Format(Str(H(k1 + m_knPnumber),#0.000) + Space(2)outstring = outstring + Format(Str(dx(k1, 1), #0.0000) + outstring = outstring + Format(Str(H(k1 + m_knPnumber) + dx(k1,1), #0.000) + Space(2)qii = Q(k1, k1)out
33、string = outstring + Str(m_mu * Sqr(qii) + Space(2) + vbCrLfNextPrint #1, outstringPrint #1, 测段号 起 点 终 点 观测高差 高差平差值 观测权 中误差 + vbCrLfFor k1 = 1 To m_GaochaNc1 = StartP(k1)c2 = EndP(k1)If c1 m_knPnumber Thenc1 = c1 - m_knPnumberEnd IfIf c2 m_knPnumber Thenc2 = c2 - m_knPnumberEnd Ifqii = Q(c1, c1) qjj
34、 = Q(c2, c2) qij = Q(c1, c2)ml = Sqr(qii + qjj - 2 * qij) * m_muPrint #1, Str(k1) + Space(3) + P_Name(StartP(k1) + Space(3) +P_Name(EndP(k1) + Space(3) + Str(gaocha(k1) + Space(3) +Format(Str(gaocha(k1) + v(k1, 1), #0.000) + Space(3) +Format(Str(P(k1, k1), #0.00) + Space(3) + Str(ml) + Space(3)+ vbC
35、rLfNextClose #1End IfEnd Sub附件3 水准网平差程序设计算例水准网如图1所示,共7个高程点、10个测段观测高差,观测值的每千米观测中误差为0.001m,已知点高程和测段高差分别见表1与表2。该水准网平差数据文件如下:10,7,2,0.001A,0.000 F,11.414A,B,73.795,20.4 A,D,14.005,18.8 A,G,14.167,15.4 C,B,71.949,8.9 D,B,59.780,14.2 C,D,12.159,12.8 C,E,15.364,9.8 F,E,5.797,19.6 G,E,3.044,15.1 D,G,0.169,10.0