Results 1 to 5 of 5

Thread: Converting C++ to VB

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    35

    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

  2. #2

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    35
    This is the C++ code, you might be able to know what went wrong when i changes it to VB.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <math.h>
    
    
    /* function prototypes */
    
    void initialize(void);
    double v1(double n1, double p1, double c1, double R, double V);
    double v2(double n2, double p2, double c2, double n1m);
    double v3(double n3, double p3, double c3, double n2m);
    double prob(int n, double p, int s);
    double factorial(int n);
    
    /* global variables */
    
    double	R, V, c1, c2, c3, p1, p2, p3, n1, n2, n3, n1m, n2m, n3m, max2, max3;
    FILE *fp;
    
    
    /*
    R:	the expected revenue of the new product
    V:	the production cost of the new product in order to generate R
    p1:	the probability of a project passing the completion stage of R&D
    p2:	the probability of a project passing the prototype testing stage of R&D
    p3:	the probability of a project passing the idea investigation stage of R&D
    c1:	the per project cost associated with the completion stage of R&D
    c2:	the per project cost associated with the prototype testing stage of R&D
    c3:	the per project cost associated with the idea investigation stage of R&D
    */
    
    
    main ()
    {
    
    double temp1, temp2, temp3, temp4;
    
    fp = fopen("result.txt", "a+");
    
    initialize();
    
    /* v0(s0=0)=0 and v0(s0>0) = R-V */
    
    /* calculate n1m based on the formula
    FOC:	n1m = ln[(-c)/[(R-V)ln(1-p)]] / ln(1-p)
    and then select one adjacent integer number which produces the larger expected profit as the final n1m
    */
    
    temp1 = log(1-p1);
    temp2 = (-c1)/(R-V);
    temp3 = log (temp2/temp1);
    temp4 = temp3 / temp1;
    
    temp4 = (int)temp4;
    if (v1(temp4,p1,c1,R,V) >= v1(temp4+1,p1,c1,R,V))
    	n1m = temp4;
    else
           	n1m = temp4+1;
    
    
    
    /*
    above shows the optimal solution for stage one and a comparison of neighboring alternatives
    */
    
    
    
    for (n2 = 1; n2 <= 30; n2++)
    {	printf("%6G", n2);
    	printf("%10G", v2(n2,p2,c2,n1m));
    	fprintf(fp, "%15G", n2);
    	fprintf(fp, "%20G\n", v2(n2,p2,c2,n1m));
    	if (v2(n2,p2,c2,n1m) > max2)
    	{	max2 = v2(n2,p2,c2,n1m);
    		n2m = n2;
    	}
    }
    
    
    /*
    above shows a numerical testing of the optimal value for stage two and a comparison.
    */
    
    
    for (n3 = 1; n3 <= 40; n3++)
    {	printf("%6G", n3);
    	printf("%10G", v3(n3,p3,c3,n2m));
    	fprintf(fp, "%15G", n3);
    	fprintf(fp, "%20G\n", v3(n3,p3,c3,n2m));
    	if (v3(n3,p3,c3,n2m) > max3)
    	{	max3 = v3(n3,p3,c3,n2m);
    		n3m = n3;
    	}
    }
    
    
    /*
    above shows a numerical testing of the optimal value for stage three and a comparison
    */
    printf("In summary, given the specific inputs, we recommend following numbers of projects\n");
    printf("should be initiated in the Idea Investigation, Prototype Testing, and Completeion\n");
    printf("stages, respectively:");
    printf("%9.1G", n1m);
    printf("%10G", n2m);
    printf("%10G\n", n3m);
    
    
    fprintf(fp, "In summary, given the specific inputs:\n\n");
    if (n3m == 0)
        {	fprintf(fp, "We recommend that you do not develop this product, because the R&D investment\n");
    	fprintf(fp, "will unlikely be recovered from the expected sale of this product.\n\n");
        }
    else
        {	fprintf(fp, "We recommend that you should initiate");
    	fprintf(fp, "%5G", n3m);
    	fprintf(fp,"    projects for the Concept Investigation Stage.\n");
    	fprintf(fp, "The maximum of expected net profit, based on this model, is");
    	fprintf(fp, "%10G\n\n",v3(n3m,p3,c3,n2m));
    
    	if (n2m<n3m)
                {	fprintf(fp,"If there are at least ");
    		fprintf(fp,"%5G", n2m);
    		fprintf(fp,"    projects that have successfully passed the Concept Investigation,\n");
    		fprintf(fp, "we recommend that you should select");
    		fprintf(fp,"%5G", n2m);
    		fprintf(fp,"    projects from them for Prototype Testing. If the number\n");
    		fprintf(fp, "of projects successfully passed Concept Investigation is less than");
    		fprintf(fp,"%5G", n2m);
    		fprintf(fp," ,\n");
    		fprintf(fp,"we recommend that any projects succeeded during Concept Investigation\n");
    	     	fprintf(fp,"should be funded for Prototype Testing.\n\n"); 
    	     }
    	else
    	     {  fprintf(fp,"We recommend that any projects succeeded during Concept Investigation\n");
    		fprintf(fp,"should be funded for Prototype Testing.\n\n");
    	     }
    	if (n1m<n2m)
    	     {	fprintf(fp,"If there are at least ");
    		fprintf(fp,"%5G", n1m);
    		fprintf(fp,"    projects that have successfully passed the Prototype Testing,\n");
    		fprintf(fp, "we recommend that you should select");
    		fprintf(fp,"%5G", n1m);
    		fprintf(fp,"    projects from them for Completion. If the number\n");
    		fprintf(fp, "of projects successfully passed Prototype Testing is less than ");
    		fprintf(fp,"%5G", n1m);
    		fprintf(fp," ,\n");
    		fprintf(fp,"we recommend that any projects succeeded during the Prototype Testing\n");
    		fprintf(fp,"should be funded for Completion.\n\n");
    	     }
    	else
    	     {  fprintf(fp,"We recommend that any projects succeeded during Prototype Testing\n");
    		fprintf(fp,"should be funded for Completion.\n\n");
    	     }
         }
    
    
    fclose(fp);
    return 0;
    }
    
    
    /* following are various functions used by main */
    
    
    void initialize(void)
    /*obtain values for the above variables */
    {
    char	temp[20];
    
    fprintf(fp, "SPECIFIC PARAMETER VALUES PROVIDED BY YOU:\n\n");
    
    printf("what is the expected cumulative revenue of the new product?");
    scanf("%20s", temp);
    R = atof(temp);
    fprintf(fp, "The expected cumulative revenue of the new product is %G\n", R);
    
    printf("what is the expected cumulative production/promotion cost?");
    scanf("%20s", temp);
    V = atof(temp);
    fprintf(fp, "The expected cumulative production/promotion cost is %G\n", V);
    
    printf("what is the average probability of a project passing the Concept Investigation stage?");
    scanf("%20s", temp);
    p3 = atof(temp);
    fprintf(fp, "The average probability of a project passing the Concept Investigation stage is %G\n", p3);
    
    printf("what is the average cost per project during the Concept Investigation stage?");
    scanf("%20s", temp);
    c3 = atof(temp);
    fprintf(fp, "The average cost per project during the Concept Investigation stage is %G\n", c3);
    
    printf("what is the average probability of a project passing the Prototype Testing stage?");
    scanf("%20s", temp);
    p2 = atof(temp);
    fprintf(fp, "The average probability of a project passing the Prototype Testing stage %G\n", p2);
    
    printf("what is the average cost per project during the Prototype Testing stage?");
    scanf("%20s", temp);
    c2 = atof(temp);
    fprintf(fp, "The average cost per project during the Prototype Testing stage is %G\n", c2);
    
    printf("what is the average probability of a project passing the Completion stage?");
    scanf("%20s", temp);
    p1 = atof(temp);
    fprintf(fp, "The average probability of a project passing the Completion stage is %G\n", p1);
    
    printf("what is the average cost per project during the Completion stage?");
    scanf("%20s", temp);
    c1 = atof(temp);
    fprintf(fp, "The average cost per project during the Completion stage is %G\n\n", c1);
    
    fprintf(fp, "RESULTS BASED ON THE PROVIDED PARAMETER VALUES:\n\n");
    }
    /* end of input */
    
    
    
    double v1(double n1, double p1, double c1, double R, double V)
    /* calculate the expected profit(v1) from the completion stage */
    
    {
    	double v1 = 0;
    	v1 = (1-pow(1-p1,n1))*(R-V) - n1*c1;
    	return v1;
    }
    
    
    double v2(double n2, double p2, double c2, double n1m)
    /* calculate the expected profit(v2) from the prototype testing stage */
    {
    	int s1;
    	double y = 0;
            double Y = 0;
    	if (n2 >= n1m)
    	{	for (s1=n1m; s1<=n2; s1++)
    		{	y = prob(n2,p2,s1) * v1(n1m,p1,c1,R,V);
    			Y = Y + y;
    		}
    		for (s1=0; s1 <= n1m-1; s1++)
                    {	y = prob(n2,p2,s1) * v1(s1,p1,c1,R,V);
    			Y = Y + y;
    		}
            }
    	else
    	{	for (s1=0; s1<=n2; s1++)
                    {	y = prob(n2,p2,s1) * v1(s1,p1,c1,R,V);
    			Y = Y + y;
    		}
    	}
            Y = Y - c2*n2;
    	return Y;
    }
    
    
    double v3(double n3, double p3, double c3, double n2m)
    /* calculate the expected profit(v3) from the idea investigation stage */
    {
    	int s2;
    	double z = 0;
            double Z = 0;
    	if (n3 >= n2m)
            {       for (s2=n2m; s2<=n3; s2++)
    		{	z = prob(n3,p3,s2) * v2(n2m,p2,c2,n1m);
    			Z = Z + z;
    		}
    		for (s2=0; s2<=n2m-1; s2++)
    		{	z = prob(n3,p3,s2) * v2(s2,p2,c2,n1m);
    			Z = Z + z;
                    }
            }
    	else
    	{
    		for (s2=0; s2<=n3; s2++)
    		{	z = prob(n3,p3,s2) * v2(s2,p2,c2,n1m);
    			Z = Z + z;
                    }
    	}
    	Z = Z - c3*n3;
    	return Z;
    }
    
    
    double prob (int n, double p, int s)
    /* calculate binomial probability */
    {
    	double t=0;
    	t=factorial(n) / (factorial(s) * factorial(n-s));
        	return t * pow(p, s) * pow(1-p, n-s);
    }
    
    
    double factorial (int n)
    {
        	if (n>1)
    		return n * factorial(n - 1);
        	return 1;
    }
    Win XP
    1.4 GHZ AMD TB
    512 DDR 2100 crucial
    Geforce4ti 4200
    Acoustic Edge sound card

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    why on earth do you want to translate c++ to vb???
    (ignoring the fact that this is plain c)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    35
    i need to convert this program to a GUI, and make a recursive function to accomplish the goal.
    Win XP
    1.4 GHZ AMD TB
    512 DDR 2100 crucial
    Geforce4ti 4200
    Acoustic Edge sound card

  5. #5
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    why not make the gui in visual c, or if you dont have that, make a dll out of it and use vb with the dll?
    retired member. Thanks for everything

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width