Recent posts

#41
OxygenBasic Examples / Re: Function qt
Last post by Charles Pegge - April 02, 2024, 08:51:17 PM
Almost:

Function factorial(x As Integer) As Integer
    ' This is a recursive function
    ' to find the factorial of an integer

    If x <= 1 Then
        return 1
    Else
        'factorial = x * factorial(x - 1)
        return x * factorial(x-1)
    End If
End Function

print factorial(4) '24
#42
OxygenBasic Examples / Function qt
Last post by Frank Brübach - April 02, 2024, 08:25:04 PM
Hi all hi Charles

IS this Kind of function structure possible in oxygen?



Function factorial(x As Integer) As Integer
    ' This is a recursive function
    ' to find the factorial of an integer

    If x = 1 Then
        factorial = 1
    Else
        'factorial = x * factorial(x - 1)
        return x * factorial(x-1) 
    End If
End Function

print factorial(2)

Dim num As Integer
    num = 3

Print "The factorial of " + num  + " is "  ''+ str(factorial(num) )
#43
Next on the #WEFAgenda2030 is a food-crisis.
They also destroy all companies making "vertical farming".

#44
Bitcoin by today is somewhere between 60000 and 70000 US$.
"Plan B" sees the value of 1 BITCOIN at 500.000 in 2025 so get in if you can!
Even if you buy just 0.1 Bitcoin.

⏰ Timestamps
0:16 Bitcoin Stock-to-Flow Model (S2F)
4:27 Bitcoin Market Cycle
5:54 Bitcoin Relative Strength Index (RSI)
8:03 Bitcoin 200 Week Moving Average (200WMA)
9:04 Bitcoin Realized Price
11:47 Bitcoin Realized Return
12:40 Bitcoin in Profit

#45
Source Code and Discussions / Re: Pattern-Matcher for Wildca...
Last post by Theo Gottwald - April 02, 2024, 08:18:56 AM
Patternmatcher Simple Variant.
This function takes the following parameters:
' Input:
- `text`: The string to search for patterns.
- `pattern`: The pattern to match against the text.
- `occurrence`: The x'th occurrence of the pattern to find (1-based index).
Output:
- `position`: ByRef parameter to store the starting position of the found pattern.
- `length`: ByRef

Function PatternMatch(ByVal text As String, ByVal pattern As String, ByVal occurrence As Long, ByRef position As Long, ByRef length As Long) As Long
    Dim i As Long, j As Long, k As Long
    Dim found As Long, match As Long
    Dim patternLength As Long, textLength As Long
   
    patternLength = Len(pattern)
    textLength = Len(text)
   
    found = 0
    i = 1
    j = 1
   
    Do While i <= textLength
        Select Case Mid$(pattern, j, 1)
            Case "+"
                If j = patternLength Then
                    found = found + 1
                    If found = occurrence Then
                        position = i - patternLength + 1
                        length = patternLength
                        PatternMatch = -1
                        Exit Function
                    End If
                    j = 1
                Else
                    j = j + 1
                End If
                i = i + 1
           
            Case "*"
                If j = patternLength Then
                    found = found + 1
                    If found = occurrence Then
                        position = i - patternLength + 1
                        length = textLength - i + 1
                        PatternMatch = -1
                        Exit Function
                    End If
                    j = 1
                Else
                    j = j + 1
                End If
           
            Case "?"
                If j = patternLength Then
                    found = found + 1
                    If found = occurrence Then
                        position = i - patternLength + 1
                        length = patternLength
                        PatternMatch = -1
                        Exit Function
                    End If
                    j = 1
                Else
                    j = j + 1
                End If
                i = i + 1
           
            Case "["
                match = 0
                j = j + 1
                Do While Mid$(pattern, j, 1) <> "]"
                    If Mid$(pattern, j, 1) = Mid$(text, i, 1) Then
                        match = -1
                        Exit Do
                    End If
                    j = j + 1
                Loop
                If match Then
                    If j = patternLength Then
                        found = found + 1
                        If found = occurrence Then
                            position = i - patternLength + 1
                            length = patternLength
                            PatternMatch = -1
                            Exit Function
                        End If
                        j = 1
                    Else
                        j = j + 1
                    End If
                    i = i + 1
                Else
                    i = i + 1
                    j = 1
                End If
           
            Case Else
                If Mid$(pattern, j, 1) = Mid$(text, i, 1) Then
                    If j = patternLength Then
                        found = found + 1
                        If found = occurrence Then
                            position = i - patternLength + 1
                            length = patternLength
                            PatternMatch = -1
                            Exit Function
                        End If
                        j = 1
                    Else
                        j = j + 1
                    End If
                    i = i + 1
                Else
                    i = i + 1
                    j = 1
                End If
        End Select
    Loop
   
    PatternMatch = 0
End Function
' Coded by MISTRAL LARGE
#46
Source Code and Discussions / Pattern-Matcher for Wildcards
Last post by Theo Gottwald - April 02, 2024, 08:16:11 AM
Version 1: using Glushkov's algorithm implementation in PowerBasic that handles the wildcard characters and the cases where a character class is empty and a wildcard character needs to be escaped.

' Global variables for the DFA
Dim dfaStates() As DFAState
Dim dfaInitialState As Long
Dim dfaAcceptingStates As String ' Bitset representing the accepting states

Type DFAState
    isAccepting As Boolean
    transitions(0 To 255) As Long ' Assuming ASCII characters
End Type

Function PatternMatch(ByVal text As String, ByVal pattern As String, ByVal occurrence As Long, ByRef position As Long, ByRef length As Long) As Long
    ' Preprocess the pattern to build a DFA
    BuildDFA(pattern)

    ' Match the text against the DFA
    Dim state As Long
    state = dfaInitialState
    Dim found As Long
    found = 0
    Dim i As Long
    i = 1
    Do While i <= Len(text)
        state = DFATransition(state, Mid$(text, i, 1))
        If DFAIsAccepting(state) Then
            found = found + 1
            If found = occurrence Then
                position = i - Len(pattern) + 1
                length = Len(pattern)
                PatternMatch = -1
                Exit Function
            End If
        End If
        i = i + 1
    Loop

    PatternMatch = 0
End Function

Sub BuildDFA(ByVal pattern As String)
    ' Implement Glushkov's algorithm to build a DFA from the pattern

    Dim numStates As Long
    numStates = Len(pattern) + 1
    ReDim dfaStates(0 To numStates - 1)

    Dim i As Long
    For i = 0 To numStates - 1
        Dim state As DFAState
        state.isAccepting = (i = numStates - 1)
        For j = 0 To 255
            state.transitions(j) = -1 ' -1 represents an invalid transition
        Next j
        dfaStates(i) = state
    Next i

    Dim currentState As Long
    currentState = 0
    For i = 1 To Len(pattern)
        Dim c As String
        c = Mid$(pattern, i, 1)
        Dim charCode As Long
        charCode = Asc(c)
        Select Case c
            Case "+"
                ' Handle the "+" wildcard character
                ' This is equivalent to the previous character being optional
                ' We add a transition from the previous state to the current state for the previous character
                Dim prevCharCode As Long
                prevCharCode = Asc(Mid$(pattern, i - 1, 1))
                dfaStates(currentState - 1).transitions(prevCharCode) = currentState

            Case "*"
                ' Handle the "*" wildcard character
                ' This is equivalent to the previous character being repeated zero or more times
                ' We add a transition from the previous state to itself for the previous character
                ' We also add a transition from the previous state to the current state for any character
                prevCharCode = Asc(Mid$(pattern, i - 1, 1))
                dfaStates(currentState - 1).transitions(prevCharCode) = currentState - 1
                For j = 0 To 255
                    dfaStates(currentState - 1).transitions(j) = currentState
                Next j

            Case "?"
                ' Handle the "?" wildcard character
                ' This is equivalent to the previous character being optional
                ' We add a transition from the previous state to the current state for any character
                For j = 0 To 255
                    dfaStates(currentState - 1).transitions(j) = currentState
                Next j

            Case "["
                ' Handle the "[" wildcard character
                ' We start a new character class
                ' We also handle the case where the character class is empty
                Dim charClassStart As Long
                charClassStart = i
                If i = Len(pattern) Or Mid$(pattern, i + 1, 1) = "]" Then
                    ' The character class is empty, so we add a transition for any character
                    For j = 0 To 255
                        dfaStates(currentState - 1).transitions(j) = currentState
                    Next j
                End If

            Case "]"
                ' Handle the "]" wildcard character
                ' We end a character class
                ' We add a transition from the previous state to the current state for any character in the class
                Dim charClassEnd As Long
                charClassEnd = i
                For j = charClassStart + 1 To charClassEnd - 1
                    Dim charClassChar As String
                    charClassChar = Mid$(pattern, j, 1)
                    Dim charClassCharCode As Long
                    charClassCharCode = Asc(charClassChar)
                    dfaStates(currentState - 1).transitions(charClassCharCode) = currentState
                Next j

            Case Else
                ' Handle a regular character or an escaped wildcard character
                If c = "\" Then
                    ' The next character is escaped
                    Dim nextChar As String
                    nextChar = Mid$(pattern, i + 1, 1)
                    Dim nextCharCode As Long
                    nextCharCode = Asc(nextChar)
                    dfaStates(currentState).transitions(nextCharCode) = currentState + 1
                    currentState = currentState + 1
                    i = i + 1 ' Skip the next character
                Else
                    dfaStates(currentState).transitions(charCode) = currentState + 1
                    currentState = currentState + 1
                End If
        End Select
    Next i

    ' Set the accepting states
    dfaAcceptingStates = Str$(2 ^ (numStates - 1)) ' Only the last state is accepting
    dfaInitialState = 0
End Sub

Function DFAIsAccepting(ByVal state As Long) As Boolean
    Dim acceptingStates As String
    acceptingStates = Str$(dfaAcceptingStates)
    Dim stateBit As Long
    stateBit = (state Mod 10)
    Return (stateBit = 1)
End Function

Function DFATransition(ByVal state As Long, ByVal c As String) As Long
    Dim charCode As Long
    charCode = Asc(c)
    Dim transition As Long
    transition = dfaStates(state).transitions(charCode)
    If transition = -1 Then
        ' If there's no valid transition, stay in the same state
        transition = state
    End If
    Return transition
End Function
' using MISTRAL LARGE


#47
OxygenBasic Examples / Re: C++ String example to o2
Last post by Frank Brübach - April 01, 2024, 04:26:01 PM
Here is complete Code expl of c++ Translation to oxygen

Oxygen:

' 01-04-2024, c++ translation to powerbasic, oxygen works
' by frank bruebach
'
' c++ to oxygen translation
'
'----------------------------------------------------------------- //
' topic string formating c++

'You can use sprintf in combination
'with std::string.c_str().

'------------ 1) --------------------------------------------------- //
'DIM a AS STRING, b AS STRING, c AS STRING
'DIM x(1 TO LEN(a) + LEN(b) + LEN(c) + 32) AS 'BYTE

'a = "test"
'b = "text.txt"
'c = "text1.txt"

' Populate the bytearray
'DIM i AS INTEGER
'FOR i = 1 TO LEN(a)
 '   x(i) = ASC(MID(a, i, 1))
'NEXT i

'FOR i = 1 TO LEN(b)
'    x(LEN(a) + i + 1) = ASC(MID(b, i, 1))
'NEXT i

'FOR i = 1 TO LEN(c)
 '   x(LEN(a) + LEN(b) + i + 2) = ASC(MID(c, i, 1))
'NEXT i

' Convert the bytearray to string
'DIM result AS STRING
'DIM x AS BYTE
'FOR i = 1 TO LEN(x)
'    result = result + CHR(x(i))
'NEXT i

'PRINT result
'----------------------------------------------------------------- //

uses console

'dim as string a="test"
'dim as byte x at strptr(a)

'dim as string b="text.txt"
'dim as byte x at strptr(b)

'dim as string c="text1.txt"
'dim as byte x at strptr(c)

'DIM a AS STRING, b AS STRING, c AS STRING
'DIM x(LEN(a) + LEN(b) + LEN(c) + 32) AS BYTE

'a = "test"
'b = "text.txt"
'c = "text1.txt"

dim as string a="test"
dim as byte x at strptr(a)

'--------------------------------------------- //
' Populate the bytearray
DIM i AS INTEGER
FOR i = 1 TO LEN(a)
    'x(i) = ASC(MID(a, i, 1))
     'print i tab x[i] tab ASC(mid(a,i,1)) cr
      print i tab x[i] tab mid(a,i,1) cr
NEXT i
'--------------------------------------------- //

dim as string b="text.txt"
dim as byte x at strptr(b)

FOR i = 1 TO LEN(b)
    'x(LEN(a) + i + 1) = ASC(MID(b, i, 1))
     'print i tab x[i] tab ASC(mid(b,i,1)) cr
     print i tab x[i] tab mid(b,i,1) cr
NEXT i
'--------------------------------------------- //
dim as string c="text1.txt"
dim as byte x at strptr(c)


FOR i = 1 TO LEN(c)
    'x(LEN(a) + LEN(b) + i + 2) = ASC(MID(c, i, 1))
     'print i tab x[i] tab ASC(mid(c,i,1)) cr
     print i tab x[i] tab mid(c,i,1) cr
NEXT i

' Convert the bytearray to string

DIM result AS String
Dim xy As Long
FOR i = 1 TO LEN(xy)
    result = result + CHR(x(i))
NEXT i

PRINT result
wait

Here is Code of c++ example

' c++ code

#include <iostream>
#include <string>

'You can use sprintf in combination with
' std::string.c_str().

'c_str() returns a const char* and works with sprintf:

string a = "test";
string b = "text.txt";
string c = "text1.txt";
char* x = new char[a.length() + b.length() + c.length() + 32];

sprintf(x, "%s %s > %s", a.c_str(), b.c_str(), c.c_str() );

string str = x;
delete[] x;
#48
OxygenBasic Examples / Re: C++ String example to o2
Last post by Frank Brübach - April 01, 2024, 03:31:12 PM
Yes thanks Charles :)

I think I have the solution  I have translated IT to Powerbasic First and the example Works already now to oxygen  yes I See the other way of Byte overlay with oxygen

Here is adapted Code snippets and IT Works with oxygen

dim as string a="test"
dim as byte x at strptr(a)

'--------------------------------------------- //
' Populate the bytearray
DIM i AS INTEGER
FOR i = 1 TO LEN(a)
    'x(i) = ASC(MID(a, i, 1))
     print i tab x[i] tab ASC(mid(a,i,1)) cr
NEXT i

Every Basic language has His own tricks ;)
#49
OxygenBasic Examples / Re: C++ String example to o2
Last post by Charles Pegge - April 01, 2024, 03:21:07 PM
You can read/write the bytes of any string by creating a byte overlay

byte b at strptr(s)

Also, using the console makes it easy to display results in a table.

uses console
string s="abcdefghij"
byte b at strptr(s)
int i
for i=1 to len(s)
  print i tab b[i] tab mid(s,i,1) cr
next
wait
#50
OxygenBasic Examples / Re: C++ String example to o2
Last post by Frank Brübach - April 01, 2024, 11:36:27 AM
Hi Charles perhaps you can Help this way better for Translation to oxygen :)

Your c++ sprintf o2 Code example I know already thx

DIM a AS STRING, b AS STRING, c AS STRING
DIM x(1 TO LEN(a) + LEN(b) + LEN(c) + 32) AS BYTE

a = "test"
b = "text.txt"
c = "text1.txt"

' Populate the bytearray
DIM i AS INTEGER
FOR i = 1 TO LEN(a)
    x(i) = ASC(MID(a, i, 1))
NEXT i

FOR i = 1 TO LEN(b)
    x(LEN(a) + i + 1) = ASC(MID(b, i, 1))
NEXT i

FOR i = 1 TO LEN(c)
    x(LEN(a) + LEN(b) + i + 2) = ASC(MID(c, i, 1))
NEXT i

' Convert the bytearray to string
DIM result AS STRING
DIM x AS BYTE
FOR i = 1 TO LEN(x)
    result = result + CHR(x(i))
NEXT i

PRINT result

My Second attempt for Translation