How to convert from PowerBasic

New to FreeBASIC? Post your questions here.
Post Reply
chrisc
Posts: 10
Joined: Dec 04, 2016 18:59

How to convert from PowerBasic

Post by chrisc »

I need to convert from PowerBasic, is there a guide to help me to do this ?

Please advise. Thanks
Boris the Old
Posts: 139
Joined: Feb 04, 2011 20:34
Location: Ontario, Canada

Re: How to convert from PowerBasic

Post by Boris the Old »

chrisc wrote:I need to convert from PowerBasic, is there a guide to help me to do this ?
Not that I've ever seen. One pretty much has to dive in and start converting, but luckily, the two languages are very similar. Often, it's just minor syntax or spelling differences. For instance, PB uses the $ sign on a key word to indicate a string (eg Mid$), but FB doesn't. The main area that you'll have problems is with converting GUI code. I began using PB in 2004, and immediately gave up on using the Dynamic Dialog Tools (DDT) feature. Instead I used a third-party package, EZGUI.

When I made the decision to move to Linux, I found that FB required the least amount of conversion effort. But once again the GUI became the main problem. However, after much searching and testing, I decided to use FLTK, and it's worked out very well.

Of course, any language conversion will depend a lot on the nature of the application. I use Currency data types in my code, so I had to write some support routines to handle the arithmetic. Also, to simulate some of PB's verbs and to provide some clarity to my code, I've made liberal use of macros. I think I have close to 400, although not all of them relate to PB.

Not a lot of useful information I'm afraid, but it might give you some ideas.

Rod
FreeFox
Posts: 69
Joined: Sep 28, 2016 23:45

Re: How to convert from PowerBasic

Post by FreeFox »

Although I don't think many people use them, the addition of the $ sign is in fact optional after most of the string functions which PB and FB share (Left, Right, Mid etc.).

This might help you a bit in the translation of a large PB code base to FB's default dialect or FB Lite. Although I don't use it myself, I see from the docs that the $ sign is still compulsory in the QB dialect.
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Re: How to convert from PowerBasic

Post by ganache »

I am trying to calculate large factorials in FB.
This is a translation of such a program in C, using array.
here is the code:

Dim As Integer a(200), index, i, j, n, tmp, counter=0
input "Enter factorial: ";n
a(0)=1
index=0
for j=n to j>=2
tmp=0
for i=0 to i<=index
tmp=(a(i)*j)+tmp
a(i)=tmp mod 10
tmp=tmp /10
while tmp > 0
a(index)=tmp mod 10
tmp=tmp / 10
print " The factorial of ";n; "is:"
wend
next i
next j
for i=index to i>=0
print a(i)
next i
Sleep
End
But something is not quit right, because there is no output.
Any hints,please?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to convert from PowerBasic

Post by fxm »

Could you post the original C code?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to convert from PowerBasic

Post by dodicat »

Ganache:
I am trying to calculate large factorials in FB.
Here is one method in FB

Code: Select all


Dim Shared As Ubyte _Mod_(0 To 99),_Div_(0 To 99)
For z As Long=0 To 99:_Mod_(z)=(z Mod 10+48):_Div_(z)=z\10:Next
    
    Function factorial(num As Long) As String
        Dim As String fact="1",a,b,c
        Dim As Ubyte n,carry,ai
        Dim As Long la,lb
        For z As Long=1 To num
            a=fact:b=Str(z):la=Len(a):lb=Len(b):c=String(la+lb,"0")
            For i As Long =la-1 To 0 Step -1
                carry=0:ai=a[i]-48
                For j As Long =lb-1 To 0 Step -1
                    n =ai*(b[j]-48)+(c[i+j+1]-48)+carry
                    carry =_Div_(n):c[i+j+1]=_Mod_(n)
                Next j
                c[i]+=carry
            Next i
            fact=Ltrim(c,"0")
        Next z
        Return fact
    End Function
    
    Print factorial(5)
    Print
    Print factorial(1000)
    
    Sleep 
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: How to convert from PowerBasic

Post by frisian »

Working

Code: Select all

Dim As Integer a(2000), index, i, j, n, tmp, carry

Input "Enter factorial: ";n
a(0)=1

For j=n To 2 Step -1
  carry = 0
  
  For i = 0 To index
    tmp = (a(i) * j) + carry
    a(i) = tmp Mod 10
    carry = tmp \ 10
  Next i
  
  While carry > 0
    index = index + 1
    a(index) = carry Mod 10
    carry = carry \ 10
  Wend
  
Next j

Print " The factorial of ";n; " is: ";
For i = index To 0 Step -1
  Print Str(a(i));
Next i

Sleep
End
For a better one, have look at how it's done in Primorial numbers at Rosetta Code
http://rosettacode.org/wiki/Primorial_numbers#FreeBASIC
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Re: How to convert from PowerBasic

Post by ganache »

fxm said : "Could you post the original C code?"

This is the c code. I translated it literally as best as I could understood.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to convert from PowerBasic

Post by fxm »

ganache wrote:I translated it literally as best as I could understood.
So it's not the original C code.
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Re: How to convert from PowerBasic

Post by ganache »

Thanks frisian.
It seems to work allright. I will play with it and try to customize it.
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: How to convert from PowerBasic

Post by frisian »

If you search the internet then it's possible to find the code snippet.
https://discuss.codechef.com/questions/ ... a-tutorial

Code: Select all

#include<stdio.h>
int main()
{
    int t;
    int a[200]; //array will have the capacity to store 200 digits.
    int n,i,j,temp,m,x;

    scanf("%d",&t);
    while(t--)
    {
       scanf("%d",&n);
       a[0]=1;  //initializes array with only 1 digit, the digit 1.
       m=1;    // initializes digit counter

       temp = 0; //Initializes carry variable to 0.
       for(i=1;i<=n;i++)
       {
            for(j=0;j<m;j++)
            {
               x = a[j]*i+temp; //x contains the digit by digit product
               a[j]=x%10; //Contains the digit to store in position j
               temp = x/10; //Contains the carry value that will be stored on later indexes
            }
             while(temp>0) //while loop that will store the carry value on array.
             { 
               a[m]=temp%10;
               temp = temp/10;
               m++; // increments digit counter
             }
      }
              for(i=m-1;i>=0;i--) //printing answer
              printf("%d",a[i]);
              printf("\n");
    }
    return 0;
}
Post Reply