|
-
Oct 5th, 2002, 06:14 PM
#1
Thread Starter
Member
Converting C++ to VB
I have to convert a C++ progam to VB. the prorgam manily does some calculations and returns three outputs stored in three variables. I converted the same exact code from C++ to VB with one exception that i had to change some variables type from integer to double. below is the code, it's supposed to give me 2, 5,5 in a label but it outputs 2,0, 0 , anyone can figure this out?
Code:
Option Explicit
Private Function CalcN1m(pLast As Double, clast As Double, Rev As Double, Vproduct As Double) As Double
Dim temp1 As Double, temp2 As Double, temp3 As Double, temp4 As Double, final2 As Double
temp1 = Log(1 - pLast)
temp2 = (-clast) / (Rev - Vproduct)
temp3 = Log(temp2 / temp1)
temp4 = temp3 / temp1
temp4 = CInt(temp4) ' temp4 = n1
If V1(temp4, pLast, clast, Rev, Vproduct) >= V1(temp4 + 1, pLast, clast, Rev, Vproduct) Then
final2 = temp4
Else
final2 = temp4 + 1
End If
CalcN1m = final2
End Function
Private Function V1(n1 As Double, probLast As Double, CostLast As Double, R As Double, V As Double) As Double
'calculate the expected profit(v1) from the completion stage &
V1 = 0
V1 = (1 - Pow(1 - probLast, n1)) * (R - V) - n1 * CostLast
End Function
Private Function CalcProfit(n2 As Double, p2 As Double, c2 As Double, nm1 As Double) As Double
' calculate the expected profit from previous stages
Dim yS As Double, YB As Double, stemp As Double
Dim firstT As Boolean, stemp2 As Double, zS As Double, ZB As Double
yS = 0: YB = 0
If n2 >= nm1 Then
For stemp = nm1 To stemp <= n2
yS = Prob(n2, p2, stemp) * V1(nm1, p1, c1, R, V)
YB = YB + yS
Next stemp
For stemp = 0 To stemp <= nm1 - 1
yS = Prob(n2, p2, stemp) * V1(stemp, p1, c1, R, V)
YB = YB + yS
Next stemp
Else
For stemp = 0 To stemp <= n2
yS = Prob(n2, p2, stemp) * V1(stemp, p1, c1, R, V)
YB = YB + yS
Next stemp
End If
YB = YB - c2 * n2
CalcProfit = YB
End Function
Private Function CalcProfit2(n3 As Double, p3 As Double, c3 As Double, nm2 As Double) As Double
Dim tempProb As Double, Tempcost As Double, tempArrayofNM As Double, yS As Double, YB As Double, stemp As Double
Dim firstT As Boolean, stemp2 As Double, zS As Double, ZB As Double, currentCounter As Double: firstT = True
Dim temprob2 As Double, tempcost2 As Double
zS = 0: ZB = 0: 'firstT = True
If n3 >= nm2 Then
For stemp2 = nm2 To stemp2 <= n3
zS = Prob(n3, p3, stemp2) * CalcProfit(nm2, p2, c2, n1m)
ZB = ZB + zS
Next stemp2
For stemp2 = 0 To stemp2 <= nm2 - 1
zS = Prob(n3, p3, stemp2) * CalcProfit(stemp2, p2, c2, n1m)
ZB = ZB + zS
Next stemp2
Else
For stemp2 = 0 To stemp2 <= n3
zS = Prob(n3, p3, stemp2) * CalcProfit(stemp2, p2, c2, n1m)
ZB = ZB + zS
Next stemp2
End If
ZB = ZB - c3 * n3
CalcProfit2 = ZB
End Function
Private Function Pow(Number As Double, Power As Double) As Double
Dim x As Integer
If Power > 1 Then
For x = 2 To Power
Number = Number * 2
Next
Pow = Number
Else
If Power = 0 Then Pow = 1
If Power = 1 Then Pow = Number
End If
End Function
Private Function Prob(Ntemp As Double, p As Double, stemp As Double) As Double
Dim t As Double ' calculate binomial probability
t = factorial(Ntemp) / (factorial(stemp) * factorial(Ntemp - stemp))
Prob = t * Pow(p, stemp) * Pow((1 - p), (Ntemp - stemp))
End Function
Private Function factorial(Number As Double) As Double
If Number < 1 Then
factorial = 1
Else
factorial = Number * factorial(Number - 1)
End If
End Function
Public Sub Form_Load()
Dim counter As Double, x As Double
Dim z As Double, firstT As Boolean, z2 As Double, temp As Integer 'Stemp = S1
p1 = 0.63
p2 = 0.48
p3 = 0.75
c1 = 13
c2 = 4
c3 = 2
R = 130
V = 1
n1m = CalcN1m(p1, c1, R, V)
Do ' this can be a for loop too like one below
max = CalcProfit(n2, p2, c2, n1m)
n2m = n2
n2 = n2 + 1
Loop While CalcProfit(n2, p2, c2, n1m) > max
For n3 = 1 To n3 <= 40
If CalcProfit2(n3, p3, c3, n2m) > max2 Then
max2 = CalcProfit2(n3, p3, c3, n2m)
n3m = n3
End If
Next n3
lbl.Caption = n1m & " " & n2m & " " & n3m 'my output
End Sub
Win XP
1.4 GHZ AMD TB
512 DDR 2100 crucial
Geforce4ti 4200
Acoustic Edge sound card
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|