December 04, 2020, 11:10:07 AM

### Author Topic: [bb] MathToString\$(...) by ShadowTurtle [ 1+ years ago ]  (Read 581 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] MathToString\$(...) by ShadowTurtle [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : MathToString\$(...)
Posted : 1+ years ago

Description : New in 1.5: More Operators

have fun

Code :
Code: BlitzBasic
1. Print MathToString("3")
2. Print MathToString("2/(3+2)")
3. Print MathToString("5>1 And 5<100")
4.
5. Function MathToString\$(TheMath\$, unit = 0, divnow = 0)
6.   Local MyParam\$ = "*/^+-=<>&|%@", MyNumbs\$ = "0123456789.", MyDivParam\$ = "*/^"
7.   Local Ziffer\$, ScanPos, MathAnswer#, MathArt\$, MathPower#, OldMathPower#
8.   Local Scan, ScanNumber\$, OldScanNumber\$, MathScan\$, MyScanText\$
9.
10.   Local bscan, bscannow, bscanhave, ScanPosA, ScanPosB
11.
12.   Local deScan, deMathScan\$, deMath
13.
14.   Local debsScan
15.
16.   TheMath\$ = Lower(TheMath\$)
17.   TheMath\$ = Replace(TheMath\$, "and", "&")
18.   TheMath\$ = Replace(TheMath\$, "xor", "@")
19.   TheMath\$ = Replace(TheMath\$, "or", "|")
20.   TheMath\$ = Replace(TheMath\$, "mod", "%")
21.
22.   MathScan\$ = Replace(TheMath\$, " ", "") : debsScan = 1
23.
24.   While bscan < Len(MathScan\$)
25.     bscan = bscan + 1
26.     If Mid\$(MathScan\$, bscan, 1) = "(" Then
27.       ScanPosA = bscan : bscannow = 1
28.       While bscannow
29.        If Mid\$(MathScan\$, bscan, 1) = "(" Then bscanhave = bscanhave + 1
30.        If Mid\$(MathScan\$, bscan, 1) = ")" Then bscanhave = bscanhave - 1
31.        If bscanhave = 0 Then bscannow = 0
32.        bscan = bscan + 1
33.        If KeyDown(1) Then End
34.       Wend
35.
36.       ScanPosB = bscan
37.
38.       MyScanText\$ = Mid\$(MathScan\$, ScanPosA+1, ScanPosB - ScanPosA - 2)
39.
40.       MyScanText\$ = MathToString\$(MyScanText\$, unit + 1)
41.       MathScan\$ = Replace(MathScan\$, Mid\$(MathScan\$, ScanPosA, ScanPosB - ScanPosA), MyScanText\$)
42.       bscan = 0
43.     End If
44.
45.     If KeyDown(1) Then End
46.   Wend
47.
48.   .NewMathScan
49.
50.   deMathScan\$ = MathScan\$
51.
52.   Scan = InMid\$(MathScan\$, MyParam\$)
53.   If Scan Then
54.     ScanNumber\$ = Mid\$(MathScan\$, 1, Scan-1)
55.     MathScan\$ = Mid\$(MathScan\$, Scan)
57.   Else
58.     Return MathScan\$
59.   End If
60.
61.   deScan = 1
62.
63.   While Not MathScan\$ = ""
64.     uu\$ = MathScan\$
65.
66.     MathArt\$ = Mid\$(MathScan\$, 1, 1)
67.     MathScan\$ = Mid\$(MathScan\$, 2)
68.
69.     If Mid\$(MathScan\$,1,1) = "-" Then
70.       MathPower# = -1
71.       MathScan\$ = Mid\$(MathScan\$, 2)
72.     Else
73.       MathPower# = 1
74.     End If
75.
76.     Scan = InMid\$(MathScan\$, MyParam\$)
77.     OldScanNumber\$ = ScanNumber\$
78.     OldMathPower# = MathPower#
79.     ScanNumber\$ = Mid\$(MathScan\$, 1, Scan-1)
80.
81.     MathScan\$ = Mid\$(MathScan\$, Len(ScanNumber\$)+1)
82.
83.     If MathArt\$ = "+" Then
85.     ElseIf MathArt\$ = "-" Then
87.     ElseIf MathArt\$ = "*" Then
88.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) * (val2(ScanNumber\$)*MathPower#)
89.        If MathPower# = -1 Then
90.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "*-" + ScanNumber\$, "-" + Str\$(MathAnswer))
91.        ElseIf MathPower# = 1 Then
92.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "*" + ScanNumber\$, Str\$(MathAnswer))
93.        End If
94.        Goto NewMathScan
95.     ElseIf MathArt\$ = "/" Then
96.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) / (val2(ScanNumber\$)*MathPower#)
97.        If MathPower# = -1 Then
98.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "/-" + ScanNumber\$, "-" + Str\$(MathAnswer))
99.        ElseIf MathPower# = 1 Then
100.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "/" + ScanNumber\$, Str\$(MathAnswer))
101.        End If
102.        Goto NewMathScan
103.     ElseIf MathArt\$ = "^" Then
104.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) ^ (val2(ScanNumber\$)*MathPower#)
105.        If MathPower# = -1 Then
106.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "^-" + ScanNumber\$, "-" + Str\$(MathAnswer))
107.        ElseIf MathPower# = 1 Then
108.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "^" + ScanNumber\$, Str\$(MathAnswer))
109.        End If
110.        Goto NewMathScan
111.     ElseIf MathArt\$ = "=" Then
112.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) = (val2(ScanNumber\$)*MathPower#)
113.        If MathPower# = -1 Then
114.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "=-" + ScanNumber\$, "-" + Str\$(MathAnswer))
115.        ElseIf MathPower# = 1 Then
116.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "=" + ScanNumber\$, Str\$(MathAnswer))
117.        End If
118.        Goto NewMathScan
119.     ElseIf MathArt\$ = "<" Then
120.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) < (val2(ScanNumber\$)*MathPower#)
121.        If MathPower# = -1 Then
122.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "<-" + ScanNumber\$, "-" + Str\$(MathAnswer))
123.        ElseIf MathPower# = 1 Then
124.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "<" + ScanNumber\$, Str\$(MathAnswer))
125.        End If
126.        Goto NewMathScan
127.     ElseIf MathArt\$ = ">" Then
128.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) > (val2(ScanNumber\$)*MathPower#)
129.        If MathPower# = -1 Then
130.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + ">-" + ScanNumber\$, "-" + Str\$(MathAnswer))
131.        ElseIf MathPower# = 1 Then
132.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + ">" + ScanNumber\$, Str\$(MathAnswer))
133.        End If
134.        Goto NewMathScan
135.     ElseIf MathArt\$ = "&" Then
136.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) And (val2(ScanNumber\$)*MathPower#)
137.        If MathPower# = -1 Then
138.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "&-" + ScanNumber\$, "-" + Str\$(MathAnswer))
139.        ElseIf MathPower# = 1 Then
140.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "&" + ScanNumber\$, Str\$(MathAnswer))
141.        End If
142.        Goto NewMathScan
143.     ElseIf MathArt\$ = "|" Then
144.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) Or (val2(ScanNumber\$)*MathPower#)
145.        If MathPower# = -1 Then
146.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "|-" + ScanNumber\$, "-" + Str\$(MathAnswer))
147.        ElseIf MathPower# = 1 Then
148.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "|" + ScanNumber\$, Str\$(MathAnswer))
149.        End If
150.        Goto NewMathScan
151.     ElseIf MathArt\$ = "%" Then
152.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) Mod (val2(ScanNumber\$)*MathPower#)
153.        If MathPower# = -1 Then
154.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "%-" + ScanNumber\$, "-" + Str\$(MathAnswer))
155.        ElseIf MathPower# = 1 Then
156.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "%" + ScanNumber\$, Str\$(MathAnswer))
157.        End If
158.        Goto NewMathScan
159.     ElseIf MathArt\$ = "@" Then
160.        MathAnswer# = (val2(OldScanNumber\$)*OldMathPower#) Xor (val2(ScanNumber\$)*MathPower#)
161.        If MathPower# = -1 Then
162.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "@-" + ScanNumber\$, "-" + Str\$(MathAnswer))
163.        ElseIf MathPower# = 1 Then
164.          MathScan\$ = Replace(deMathScan\$, OldScanNumber\$ + "@" + ScanNumber\$, Str\$(MathAnswer))
165.        End If
166.        Goto NewMathScan
167.     Else
168.        Return "SYNTAX ERROR"
169.     End If
170.   Wend
171.
173. End Function
174.
175. Function InMid\$(A\$, B\$) ; in benutzung
176.   Local C, Q, W
177.   C = 0
178.   For Q = 1 To Len(A\$)
179.     For W = 1 To Len(B\$)
180.       If (Mid\$(A\$, Q, 1) = Mid\$(B\$, W, 1)) And C = 0 Then C = Q : Exit
181.     Next
182.     If C>0 Then Exit
183.   Next
184.   Return C
185. End Function
186.
187. Function val2#(sstring\$)
188. Local temp#=0
189. Local decimal=0
190. Local sign=1
191. Local a
192. Local b
193. Local c
194. Local base=10
195. a=Instr(sstring\$,"-",1)
196. If a Then negative=-1
197. b=Instr(sstring\$,"&",a+1)
198. If b Then
199.   Select Mid\$(sstring\$,a+1,1)
200.   Case "B", "b"
201.     base=2
202.     a=b+1
203.   Case "O", "o"
204.     base=8
205.     a=b+1
206.   Case "H", "h"
207.     base=16
208.     a=b+1
209.   Default
210.     base=10
211.   End Select
212. End If
213. decimal=0
214. For b=a+1 To Len(sstring\$)
215.   c=Asc(Mid(sstring\$,b,1))
216.   Select c
217.   Case 44          ;","
218.     Goto skip
219.   Case 45          ;"-"
220.     sign=-sign
221.   Case 46          ;"."
222.     decimal=1
223.   Case 48,49,50,51,52,53,54,55,56,57   ;"0" To "9"
224.     temp#=temp*base+c-48
225.     If decimal Then decimal=decimal*base
226.   Case 65,66,67,68,69,60    ;"A" to "F"
227.     If base=16 Then
228.       temp#=temp#*base+c-55
229.       If decimal Then decimal=decimal*base
230.     Else
231.       Goto fini
232.     EndIf
233.   Case 97,98,99,100,101,102   ;"a" to "f"
234.     If base=16 Then
235.       temp#=temp#*base+c-87
236.       If decimal Then decimal=decimal*base
237.     Else
238.       Goto fini
239.     EndIf
240.   Default
241.     Goto fini
242.   End Select
243. .skip
244. Next
245. .fini
246. If decimal Then temp#=temp#/decimal
247.
248. If negative = -1 Then
249.   Return -(temp#*sign)
250. Else
251.   Return temp#*sign
252. End If
253. End Function

virtlands(Posted 1+ years ago)

I tested this code. So, in other words, it parses math "text", computes it, and returns the result as updated "text".  QUite interesting.  I can imagine you took days on this.

weeks

virtlands(Posted 1+ years ago)

I just had a wild idea for a future project...(A:) You can extend this concept of text computations to include various mathematical unicode characters,..., and therefore compute that text. For example, you can parse (and compute) text that has subscripts, superscripts, fractions, etc.    SC Unipad (excellent unicode editor) :: <a href="http://www.unipad.org/main/" target="_blank">http://www.unipad.org/main/[/url]....Here's some Unicode code that I did (several years ago). I forgot what it does; you're welcome to use it. UNICODE to UTF8 conversion Functions
Code: [Select]
`;; Some important UNICODE to UTF8 conversion Functions:;;;;  You'll need to purchase your copy of FastText.DLLInclude "C:Blitz3d ProjectsFastText_unicode.BB"    ;; Global WhiteSquare_UTF8\$ = Uni_to_UTF8\$(\$1111)  ;; ??????Type UTF8     Field byte[4]     Field s\$End Type UTF8sam.UTF8 = New UTF8   ;; UTF8 Sample Graphics 1024,480,32,2 Local s\$ UniFont0=LoadFont("Tahoma",25) font0=LoadFont("arial.ttf",20)  SetFont UniFont0  horiz = 60  ;; a horizontal placement  ;; This loop prints UTF8 (unicode) characters one-at-a-time.  ;;  For u = \$24E6 To \$24FE        Text horiz,40, Uni_To_UTF8(u)                horiz = horiz +20 Next  ;; This loop builds up a string first, and then prints them all at once ;; For u = \$2730 To \$274D       s\$ = s\$ + uni_to_utf8(u)            Next       Text 60,70, s     WaitKey():End ; This function converts a unicode value; into a UTF8 byte string;Function Uni_to_UTF8\$(u) Local b1,b2,b3,b4 If (u < \$80)         Return Chr\$(u)     End If  If (u < \$800)     b1 = ((u Shr 6) And \$1f) Or \$c0     b2 = (u And \$3f) Or \$80     Return Chr\$(b1)+Chr\$(b2)    End If  If (u<\$10000)     b1 = ((u Shr 12) And \$0f) Or \$e0     b2 = ((u Shr 6) And \$3f) Or \$80     b3 = (u And \$3f) Or \$80     Return Chr\$(b1)+Chr\$(b2)+Chr\$(b3)  End If  If (u<\$110000)     b1 = ((u Shr 18) And \$7) Or \$f0     b2 = ((u Shr 12) And \$3f) Or \$80     b3 = ((u Shr 6) And \$3f) Or \$80     b4 = (u And \$3f) Or \$80      Return Chr\$(b1)+Chr\$(b2)+Chr\$(b3)+Chr\$(b4) End If End Function ;  creates a UTF8 type from a unicode input;Function Uni_to_UTF8t.UTF8(ut.UTF8,u)  Local b1,b2,b3,b4  If (u < \$80)         Return SetUTF8(ut,u)   End If  If (u < \$800)     b1 = ((u Shr 6) And \$1f) Or \$c0     b2 = (u And \$3f) Or \$80     Return SetUTF8(ut,b1,b2)      End If  If (u<\$10000)     b1 = ((u Shr 12) And \$0f) Or \$e0     b2 = ((u Shr 6) And \$3f) Or \$80     b3 = (u And \$3f) Or \$80         Return SetUTF8(ut,b1,b2,b3)    End If  If (u<\$110000)     b1 = ((u Shr 18) And \$7) Or \$f0     b2 = ((u Shr 12) And \$3f) Or \$80     b3 = ((u Shr 6) And \$3f) Or \$80     b4 = (u And \$3f) Or \$80           Return SetUTF8(ut,b1,b2,b3,b4) End If End Function Function SetUTF8.UTF8(ut.UTF8, b1=0,b2=0,b3=0,b4=0)  Local s\$    If ut=Null Then ut=New UTF8    utyte[1] = b1  utyte[2] = b2  utyte[3] = b3  utyte[4] = b4  For z=1 To 4      If utyte[z]=0 Then Exit      s = s + utyte[z]  Next   uts = s  Return utEnd Function `;=== And, here is "FastText_Unicode.BB" , include file ::===
Code: [Select]
`; Encoding constsConst FT_ASCII = 0Const FT_UNICODE = 1; Smooth const (LoadFont function)Const FT_DEFAULT = 0Const FT_NONANTIALIASED = 1Const FT_ANTIALIASED = 2Const FT_CLEARTYPE = 3; Horisontal align consts (X axis)Const FT_NONE = 0Const FT_LEFT = 0Const FT_CENTER = 1Const FT_RIGHT = 2; Vertical align consts (Y axis)Const FT_TOP = 0Const FT_MID = 1Const FT_MIDDLE = 1Const FT_BOTTOM = 2Const FT_BASELINE = 3; TextRect Formatting constsConst FT_NOBREAK = 256Const FT_NOCLIP = 512Const FT_CALCRECT = 1024Function LoadFont(name\$="Tahoma", height=13, bold=0, italic=0, underline=0, angle#=0, smooth=FT_ANTIALIASED, encoding=FT_UNICODE) Return LoadFont_(name,height,bold,italic,underline,angle,smooth,encoding)End FunctionFunction SetFont(font) SetFont_( font )End FunctionFunction FreeFont(font) FreeFont_( font )End FunctionFunction FontWidth() Return FontWidth_()End FunctionFunction FontHeight() Return FontHeight_()End FunctionFunction Text(x, y, txt\$, cx=FT_LEFT, cy=FT_TOP, encoding=FT_UNICODE) Text_(x,y,txt,cx,cy,encoding)End FunctionFunction TextRect(x, y, w, h, txt\$, formatting=FT_LEFT, encoding=FT_UNICODE) Return TextRect_(x,y,w,h,txt,formatting,encoding)End FunctionFunction TextBackground(r%=-1, g%=-1, b%=-1) TextBackground_ r,g,bEnd FunctionFunction StringWidth(txt\$, encoding=FT_UNICODE) Return StringWidth_(txt,encoding)End FunctionFunction StringHeight(txt\$, encoding=FT_UNICODE) Return StringHeight_(txt,encoding)End Function`