Script Library: 1247 scripts
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Archive version of: supercalculator.r ... version: 6 ... crazyaxe 19-Feb-2010

Amendment note: Added help and parenthesis || Publicly available? Yes

Rebol [
	title: "Supercalculator"
	author: "Massimiliano Vessi"
	date:  17/02/2010
	email: maxint@tiscali.it
	file: %supercalculator.r
	Purpose: {"Scientific calculator in Rebol!"}
	;following data are for www.rebol.org library
	;you can find a lot of rebol script there
	library: [ 
		level: 'beginner 
		platform: 'all 
		type: [tutorial  tool] 
		domain: [vid gui  text-processing ui user-interface scientific] 
		tested-under: [windows linux] 
		support: none 
		license: [gpl] 
		see-also: none 
		] 
	version: 1.4.6
	]
;following lines are to obtain current file version
header-script: system/script/header
version: "Version: "
append version header-script/version
	

risultato2: " "

decimali: 0.01     
; We define a function to round the values to the specified digits
troncare: func [ misura2 ]
   [
   esatto: round/to misura2 decimali
   return esatto
   ]


valuta: func [frase][
	frase: to-string frase
	replace/all frase  "abs-"   "  abs  " ;it's tricky but necessary
	replace/all frase  "abs+"   "  abs  "
	replace/all frase "*"  " ) * ( "
	replace/all frase "/"   " ) / ( "
	replace/all frase "+"   " )) + (( "
	replace/all frase "-"   " )) - (( "
	replace/all frase "^^"   "  **  " ;bad change, but necessary
	replace/all frase "exp"   "  exp  "
	replace/all frase "log"   "  log-10  "
	replace/all frase "ln"   "  log-e  "
	replace/all frase "sqrt"   "  square-root  " ;bad change, but necessary
	replace/all frase  "abs"   "  abs  "
	replace/all frase "sin"   "  sine  " 
	replace/all frase "cos"   "  cosine  "
	replace/all frase "tangent"   "  tangent  "
	replace/all frase "arcs"   "  arcsine  " ;bad change, but necessary
	replace/all frase "arcc"   "  arccosine  " ;bad change, but necessary
	replace/all frase "arct"   "  arctangent  " ;bad change, but necessary
	insert frase " (( "
	append frase " )) "	
	
	;uncomment the following line to debug or to see what happen...	
	;print frase
	
	risultato: do frase 
	
	;check if user wants to round result
	if y_chk/data = true [  risultato: troncare risultato ]
	
	;print risultato
	;restore the origina string
	replace/all frase    "  **  " "^^"
	replace/all frase   "  arcsine  "  "arcs" 
	replace/all frase    "  arccosine  " "arcc"
	replace/all frase   "  arctangent  " "arct" 
	replace/all frase    "  square-root  " "sqrt"
	replace/all frase    "  abs  "  "abs"
	replace/all frase    "  sine  "  "sin"
	replace/all frase   "  cosine  " "cos" 
	replace/all frase    "  log-10  " "log"
	replace/all frase    "  log-e  " "ln"
	
	replace/all  frase " ) " "" ;remove al simple parenthesis
	replace/all  frase " ( " "" ;remove al simple parenthesis
	replace/all  frase " (( " "" ;remove al double parenthesis
	replace/all  frase " )) " "" ;remove al double parenthesis
	
	
	pretty_frase: trim/all frase
	risultato2: head risultato2
	risultato2: insert risultato2  (reform [ "^/" pretty_frase "^/-----------^/= " risultato "^/"])
	risultato2: head risultato2
	return risultato2
	]



solve_all: func [] [
	
	]

view layout [

	text "History:"
	a_field: area 255.255.100
	
	text version ;to visualize version
	return
	guide
	
	text "Write expression below:"
	b_field: field "" [ 
		if  b_field/text = "" [ b_field/text: "0"]
		a_field/text: to-string (valuta b_field/text)
		b_field/text: copy []
		b_field/text: to-string b_field/text
		show [ a_field b_field]
		]
	across
	button 20x20 "1" [ append b_field/text "1"  show b_field]
	button 20x20 "2" [ append b_field/text "2"  show b_field]
	button 20x20 "3" [ append b_field/text "3"  show b_field]
	button 20x20 "+" [ append b_field/text "+"  show b_field]
	button 20x20 "-" [ append b_field/text "-"  show b_field]
	button "sin"  40x20 [ append b_field/text "sin"  show b_field]
	button "cos" 40x20 [ append b_field/text "cos"  show b_field]
	button "tan" 40x20 [ append b_field/text "tangent"  show b_field]
	return
	button 20x20 "4" [ append b_field/text "4"  show b_field]
	button 20x20 "5" [ append b_field/text "5"  show b_field]
	button 20x20 "6" [ append b_field/text "6"  show b_field]
	button 20x20 "*" [ append b_field/text "*"  show b_field]
	button 20x20 "/" [ append b_field/text "/"  show b_field]
	button "asin"  40x20 [ append b_field/text "arcs"  show b_field]
	button "acos" 40x20 [ append b_field/text "arcc"  show b_field]
	button "atan" 40x20 [ append b_field/text "arct"  show b_field]
	return
	button 20x20 "7" [ append b_field/text "7"  show b_field]
	button 20x20 "8" [ append b_field/text "8"  show b_field]
	button 20x20 "9" [ append b_field/text "9"  show b_field]
	button 20x20 "0" [ append b_field/text "0"  show b_field]
	button 20x20 green "=" [ 
		if  b_field/text = "" [ b_field/text: "0"]
		a_field/text: to-string (valuta b_field/text)
		b_field/text: copy []
		b_field/text: to-string b_field/text
		show [ a_field b_field]
		]
	button  "log" 30x20 [ append b_field/text "log"  show b_field]	
	button  "ln" 30x20 [ append b_field/text "ln"  show b_field]	
	button  "e^^" 30x20 [ append b_field/text " exp "  show b_field]	
	button  "abs" 31x20  [ append b_field/text "abs"  show b_field]	
	return
	button 20x20 "." [ append b_field/text "."  show b_field]
	button 20x20 " ^^ " [ append b_field/text "^^"  show b_field]
	button   42x20 "SQRT" [ append b_field/text "sqrt"  show b_field]
	button 30x20 "EE" [ append b_field/text "e"  show b_field]
	button 20x20 "(" [ append b_field/text "("  show b_field]
	button 20x20 ")" [ append b_field/text ")"  show b_field]
	button red 30x20 "CC" [ b_field/text: copy []   show b_field]
	btn-help [ view/new layout [ 
			title "HELP"
			text "Welcome to Supercalculator, a Scientific calculator written in Rebol."
			text "You can use it on Windows, Liunx, Mac and whatever Rebol works!"
			text "You can write directly the formulas in the field an press ENTER or press =."
			text "You can use parethesis to write correctly the formulas."
			text "You can contact me for help: Massimiliano Vessi maxint@tiscali.it"
			text  version
			image logo.gif
			]
		
		]
	return
	text "Fixed decimal digits?"
	y_chk: check false
	;return
	text "Digits"
	cifredecimali: text "2"

	button 20x20 "+" [ decimali: decimali * 0.1
              if decimali > 0.1 [decimali: 0.1]
              cifra: to-integer cifredecimali/text
              cifra: cifra + 1
              if cifra < 1 [ cifra: 1]
              cifredecimali/text: cifra
              show cifredecimali
             ]
	button 20x20 "-" [ decimali: decimali / 0.1
              if decimali > 0.1 [decimali: 0.1]
              cifra: to-integer cifredecimali/text
              cifra: cifra - 1
              if cifra < 1 [ cifra: 1]
              cifredecimali/text: cifra
              show cifredecimali
             ]
	 
	]