1. Parsing calculator
- Posted by Fendaril Sep 18, 2009
- 1244 views
Hey guys. I worked on a little project that outputs the value of two terms with a specified operator. It parses so you can enter in '1+1,29873/2,467384*2 and etc....'. If anyone is interested try it out.
include get.e include std/convert.e constant TRUE=1 constant FALSE=0 constant ADDITION=2 constant SUBTRACTION=3 constant MULTIPLY=4 constant DIVIDE=5 atom found=0 sequence sFirstPart="" sequence sSecondPart="" atom operationType=0 procedure add(atom SymbolLocation,atom operation) for i=1 to SymbolLocation-1 do sFirstPart=append(sFirstPart,equation[i]) end for for i=SymbolLocation+1 to length(equation) do sSecondPart=append(sSecondPart,equation[i]) end for if operation=ADDITION then ?to_number(sFirstPart)+to_number(sSecondPart) elsif operation=SUBTRACTION then ?to_number(sFirstPart)-to_number(sSecondPart) elsif operation=MULTIPLY then ?to_number(sFirstPart)*to_number(sSecondPart) elsif operation=DIVIDE then ?to_number(sFirstPart)/to_number(sSecondPart) else puts(1,"ERROR") end if end procedure sequence equation=prompt_string("Enter your equation: ") for i=1 to length(equation)-1 do if equation[i]='+' then found=TRUE operationType=ADDITION add(i,operationType) elsif equation[i]='-' then found=TRUE operationType=SUBTRACTION add(i,operationType) elsif equation[i]='*' then found=TRUE operationType=MULTIPLY add(i,operationType) elsif equation[i]='/' then found=TRUE operationType=DIVIDE add(i,operationType) end if end for if found=FALSE then --person tried to be a wise guy >.> puts(1,"Invalid equation\n") end if