学生实验报告院系:测绘学院专业班级:测绘13级3班学号:2013305517学生姓名:王泽指导教师:郭辉老师2016年05月20日安徽理工大学实验报告实验课程名称:数据结构与软件开发上机实验开课院系及实验室:测绘学院红楼二楼机房实验1 编程基本知识练习实验目的:通过该实验课内容的练习,学生应掌握VB 编程的基本语法、变量的定义、数组(动态数组)的定义、VB 语言中子过程与函数的定义以及文本文件的读写等知识。
实验内容: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” ( 文本文件路径为C:\Users\ WH\Desktop\练习\data.txt”)。
输入以下内容:6,7,4,0.005A,35.418B,45.712C,25.270D,24.678在桌面上新建一“result.txt” ( 文本文件路径为C:\Users\ WH\Desktop\练习\result.txt”)。
(2)从文本文件中读数据Dim linedata as string, m_GaochaN as integer,m_Pnumber asinteger,m_knPnumber as integer,M as Double,k1 as integer'linedata 为存储文本文件一行信息的字符串变量Dim a() as String,H() as Double'a()为存储点名,H()存储高程Open“C:\Users\ WH\Desktop\练习\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)GetstationNumber (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) = nameGetstationNumber = iExit ForEnd IfNext iEnd Function(4)从文本文件中写数据(将从data.txt 读入的数据,写入到result.txt 文件中) Open“C:\Users\ WH\Desktop\ 练习\result.txt” For Output As #1outstring = outstring + str(m_GaochaN) +","outstring = outstring + str(m_Pnumber) + ","outstring = outstring + str(m_knPnumber) + ","outstring = outstring + str(M) + vbCrLfFor k1 = 1 To m_knPnumberoutstring = outstring +a(k1)+ ","+Format(H(k1), "##0.000") + vbCrLfNextPrint #1, outstring实验2 水准网平差程序设计实验目的:通过本次实验的练习,使学生掌握水准网平差中近似高程计算算法、组成误差方程系数阵B、组成误差方程常数项L及权阵生成算法。
掌握法方程的组建参数改正值的计算以及单位权中误差的计算。
掌握水准网平差中输出高程平差值和高程中误差、输出观测值及其改正数与平差值等信息并保存为平差成果文件的方法。
实验内容:1)在读入水准网数据后,进行待定点近似坐标的计算;2)基于间接平差的思想,以观测的测段高差为观测值,组成误差方程系数矩阵B 及常数项矩阵L;3)基于水准网平差中定权的方法(测段长或测站数定权),生成权阵P。
4) 利用矩阵的基本运算(调用矩阵运算函数即可)得到法方程系数矩阵及常数项矩阵。
该实验的内容其实质就是调用矩阵运算函数。
5)熟悉VB 中输出函数Print 的用法,及控制输出格式的函数的用法,如Space()、String()6)水准网平差结束后,将变量及数组中的数据输出到平差结果文件中。
实验步骤:1.读入水准网数据(1)建立文本文件并输入数据在桌面上新建一“data.txt” ( 文本文件路径为C:\Users\ WH\Desktop\练习1\data.txt”)。
输入以下内容:10,7,2,0.001A,0.000F,11.414A,B,73.795,20.4A,D,14.005,18.8A,G,14.167,15.4C,B,71.949,8.9D,B,59.780,14.2C,D,12.159,12.8C,E,15.364,9.8F,E,5.797,19.6G,E,3.044,15.1D,G,0.169,10.0在桌面上新建一“result.txt” ( 文本文件路径为C:\Users\ WH\Desktop\练习1\ out.txt”)。
Private Sub Command1_Click()(2)读入水准网数据Open "C:\Users\WH\Desktop\练习1\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))ReDim A(1 To m_knPnumber), H(1 To m_knPnumber)For k1 = 1 To m_knPnumberLine Input #1, linedatak = Split(linedata, ",")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_GaochaNLine Input #1, linedatak = Split(linedata, ",")StartP(k2) = k(0)EndP(k2) = k(1)gaocha(k2) = CDbl(k(2))Li(k2) = CDbl(k(3))NextClose #1End Sub2.编辑界面打开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() As 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 Double)'矩阵求差函数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()需转置的矩阵,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=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 + vbDefaultButton1 Exit FunctionEnd IfReDim MatrixMultiply(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'功能:实矩阵求逆的全选主元高斯-约当法'参数:n - Integer 型变量,矩阵的阶数'mtxA - Double 型二维数组,体积为n x n。