Private Sub Command1_Click()
Dim x() As Double, y() As Double, z() As Double, i As Integer, n As Integer, d() As Double, X1 As Double, Y1 As Double, X2 As Double, Y2 As Double
n = InputBox("请输入需求点的个数")
Do Until n > 2
n = InputBox("请输入大于2的需求点的数")
Loop
ReDim x(n)
ReDim y(n)
ReDim z(n)
ReDim d(n)
For i = 1 To n
x(i) = InputBox("请输入第" & i & "个点的X坐标")
y(i) = InputBox("请输入第" & i & "个点的Y坐标")
z(i) = InputBox("请输入第" & i & "个点的需求量")
d(i) = 1
Next
Do Until Abs(X2 - X1) <= 0.001 And Abs(Y2 - Y1) <= 0.001
s = 0: t = 0: e = 0
For j = 1 To Int(n / 2)
s = s + x(j) * z(j)
t = t + y(j) * z(j)
e = e + z(j) / d(j)
Next
X1 = X2: X2 = s / e
Y1 = Y2: Y2 = t / e
For i = 1 To Int(n / 2)
d(i) = Round(Sqr((X2 - x(i)) ^ 2 + (Y2 - y(i)) ^ 2), 1)(提示数据溢出)
Next
Loop
Label1.Caption = Label1.Caption & "(" & X2 & "," & Y2 & ")"
End Sub
你自己再看看吧 可以运行了 但是 结果有问题 可能是你的公式有问题 你在改改!!!!
Dim x1%, y1%, y2%, x2%, j%, e%, t%, s%
Private Sub Command1_Click()
Dim x(100) As Integer, y(100) As Integer, z(100) As Integer, i As Integer, n As Integer, d(100) As Double
n = InputBox("请输入需求点的个数")
If n < 2 Then
InputBox ("请输入大于2的数")
End If
For i = 1 To n
x(i) = InputBox("请输入第" & i & "个点的X坐标")
y(i) = InputBox("请输入第" & i & "个点的Y坐标")
z(i) = InputBox("请输入第" & i & "个点的需求量")
d(i) = 1
Next
Do
s = 0: t = 0: e = 0
For j = 1 To Int(n / 2)
If d(j) <> 0 Then
s = s + x(j) * z(j)
t = t + y(j) * z(j)
e = e + z(j) / d(j)
Else
s = s + x(j) * z(j)
t = t + y(j) * z(j)
e = e + z(j)
End If
Next
If e = 0 Then
x1 = x2: x2 = 0
y1 = y2: y2 = 0
Else
x1 = x2: x2 = s / e '
y1 = y2: y2 = t / e
End If
For i = 1 To Int(n / 2)
d(i) = Round(Sqr((x2 - x(i)) ^ 2 + (y2 - y(i)) ^ 2), 5) '(提示数据溢出)
Next
Loop Until Abs(x2 - x1) <= 0.001 And Abs(y2 - y1) <= 0.001
Label1.Caption = Label1.Caption & "(" & x2 & "," & y2 & ")"
End Sub
'除数为零是不允许的。不知道是否可以,改了不少地方
把溢出的那句话的每个变量(包括常数)都用CDbl()转换掉