用vb编写一个求任意多边形面积的程序,公式如下。要求用读入数据的形式读入各点X/Y坐标。好的话追加悬赏
发布网友
发布时间:2022-05-25 11:40
我来回答
共1个回答
热心网友
时间:2023-10-09 15:17
'添加一个Command1,一个CommonDialog1Dim sinX(100) As Single '多边形坐标XDim sinY(100) As Single '多边形坐标YDim intNum As Integer '多边形边数
Private Sub OpenTxtFile(strPFile As String) '打开文件'坐标数据文件为文本文件,XY坐标间用半角逗号隔开,一个坐标点占一行,文件不能有空行On Error GoTo ThisErrDoEventsDim i As IntegerDim strTemp As StringDim strIn() As StringDim fileNUM As IntegerfileNUM = FreeFileOpen strPFile For Input As #fileNUM '打开文件i = 0Do While Not EOF(fileNUM) '执行循环直到文件尾Line Input #fileNUM, strTemp '读出一行strIn = Split(strTemp, ",")sinX(i) = Val(strIn(0)) '将值赋给变量sinY(i) = Val(strIn(1)) '将值赋给变量i = i + 1LoopClose #fileNUM '关闭文件intNum = i '取边数Exit SubThisErr:MsgBox Err.Description, vbOKOnly, Err.SourceEnd Sub
Private Sub Command1_Click()Dim i As IntegerDim sinOutput As SingleDim strFileName As StringCommonDialog1.ShowOpen '显示打开对话框strFileName = CommonDialog1.FileName '选取数据文件Call OpenTxtFile(strFileName)sinOutput = 0#For i = 0 To intNum - 1sinOutput = sinOutput + (sinX(i) + sinX(i + 1)) * (sinY(i + 1) - sinY(i)) / 2 '我认为,你的计算公式是不正确的Next iMsgBox "你所求的 " & intNum & " 边形的面积是:" & vbCrLf & sinOutputEnd Sub
'下边是一个四边形的各点坐标,试一下是正常的'2.5,3.4'4.5,2.0'5.6,4.8'4.2,6.1'下面为根据你的公式计算的结果(如果你的公式是正确的话)'你所求的 4 边形的面积是:'2.799999