请高手帮一下忙,毕业设计卡壳了,老师让把一段BASIC程序调成VB,马上就要提交,不胜感激!
做毕业设计老师让我把一段BASIC程序调成VB,可是以前一点相关基础也没有,做来做去总是调不成功,请高手帮帮忙,指点一下,谢谢了!下面是BASIC源代码
5: rem pumpfailure waterhammer program
10: dim h(8),hp(8),q(8),qp(8),wh(48)wm(48)
15: read n,d a,el,es,qn,hn,nn,mn,gd,dt,v,g
20: for i=1to48
25: read wh(i)
30: next i
35: for i=1to48
40: read wm(i)
45: next i
50: dx=.1:ns=n+1
55: b=4*a/(g*pi*d*d):hp(8)=el:c1=es-el
60: c3=.05236*gd*nn/(g*mn*dt)
65: x=pi+atn(v)
70: i=int(x/dx+6)
75: k1=0
80: a1=wh(i+1)-wh(i)/dx
85: a0=wh(i+1)-a1*(i-5)
90: for k2=1to4
95: f1=c1+hn*(1+v*v)*(a0+a1*x)
100: df=hn*(2*v*(a0+a1*x)+a1)
105: v=v-f1/df
110: x=pi+atn(v)
115: next k2
120: ii = Int(x / dx + 6)
125: If ii = i goto 160
130: k1=k1+1
135: if k1>4 goto 150
140: i=ii
145: goto 80
150: lprint "trouble with steady state"
155: goto 565
160: b1 = (wm(i + 1) - wm(i)) / dx
165: b0 = wm(i + 1) - b1 * (i - 1) * dx
170: m = (1 + v * v) * (b0 + b1 * x)
175: VB = v: va = v: bb = 1: ba = 1: bt = 1: l = 0
180: t = 0: k3 = 0
185: For i = 1 To ns
190: q(i) = 2 * v * qn: h(i) = el: q0 = q(5)
195: Next i
200: lprint "time beta v h(1) h(5) l"
205: lprint using"###.##"; t; bt; v;
210: lprint using"####.#"; h(1); h(5);
212: lprint using"###.##"; l
215: t=t+dt
220: if t>50 goto 570
230: k3=k3+1
235: qp(8) = q(7) + (h(7) - el) / b
240: for i=6 to 7
245: cp = h(i - 1) + q(i - 1) * b
250: hp(i) = 0.5 * (cp + h(i + 1) - q(i + 1) * b)
255: qp(i) = (cp + hp(i)) / b
260: next i
265: cp = h(4) + q(4) * b: cm = h(6) - q(6) * b
270: if l>0 goto 295
275: l=0
280: hp(5) = 0.5 * (cp + cm): qp(5) = (hp(5) - cm) / b: qi = qp(5)
282: If hp(5) <= 70.5 goto 295
285: goto 315
295: hp(5)=70.5
300: qi = (cp - hp(5)) / b
305: qp(5) = (hp(5) - cm) / b
310: l = l + 0.5 * dt * (qp(5) + q(5) - qi - q0) * 4 / (pi * d * d)
315: for i=2 to 3
320: cp = h(i - 1) + q(i - 1) * b
325: hp(i) = 0.5 * (cp + h(i + 1) - q(i + 1) * b)
330: qp(i) = (cp - hp(i)) / b
335: next i
337: cp = h(3) + q(3) * b
339: hp(4) = 0.5 * (cp + h(5) - q0 * b)
343: qp(4) = (cp - hp(4)) / b
345: cm = h(2) - q(2) * b
350: bt = 2 * ba - bb: v = va
355: k4=0
360: if bt<0goto 375
365: x = pi + Atn(v / bt)
370: goto 380
375: x = Atn(v / bt)
380: i=int(x/dx+6)
385: a1 = (wh(i + 1) - wh(i)) / dx
390: a0 = wh(i + 1) - a1 * (i - 5) * dx
395: b1 = (wm(i + 1) - wm(i)) / dx
400: b0 = wm(i + 1) - b1 * (i - 5) * dx
405: For k5 = 1 To 8
410: f1 = es - cm - 2 * b * qn * v + hn * (bt * bt * v * v) * (a0 + a1 * x)
415: fa = -2 * b * qn + hn * (2 * v * (a0 + a1 * x) + a1 * bt)
420: fb = hn * (2 * bt * (a0 + a1 * x) - v * a1)
425: f2 = (bt * bt + v * v) * (b0 + b1 * x) + m + c3 * (bt - ba)
430: fc = 2 * v * (b0 + b1 * x) + bt * b1
435: fd = 2 * bt * (b0 + b1 * x) - v * b1 + c3
440: db = (f2 / fc - f1 / fa) / (fb / fa - fd / fc)
445: dv = -f1 / fa - db * fb / fa
450: v = v + dv: bt = bt + db
460: If bt < 0 goto 475
465: x = pi + Atn(v / bt)
470: goto 480
475: x = Atn(v / bt)
480: If Abs(db) + Abs(dv) < 0.0002 goto 490
485: next k5
490: ii = Int(x / dx + 6)
495: if ii=i goto 525
500: i=ii
505: k4=k4+1
510: if k4<5 goto 385
515: lprint"trouble with pump"
520: goto 565
525: qp(i) = 2 * v * qn: hp(1) = cm + qp(1) * b
530: vb = va: va = v: bb = ba: ba = bt: q0 = qi
535: m = (bt * bt + v * v) * (b0 + b1 * x)
540: for i= 1 to 8
545: q(i) = qp(i): h(i) = hp(i)
550: next i
555: If Int(k3 / 5) * 5 = k3 goto 205
560: goto 215
565: stop
570: end
600: data 7 2.5 1000 79.2 0 7.33 79.2 375 17200 100000 0.14 1 9.807
604: data 0.14 0.3 0.45 0.6 0.73
605: data 0.81 0.77 0.73 0.695 0.67 0.66 0.655 0.66 0.67 0.695 0.725
610: data 0.77 0.825 0.89 0.975 1.1 1.26 1.4 1.54 1.64 1.68 1.68
615: data 0.66 1.63 1.59 1.54 1.495 1.47 1.41 1.39 1.38 1.38 1.375
620: data 1.36 1.34 1.27 1.16 0.99 0.78 0.54 0.3 0.08 -0.12
624: data -0.86 -0.7 -0.56 -0.48 -0.39
625: data -0.3 -0.23 -0.15 -0.06 0.03 0.13 0.225 0.335 0.44 0.545 0.66
630: data 0.76 0.86 0.96 1.07 1.16 1.25 1.31 1.35 1.365 1.35 1.3
635: data 1.21 1.09 0.9 0.7 0.56 0.47 0.4 0.36 0.34 0.36 0.4
640: data 0.45 0.495 0.53 0.55 0.56 0.565 0.52 0.42 0.3 0.18
这并不是毕业设计的必须内容,所以我并没有弄虚作假 --------------------编程问答-------------------- 建议你还是自己转,遇到问题再来问。
天狼工作室
http://www.j2soft.cn/ --------------------编程问答-------------------- 看到一堆GOTO --------------------编程问答-------------------- 因为不能发帖,借贵宝地用下下哈,谢谢啦
有字符串 str = “12345求助信息 a b c d e”
其中 求助信息 后面的空格数不是固定的,上面例子中是3个
我想从 “求”字开始,截取10个字节的串,
用 MIDB(str,6,10),结果 “求助信息”取出来变成了乱码
求高人指点。。。。
--------------------编程问答-------------------- --------------------编程问答-------------------- 广告来了 --------------------编程问答--------------------
mid(str,6,10) --------------------编程问答-------------------- 路过 --------------------编程问答-------------------- 恩确实是一大堆GOTO语句很让人头大,我尝试着用IF THEN 和DO WHILE LOOP 语句来代替他们,发现嵌套是一环扣一环,而且还有交叉现象,
做到这就往下做不动了
大家看
Option Explicit
Private Const pi = 3.1415926
Private Sub Command1_Click()
Dim h() As Double, hp(0 To 7) As Double, q() As Double, qp() As Double, wh(48) As Double, wm(48) As Double
Dim i As Integer, z As Integer, n As Integer, f#, d#, a#, el#, es#, qn#, hn#, nn%, mn#, gd#, dt#, tm#, dx#, v#, g#, jp#, ns#, b#, c1#, c3#, x#, k1#, a1#, a0#, k2%, f1#, df#, ii%, b1%, b0%, m#, vb#, va#, bb#, ba#, bt#, l%, t%, k3%, q0%, cp#, cm#, qi#, k4%, k5%, fa#, fb#, f2#, fc#, fd#, db#, dv#
Open "C:\Documents and Settings\Administrator\桌面\程序\www.txt" For Input As #1
Input #1, n, d, a, el, es, qn, hn, nn, mn, gd, dt, v, g
Debug.Print n, d, a, el, es, qn, hn, nn, mn, gd, dt, v, g
Close #1
Open "C:\Documents and Settings\Administrator\桌面\程序\whi.txt" For Input As #2
For z = 0 To 47
Input #2, wh(z)
Debug.Print wh(z)
Next z
Close #2
Open "C:\Documents and Settings\Administrator\桌面\程序\wmi.txt" For Input As #3
For z = 0 To 47
Input #3, wm(z)
Debug.Print wm(z)
Next z
Close #3
dx = 0.1: ns = n + 1
b = 4 * a / (g * pi * d * d): hp(7) = el: c1 = es - el
c3 = 0.05236 * gd * nn / (g * mn * dt)
x = pi + Atn(v)
i = Int(x / dx + 6)
k1 = 0
Do While i = ii
a1 = (wh(i + 1) - wh(i)) / dx
a0 = wh(i + 1) - a1 * (i - 5) * dx
For k2 = 1 To 4
f1 = c1 + hn * (1 + v * v) * (a0 + a1 * x)
df = hn * (2 * v * (a0 + a1 * x) + a1)
v = v - f1 / df
x = pi + Atn(v)
Next k2
ii = Int(x / dx + 6)
If ii = i Then
b1 = (wm(i + 1) - wm(i)) / dx
b0 = wm(i + 1) - b1 * (i - 1) * dx
m = (1 + v * v) * (b0 + b1 * x)
vb = v: va = v: bb = 1: ba = 1: bt = 1: l = 0
t = 0: k3 = 0
For i = 1 To ns
q(i) = 2 * v * qn: h(i) = el: q0 = q(5)
Next i
Print "time beta v h(1) h(5) l"
Print Spc(8); "###.##"; t; bt; v;
Print Spc(8); "####.#"; h(1); h(5);
Print Spc(8); "###.##"; l
t = t + dt
If t > 50 Then
End
End If
Do While t <= 50
k3 = k3 + 1
qp(8) = q(7) + (h(7) - el) / b
For i = 6 To 7
cp = h(i - 1) + q(i - 1) * b
hp(i) = 0.5 * (cp + h(i + 1) - q(i + 1) * b)
qp(i) = (cp + hp(i)) / b
Next i
cp = h(4) + q(4) * b: cm = h(6) - q(6) * b
If l > 0 Then
hp(5) = 70.5
qi = (cp - hp(5)) / b
qp(5) = (hp(5) - cm) / b
l = l + 0.5 * dt * (qp(5) + q(5) - qi - q0) * 4 / (pi * d * d)
For i = 2 To 3
cp = h(i - 1) + q(i - 1) * b
hp(i) = 0.5 * (cp + h(i + 1) - q(i + 1) * b)
qp(i) = (cp - hp(i)) / b
Next i
cp = h(3) + q(3) * b
hp(4) = 0.5 * (cp + h(5) - q0 * b)
qp(4) = (cp - hp(4)) / b
cm = h(2) - q(2) * b
bt = 2 * ba - bb: v = va
k4 = 0
If bt < 0 Then
x = Atn(v / bt)
i = Int(x / dx + 6)
Do While k4 < 5
a1 = (wh(i + 1) - wh(i)) / dx
a0 = wh(i + 1) - a1 * (i - 5) * dx
b1 = (wm(i + 1) - wm(i)) / dx
b0 = wm(i + 1) - b1 * (i - 5) * dx
For k5 = 1 To 8
f1 = es - cm - 2 * b * qn * v + hn * (bt * bt * v * v) * (a0 + a1 * x)
fa = -2 * b * qn + hn * (2 * v * (a0 + a1 * x) + a1 * bt)
fb = hn * (2 * bt * (a0 + a1 * x) - v * a1)
f2 = (bt * bt + v * v) * (b0 + b1 * x) + m + c3 * (bt - ba)
fc = 2 * v * (b0 + b1 * x) + bt * b1
fd = 2 * bt * (b0 + b1 * x) - v * b1 + c3
db = (f2 / fc - f1 / fa) / (fb / fa - fd / fc)
dv = -f1 / fa - db * fb / fa
v = v + dv: bt = bt + db
If bt < 0 Then
x = Atn(v / bt)
If Abs(db) + Abs(dv) < 0.0002 Then
ii = Int(x / dx + 6)
If ii = i Then
qp(i) = 2 * v * qn: hp(1) = cm + qp(1) * b
vb = va: va = v: bb = ba: ba = bt: q0 = qi
m = (bt * bt + v * v) * (b0 + b1 * x)
For i = 1 To 8
q(i) = qp(i): h(i) = hp(i)
Next i
ElseIf i = ii Then
k4 = k4 + 1
Exit Do
Loop
Print Spc(8); "trouble with pump"
Stop
End If
Else
Next k5
ii = Int(x / dx + 6)
--------------------编程问答-------------------- 好像vb也可以goto的说,大家一起goto,不是很好吗 --------------------编程问答-------------------- 看着楼主的一堆乱糟糟的代码就头痛........... -_-!
我的VB代码中出现 goto 的地方只有:
On Error Goto .....
--------------------编程问答-------------------- GoTo --------------------编程问答-------------------- 平生最恨的就是goto
我的程序中只有On Error Goto ..... --------------------编程问答-------------------- 管他的呢!不用goto是为了程序的可读性,程序的可读性是为了工作的快速性,所以一切的一切就是为了速度,如果用goto反而速度更快,不用就是舍本逐末了!不为新工程还是少用goto的好。 --------------------编程问答-------------------- 头大了,还是让耐心好的慢慢看吧 --------------------编程问答-------------------- BASIC代码中的 data 有问题?
貌似 执行 80~145 之间的代码时,下标溢出..............
Option Explicit
Option Base 1
Sub Main()
Rem pumpfailure waterhammer program
Const Pi# = 3.14159265358979
Dim h#(8), hp#(8), q#(8), qp#(8), wh#(48), wm#(48)
Dim n#, d#, a#, el#, es#, qn#, hn#, nn#, mn#, gd#, dt#, v#, g#
Dim i&, lPt&, la&, arrTemp$()
Dim strData$(10)
strData(1) = "data 0.14 0.3 0.45 0.6 0.73"
strData(2) = "data 0.81 0.77 0.73 0.695 0.67 0.66 0.655 0.66 0.67 0.695 0.725"
strData(3) = "data 0.77 0.825 0.89 0.975 1.1 1.26 1.4 1.54 1.64 1.68 1.68"
strData(4) = "data 0.66 1.63 1.59 1.54 1.495 1.47 1.41 1.39 1.38 1.38 1.375"
strData(5) = "data 1.36 1.34 1.27 1.16 0.99 0.78 0.54 0.3 0.08 -0.12"
strData(6) = "data -0.86 -0.7 -0.56 -0.48 -0.39"
strData(7) = "data -0.3 -0.23 -0.15 -0.06 0.03 0.13 0.225 0.335 0.44 0.545 0.66"
strData(8) = "data 0.76 0.86 0.96 1.07 1.16 1.25 1.31 1.35 1.365 1.35 1.3"
strData(9) = "data 1.21 1.09 0.9 0.7 0.56 0.47 0.4 0.36 0.34 0.36 0.4"
strData(10) = "data 0.45 0.495 0.53 0.55 0.56 0.565 0.52 0.42 0.3 0.18"
n = 7: d = 2.5: a = 1000
el = 79.2: es = 0: qn = 7.33
hn = 79.2: nn = 375: mn = 17200
gd = 100000: dt = 0.14: v = 1: g = 9.807
lPt = 0
For i = 1 To 10
arrTemp = Split(strData(i))
For la = LBound(arrTemp) + 1 To UBound(arrTemp)
lPt = lPt + 1
If (lPt > 48) Then
wm(lPt - 48) = Val(arrTemp(la))
Else
wh(lPt) = Val(arrTemp(la))
End If
Next
Next
Dim dx#, ns&, b#, c3#, x#, c1#
dx = 0.1: ns = n + 1
b = 4 * a / (g * Pi * d * d): hp(8) = el: c1 = es - el
c3 = 0.05236 * gd * nn / (g * mn * dt)
x = Pi + Atn(v)
i = Int(x / dx + 6)
Dim k1&, k2&, a1#, a0#, f1#, df#, ii&
k1 = 0
Do
a1 = wh(i + 1) - wh(i) / dx
a0 = wh(i + 1) - a1 * (i - 5)
For k2 = 1 To 4 'for k2=1 to 4
f1 = c1 + hn * (1 + v * v) * (a0 + a1 * x)
df = hn * (2 * v * (a0 + a1 * x) + a1)
v = v - f1 / df
x = Pi + Atn(v)
Next 'Next k2
ii = Int(x / dx + 6)
If ii = i Then GoTo 160
k1 = k1 + 1
If k1 > 4 Then Exit Do
i = ii
Loop
'150: lprint "trouble with steady state"
Debug.Print "trouble with steady state"
GoTo 565
160:
Dim b1#, m#, b0#
b1 = (wm(i + 1) - wm(i)) / dx
'165: b0 = wm(i + 1) - b1 * (i - 1) * dx
b0 = wm(i + 1) - b1 * (i - 1) * dx
'170: m = (1 + v * v) * (b0 + b1 * x)
m = (1 + v * v) * (b0 + b1 * x)
'175: vb = v: va = v: bb = 1: ba = 1: bt = 1: l = 0
Dim vb#, va#, bb&, ba&, bt&, l&
vb = v: va = v: bb = 1: ba = 1: bt = 1: l = 0
'180: T = 0: k3 = 0
'185: For i = 1 To ns
'190: q(i) = 2 * v * qn: h(i) = el: q0 = q(5)
'195: Next i
Dim T#, k3&, q0#
T = 0: k3 = 0
For i = 1 To ns
q(i) = 2 * v * qn: h(i) = el: q0 = q(5)
Next
'200: lprint "time beta v h(1) h(5) l"
Debug.Print "time beta v h(1) h(5) l"
'205: lprint using"###.##"; t; bt; v;
'210: lprint using"####.#"; h(1); h(5);
'212: lprint using"###.##"; l
205:
Dim strTemp$, strFormat$
strFormat = "000.##"
strTemp = Format$(T, strFormat)
For i = 1 To InStr(1, strTemp, ".")
If (Asc(Mid$(strTemp, i, 1)) <> 48) Then Exit For
Mid$(strTemp, i, 1) = " "
Next
Debug.Print strTemp;
strTemp = Format$(bt, strFormat)
For i = 1 To InStr(1, strTemp, ".")
If (Asc(Mid$(strTemp, i, 1)) <> 48) Then Exit For
Mid$(strTemp, i, 1) = " "
Next
Debug.Print strTemp;
strTemp = Format$(v, strFormat)
For i = 1 To InStr(1, strTemp, ".")
If (Asc(Mid$(strTemp, i, 1)) <> 48) Then Exit For
Mid$(strTemp, i, 1) = " "
Next
Debug.Print strTemp;
'lprint using"####.#"; h(1); h(5);
strFormat = "0000.#"
strTemp = Format$(h(1), strFormat)
For i = 1 To InStr(1, strTemp, ".")
If (Asc(Mid$(strTemp, i, 1)) <> 48) Then Exit For
Mid$(strTemp, i, 1) = " "
Next
Debug.Print strTemp;
strTemp = Format$(h(5), strFormat)
For i = 1 To InStr(1, strTemp, ".")
If (Asc(Mid$(strTemp, i, 1)) <> 48) Then Exit For
Mid$(strTemp, i, 1) = " "
Next
Debug.Print strTemp;
'lprint using"###.##"; l
strTemp = Format$(l, "000.##")
For i = 1 To InStr(1, strTemp, ".")
If (Asc(Mid$(strTemp, i, 1)) <> 48) Then Exit For
Mid$(strTemp, i, 1) = " "
Next
Debug.Print strTemp;
'215: T = T + dt
Do '560
T = T + dt
'220: If T > 50 Then GoTo 570
If T > 50 Then GoTo 570
'230: k3 = k3 + 1
k3 = k3 + 1
qp(8) = q(7) + (h(7) - el) / b
Dim cp#, cm#, qi#
For i = 6 To 7
cp = h(i - 1) + q(i - 1) * b
hp(i) = 0.5 * (cp + h(i + 1) - q(i + 1) * b)
qp(i) = (cp + hp(i)) / b
Next i
cp = h(4) + q(4) * b: cm = h(6) - q(6) * b
If l > 0 Then
hp(5) = 0
Else
l = 0
hp(5) = 0.5 * (cp + cm): qp(5) = (hp(5) - cm) / b: qi = qp(5)
End If
If hp(5) <= 70.5 Then
hp(5) = 70.5
qi = (cp - hp(5)) / b
qp(5) = (hp(5) - cm) / b
l = l + 0.5 * dt * (qp(5) + q(5) - qi - q0) * 4 / (Pi * d * d)
End If
For i = 2 To 3
cp = h(i - 1) + q(i - 1) * b
hp(i) = 0.5 * (cp + h(i + 1) - q(i + 1) * b)
qp(i) = (cp - hp(i)) / b
Next
'337: cp = h(3) + q(3) * b
cp = h(3) + q(3) * b
'339: hp(4) = 0.5 * (cp + h(5) - q0 * b)
hp(4) = 0.5 * (cp + h(5) - q0 * b)
'343: qp(4) = (cp - hp(4)) / b
qp(4) = (cp - hp(4)) / b
'345: cm = h(2) - q(2) * b
cm = h(2) - q(2) * b
'350: bt = 2 * ba - bb: v = va
bt = 2 * ba - bb: v = va
'355: k4 = 0
Dim k4&, k5
k4 = 0
'360: If bt < 0 Then GoTo 375
'365: x = Pi + Atn(v / bt)
'370: GoTo 380
'375: x = Atn(v / bt)
If bt < 0 Then
x = Atn(v / bt)
Else
x = Pi + Atn(v / bt)
End If
'380: i = Int(x / dx + 6)
i = Int(x / dx + 6)
385:
a1 = (wh(i + 1) - wh(i)) / dx
a0 = wh(i + 1) - a1 * (i - 5) * dx
b1 = (wm(i + 1) - wm(i)) / dx
b0 = wm(i + 1) - b1 * (i - 5) * dx
Dim fa#, fb#, fc#, fd#, f2#, Db#, Dv#
For k5 = 1 To 8
f1 = es - cm - 2 * b * qn * v + hn * (bt * bt * v * v) * (a0 + a1 * x)
fa = -2 * b * qn + hn * (2 * v * (a0 + a1 * x) + a1 * bt)
fb = hn * (2 * bt * (a0 + a1 * x) - v * a1)
f2 = (bt * bt + v * v) * (b0 + b1 * x) + m + c3 * (bt - ba)
fc = 2 * v * (b0 + b1 * x) + bt * b1
fd = 2 * bt * (b0 + b1 * x) - v * b1 + c3
Db = (f2 / fc - f1 / fa) / (fb / fa - fd / fc)
Dv = -f1 / fa - Db * fb / fa
v = v + Dv: bt = bt + Db
If (bt < 0) Then
x = Atn(v / bt)
Else
x = Pi + Atn(v / bt)
End If
If Abs(Db) + Abs(Dv) < 0.0002 Then Exit For
Next
ii = Int(x / dx + 6)
If (ii = i) Then
qp(i) = 2 * v * qn: hp(1) = cm + qp(1) * b
vb = va: va = v: bb = ba: ba = bt: q0 = qi
m = (bt * bt + v * v) * (b0 + b1 * x)
For i = 1 To 8
q(i) = qp(i): h(i) = hp(i)
Next i
If Int(k3 / 5) * 5 = k3 Then GoTo 205
Else
i = ii
k4 = k4 + 1
If k4 < 5 Then GoTo 385
Debug.Print "trouble with pump"
Exit Do ' 215
End If
Loop 'GoTo 215
'565: Stop
'570: End
565:
Stop
570:
End
End Sub
--------------------编程问答-------------------- GoTo 太多、太乱..........
已经作了点处理,去掉了一些 GoTo 。
但原代码中 GoTo 造成的交叉太多,还没完全理顺。
补充:VB , 基础类