Qbasicnews.com

Full Version: What about a collection of user routines?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This may have already been done and I haven't seen it (if it has let me know where).....

One thought might be to put a sticky post on the forum for each catagory of routines (String Manip, Math, Sound, etc) and let people post their routines for others to cut-n-paste. This idea came to me as I was doing a Replace function for a math parser I'm working on.

Just a thought

Code:
Function Replace(Expression as string, Find as string, WithWhat as String) as string
   dim FirstHalf as string
   dim SecondHalf As String
  
   dim Length as integer
  
   dim TExpression as string
   dim Where as integer
  
   Length = Len(Find)
  
   TExpression = Expression
   Where = instr(TExpression,Find)
  
   do while Where <> 0
      FirstHalf = left$(TExpression,Where-1)
      SecondHalf = Right$(TExpression,len(TExpression)-(Where+Length-1))
      TExpression = FirstHalf + WithWhat + SecondHalf
      Where = instr(TExpression,Find)
   loop

   Replace = TExpression

end function
It's a good idea, if it isnt done here, feel free to head over to www.freebasic.tk (it's in my sig if .tk doesent work for you) and do it there.

We're already planning a tutorials section on fbtk, and a code snippet section is always good Big Grin
Good idea, some commonly used math functions ....

Code:
Function fact(arg1 as double) as double
   ''Returns the factorial (n!)
        Dim ct as integer
        Dim tempval as double
            arg1=int(arg1)
            if arg1=0 or arg1=1 then
              fact=1
         exit function
              end if
              ct=1
          tempval=1
          do
          ct=ct+1
              if arg1>=ct then
              tempval=tempval*ct
              else
          exit do
              end if
              loop
          fact=tempval
End Function

Function snorm(z As Double) as double
   ''Returns the cummulative normal distribution
   Dim pi as double
   pi=3.14159265358979
   Dim a1 as double,a2 as double,a3 as double,a4 as double,a5 as double,k as double,w as double
    a1 = 0.31938153
    a2 = -0.356563782
    a3 = 1.781477937
    a4 = -1.821255978
    a5 = 1.330274429
   If 0 > z Then w = -1 Else w = 1
   k = 1 / (1 + 0.2316419 * w * z)
   snorm = 0.5 + w * (0.5 - 1 / Sqr(2 * pi) * Exp(-z ^ 2 / 2) * (a1 * k + a2 * k ^ 2 + a3 * k ^ 3 + a4 * k ^ 4 + a5 * k ^ 5))
End Function

FUNCTION snormInv(p AS DOUBLE) as double
  ''Returns the z value for a given probability

    Dim Minz as double,Maxz as double,zVal as double,pVal as double,prec as double
    prec = 1e-06
      Minz = -6.0
      Maxz = 6.0
      zVal = 0.0
      WHILE (Maxz - Minz) > prec
       pVal = snorm(zVal)
       IF pVal > p THEN
        Maxz = zVal
       ELSE
        Minz = zVal
       END IF
       zVal = (Maxz + Minz) * 0.5
      WEND
      snorminv = zVal
END FUNCTION

Function Combin(arg1 as double,arg2 as double) as double
   ''Returns the available Combinations
''arg1 -> number n
''arg2 -> number k
   Dim tempvalue1 as double, tempvalue2 as double
    Dim ct as integer
if int(arg1)<0 or int(arg2)<0 or int(arg1)<int(arg2) then
  Combin=0
  ''TheError=1
  Exit Function
end if
    if int(arg2)=0 then
        Combin=1
        Exit Function
    end if
    tempvalue2=1
    tempvalue1=1
    for ct=1 to int(arg2)
        tempvalue2=tempvalue2*ct
    next ct
    for ct=(int(arg1)-int(arg2)+1) to int(arg1)
        tempvalue1=tempvalue1*ct
    next ct
    Combin=tempvalue1/tempvalue2
End Function

Function Permut(arg1 as double,arg2 as double) as double
   ''Returns the available Permutations
'arg1 -> number n
'arg2 -> number k
Dim tempvalue1 as double
    Dim ct as integer
if int(arg1)<0 or int(arg2)<0 or int(arg1)<int(arg2) then
  Permut=0
  ''TheError=1
  Exit Function
end if
    if int(arg2)=0 then
        Permut=1
        Exit Function
    end if
    tempvalue1=1
    for ct=(int(arg1)-int(arg2)+1) to int(arg1)
        tempvalue1=tempvalue1*ct
    next ct
    Permut=tempvalue1
End Function

Function BinomDist(arg1 as double,arg2 as double,arg3 as double,arg4 as integer) as double
   ''Returns the Binomial probability
''arg1 -> number S
''arg2 -> number of trials
''arg3 -> prob of S
''arg4 -> Cumulative
    Dim i as double
    Dim s as double
    if int(arg2)<0 or int(arg1)<0 or int(arg2)<int(arg1) or arg3>1 or arg3<0 then
        BinomDist=0
  ''TheError=1
     exit function
    end if
    Select Case arg4
     Case 0
         BinomDist = Combin(arg2,arg1)*arg3^(int(arg1))*(1-arg3)^(int(arg2)-int(arg1))
     Case 1
         s=0
         for i=0 to arg1
             s=s+Combin(arg2,i)*arg3^i*(1-arg3)^(int(arg2)-i)
         next i
         BinomDist=s
     Case else
         ''TheError=1
         BinomDist=0
    End Select
End Function

Function Poisson(arg1 as double,arg2 as double,arg3 as integer) as double
   ''Returns the Poisson probability
''arg1 -> number S
''arg2 -> mean
''arg3 -> Cumulative
    Dim i as double
    Dim s as double
    if int(arg1)<0 or arg2<0 then
     Poisson=0
     ''TheError=1
     Exit Function
    end if
    Select Case arg3
     Case 0
         Poisson = exp(-arg2)*arg2^arg1/fact(arg1)
     Case 1
         s=0
         for i=0 to arg1
             s=s+exp(-arg2)*arg2^i/fact(i)
         next i
         Poisson=s
     Case else
         ''TheError=1
         Poisson=0
    End Select
End Function



'' tests
   Print "fact(5)= ";fact(5)
   Print "snorm(2)= ";snorm(2)
   Print "snorminv(0.5)= ";snorminv(0.5)
   Print "combin(5,3)= ";combin(5,3)
   Print "permut(5,3)= ";permut(5,3)
   Print "BinomDist(3,5,0.5,0)= ";BinomDist(3,5,0.5,0)
   Print "Poisson(3,5,0)= ";Poisson(3,5,0)
sleep
Quote:This may have already been done and I haven't seen it (if it has let me know where).....

One thought might be to put a sticky post on the forum for each catagory of routines (String Manip, Math, Sound, etc) and let people post their routines for others to cut-n-paste. This idea came to me as I was doing a Replace function for a math parser I'm working on.

Just a thought

Code:
Function Replace(Expression as string, Find as string, WithWhat as String) as string
   dim FirstHalf as string
   dim SecondHalf As String
  
   dim Length as integer
  
   dim TExpression as string
   dim Where as integer
  
   Length = Len(Find)
  
   TExpression = Expression
   Where = instr(TExpression,Find)
  
   do while Where <> 0
      FirstHalf = left$(TExpression,Where-1)
      SecondHalf = Right$(TExpression,len(TExpression)-(Where+Length-1))
      TExpression = FirstHalf + WithWhat + SecondHalf
      Where = instr(TExpression,Find)
   loop

   Replace = TExpression

end function

If the string WithWhat contains the string Find you will get an infinite loop.
Yep, I'm glad you caught that. I was just using it to find and replace spaces. I guess public peer-review works. :lol:

Try this one (I'm not saying it is perfect though):
Code:
function Replace (Expression as String, Find as String, WithWhat as String) as string

   Dim ReplaceCounter as integer
   dim Where as integer
   dim TExpression as String
   dim Length As integer

  
   TExpression = ""
   Length = len(Expression)
  
   ReplaceCounter=0
   Where = Instr(1,Expression,Find)
  
   if Where = 0 then
      Replace = Expression
      Exit Function
   end if
  
   'Get The First Part
   TExpression = Mid$(Expression,1,Where-1) + WithWhat
   ReplaceCounter = Where + 1
   Where = Instr(ReplaceCounter,Expression,Find)

   DO While Where <> 0
      if Where<>ReplaceCounter then
         TExpression = TExpression + Mid$(Expression,ReplaceCounter,Where-ReplaceCounter) + WithWhat
      else
         TExpression = TExpression +  WithWhat
      end if
      ReplaceCounter = Where + 1
      Where = Instr(ReplaceCounter,Expression,Find)
   loop
  
   'Get The Last Part
   If ReplaceCounter -1 <> Length then TExpression = TExpression + Right$(Expression,Length-ReplaceCounter+1)
   Replace = TExpression
  
End Function