Dealing with entered equations (Development Foros MSX)MSX Resource Center            
                       
English Nederlands Espa�ol Portugu�s Russian                  
 Noticias
   Página principal
  Almacén de noticias
  Temas de noticias

 Recursos
   Foros MSX
  Artículos
  Analisis
  Informe de ferias/RUs
  Álbum de fotos
  Ferias y encuentros
  Encuestas
  Enlaces
  Buscar

 Software
   Descargas
  Tienda Online

 MRC
   Quiénes somos
  Únete a nuestro equipo
  Donar
  Políticas
  Contacta con nosotros
  Enlázanos
  Estadísticas

 Buscar
 
  

  

 Login
 

Login

Contraseña




¿Aún no tienes una cuenta? ¡Conviértete en miembro del MSX Resource Center! ¡Únete a nosotros!.


 Estadísticas
 

Hay 54 invitados y 5 miembros en línea

Eres un usuario anónimo.
 

Foros MSX


Foros MSX

Development - Dealing with entered equations

Autor

Dealing with entered equations

Msx-Basic
msx friend
Mensajes: 10
Publicado: Enero 13 2005, 14:51   
Hello guys,i have an important question and i think that it is difficult..
how can i make a program to tell the user to enter a mathematical equation and the program will draw this equation?????
drawing is not the problem,the problem is how can we enter the equation in Run-Time ..it means how to convert string variable to a mathematical expression.

note:the program is in any basic language..(msx,quick basic,gwbasic...etc.)

thanks


AuroraMSX

msx master
Mensajes: 1262
Publicado: Enero 13 2005, 15:23   
That's not an easy one, actually. You'll need to write a parser that analyzes the entered string, account for syntax errors, possibly have a few pre-defined functions etc etc. So, actually you'll be writing a complete interpreter for your equations

Maybe you can convince the BASIC interpreter to somehow convert the entered text string (math equation) into a line of BASIC saying DEFFNA(x)=<the equation> ...
wolf_

msx legend
Mensajes: 4779
Publicado: Enero 13 2005, 16:10   
you need a formula parser indeed.. I only have some Blitzbasic parser here, I found it in the code archives of blitzbasic.com

poke/peek works with banks ..

A bank is a piece of mem.. so, if I create 128bytes of mem, then it's like:

bank=createbank(128)

using it:

pokebyte bank,offset,value ; 1 byte-value
pokeshort bank,offset,value ; 2 byte-value
pokeint bank,offset,value ; 4 byte-value
pokefloat bank,offset,value ; 4 byte-value (or 8 byte... dunno I think it's 8..)

reading:

bla=peekbyte(bank,offset) ; reads 1 byte-value
etc. etc.



So, I dunno if it's useful, it's perhaps good pseudocode as a start to make your own parser ..

Function eval#(s$)

	
	Local lens

	s=Lower(s)
	s=Replace(s,"and","&")
	s=Replace(s,"xor","@")
	s=Replace(s,"or","|")
	s=Replace(s,"mod","%")
	lens=Len(s)

	Local value=CreateBank((lens+1)*4)
	Local claimed=CreateBank(lens+1)
	Local m$,i,oldi,gotpoint,nest
	Local cmdpri$,cmdsi$;strings, so numbers can be easily inserted
	Local ci,pri

	
	For i=1 To lens
		m=Mid(s,i,1)
		pri=0
		Select m
		Case "&","|","@"
			pri=1
		Case "=","<",">"
			pri=2
		Case "+","-"
			pri=3
		Case "*","/","%"
			pri=4
		Case "^"
			pri=5
		Case "("
			nest=nest+1
		Case ")"
			nest=nest-1
			If nest<0 Then RuntimeError "too many ')'"
		Default
			If Int(m)<>0 Or m="0" Or m="." Then
				;get number into value bank
				oldi=i
				gotpoint=(m=".")
				Repeat
					i=i+1
					m=Mid(s,i,1)
					If m="." Then
						If gotpoint=1 Then RuntimeError "2 decimal points!"
						gotpoint=1
					Else
						If Int(m)=0 And m<>"0" Then Exit
					EndIf
					PokeByte claimed,i,oldi
				Forever
				PokeFloat value,oldi*4,Float(Mid(s,oldi,i-oldi))
				i=i-1
			Else
				If m<>" " Then RuntimeError "what is '"+m+"' ?"
			EndIf
		End Select
		If pri>0 Then
			;insert operators into list by highest priority to lowest
			pri=pri+5*nest
			For ci=1 To Len(cmdpri)
				If pri>Asc(Mid(cmdpri,ci,1)) Then
					Exit
				EndIf
			Next
			cmdpri=Left(cmdpri,ci-1)+Chr(pri)+Mid(cmdpri,ci)
			cmdsi =Left(cmdsi ,ci-1)+Chr(i  )+Mid(cmdsi ,ci)
		EndIf
	Next
	If nest>0 Then RuntimeError "too many '('"

	Local lenc
	lenc=Len(cmdsi)
	Local mi,ii,ii2,add
	Local lv#,rv#,ans#
	For i=1 To lenc
		mi=Asc(Mid(cmdsi,i,1))
		;find values to the right and left of operator
		For add=-1 To +1 Step 2
			ii=mi+add
			;skip over spaces and parens
			Repeat
				m=Mid(s,ii,1)
				If m<>" " And m<>"(" And m<>")" Then Exit
				ii=ii+add
			Forever
			;magical code that makes sure it uses the correct order of the priorities
			Repeat
				ii2=PeekByte(claimed,ii)
				If ii2=0 Then Exit
				ii=ii2
			Forever
			PokeByte claimed,ii,mi
			If add=-1 Then
				lv=PeekFloat(value,ii*4) 
			Else
				rv=PeekFloat(value,ii*4)
			EndIf
		Next
		;do the math and put result in the value bank a the spot of the operator
		Select Mid(s,mi,1)
			Case "+" ans=lv+rv
			Case "-" ans=lv-rv
			Case "*" ans=lv*rv
			Case "/" ans=lv/rv
			Case "=" ans=lv=rv
			Case "<" ans=lv<rv
			Case ">" ans=lv>rv
			Case "&" ans=lv And rv
			Case "|" ans=lv Or rv
			Case "%" ans=lv Mod rv
			Case "^" ans=lv^rv
			Case "@" ans=lv Xor rv
			Default RuntimeError Mid(s,mi,1)
		End Select
		PokeFloat value,mi*4,ans
	Next

	FreeBank value
	FreeBank claimed
	Return ans
End Function

 
 







(c) 1994 - 2008 Fundación MSX Resource Center. MSX es una marca registrada de MSX Licensing Corporation