Board Logo
« FreeCalc - Expression evaluator - EVAL() for JB »

Welcome Guest. Please Login or Register.
Jan 16th, 2018, 3:36pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 sticky  Author  Topic: FreeCalc - Expression evaluator - EVAL() for JB  (Read 580 times)
Balajiganapathi
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 11
xx FreeCalc - Expression evaluator - EVAL() for JB
« Thread started on: Jun 21st, 2008, 1:09pm »

Hi! FreeCalc 1.0 is an expression evaluator. It can store values in variables.

EDIT: The latest release (FreeCalc 2.0) is available. Just scroll down.
« Last Edit: Oct 18th, 2010, 03:47am by Stefan Pendl » User IP Logged

<<Balajiganapathi>>
zlatkoJB
Junior Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 109
xx Re: FreeCalc - Expression evaluator
« Reply #1 on: Jun 21st, 2008, 2:54pm »

Good job,balaji wink
User IP Logged

mylifedrive
Full Member
ImageImageImageImage


member is offline

Avatar

Encountering Errors


Homepage PM

Gender: Male
Posts: 179
xx Re: FreeCalc - Expression evaluator
« Reply #2 on: Jun 21st, 2008, 3:16pm »

Wow! that's neat! I don't know if anyone has made something like this before, but I really like it! Is this all you plan to do or do you think you could add GUI later?
User IP Logged

Just Basic is the BEST FREE BASIC AROUND!
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: FreeCalc - Expression evaluator
« Reply #3 on: Jun 21st, 2008, 4:19pm »

Balaji,
really good job.
Now, if you only made it read in floating point numbers and show a way to add functions like sin(x), it probably 'll be perfect...

mylifedrive,
Quote:
I don't know if anyone has made something like this before

why, yes, this very forum, expression evaluator

Though Balaji approach much more appealing (and clean), my program works too, has decimals and functions (and does not have variables, yes).

Also where is anecdotical post - using VBS eval(), in tips
eval() function
Not expected to be of much use, though.
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
mylifedrive
Full Member
ImageImageImageImage


member is offline

Avatar

Encountering Errors


Homepage PM

Gender: Male
Posts: 179
xx Re: FreeCalc - Expression evaluator
« Reply #4 on: Jun 21st, 2008, 9:03pm »

WOW! good thing you pointed that out tsh73, I actually MISSED A THREAD!!! We can't let that happen now, could we?!! wink
« Last Edit: Jun 21st, 2008, 9:04pm by mylifedrive » User IP Logged

Just Basic is the BEST FREE BASIC AROUND!
Balajiganapathi
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 11
xx Re: FreeCalc - Expression evaluator
« Reply #5 on: Jun 23rd, 2008, 10:05am »

Thnks for the encouraging comments smiley.
I plan to add floating point numbers and pre-defined functions in the next version.
User IP Logged

<<Balajiganapathi>>
Balajiganapathi
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 11
xx Re: FreeCalc - Expression evaluator
« Reply #6 on: Aug 19th, 2008, 02:58am »

FreeCalc 2.0 can now read floating point numbers and has several predefined functions.

Code:
    global inp$, pos, char$, error$, count
    dim var$(100), value(100)

    print "FreeCalc 2.0"
    print "Welcome. Enter 'help' to view FreeCalc Help. Enter 'exit' to close FreeCalc"

[input]
    input inp$
    if inp$="" then [input]
    inp$=lower$(inp$)
    gosub [command]
    if command then [input]
    ans$=eval$()
    if error$<>"" then ans$="ERROR: ";error$
    print ans$
    goto [input]

[command]
    command=1
    select case inp$
    case "exit"
        print "Thank You for using FreeCalc"
        END
    case "help"
        print "FreeCalc Help"
        print "    FreeCalc is an expression evaluator. It supports variables and can store values."
        print "Integers, floating point & scientific notation numbers are accepted.."
        print: print "Operators(In evaluation order):"
        print "-  -> Negative"
        print "() -> Parenthesis"
        print "*/ -> Multiplication, Division"
        print "+- -> Addition, Subtraction"
        print: print "Expressions can be entered directly. Assignment statements must start with ':'"
        print "Variables names must consist of one or more alphabets. No digits are allowed."
        print: print "Examples:"
        print "?12*15"
        print "?:side=5.1e2/2"
        print "?:area=pow(side,2)"
        print: print "Predefined functions:"
        print "pi(), sin(n), cos(n), tan(n), asin(n), acos(n), atan(n), log(n), exp(n), sqrt(n), pow(n, p)"
        print: print "Commands:"
        print "about -> Displays information about FreeCalc"
        print "clear -> Clears the screen"
        print "exit  -> Exit FreeCalc"
        print "help  -> FreeCalc help"
        print "var   -> Displays currently used variables and their values"
        print "Commands and variable names are not case sensitive": print
    case "clear"
        cls
    case "var"
        print "Total ";count;" variables stored in memory"
        for i=1 to count
            print var$(i);"=";value(i)
        next i
    case "about"
        print "FreeCalc"
        print "Version 2.0"
        print "Written by Balaji": print
    case else
        command=0
    end select
    return

function eval$()
    pos=0: error$=""
    call nextChar
    if char$=":" then
        eval$=assignment$()
    else
        eval$=str$(expression())
    end if
    if pos<=len(inp$) and error$="" then error$="Unknown Command"
end function

function assignment$()
    call nextChar
    var$=getID$()
    if error$<>"" then exit function
    index=getIndex(var$)
    if match("=")=0 then exit function
    value=expression()
    value(index)=value
    assignment$=var$(index);"=";value(index)
end function

function expression()
    expression=addTerm()
    if error$<>"" then exit function
end function

function addTerm()
    ans=multTerm()
    do while instr("+-", char$)>0
        op$=char$
        call nextChar
        temp=multTerm()
        if error$<>"" then exit function
        if op$="+" then ans=ans+temp else ans=ans-temp
    loop
    addTerm=ans
end function

function multTerm()
    ans=factor()
    do while instr("*/", char$)>0
        op$=char$
        call nextChar
        temp=factor()
        if error$<>"" then exit function
        if op$="*" then
            ans=ans*temp
        else
            if temp=0 then
                error$="Division by zero"
                exit function
            end if
            ans=ans/temp
        end if
    loop
    multTerm=ans
end function

function factor()
    if isAlpha() then
        id$=getID$()
        if char$="(" then
            factor=getFunction(id$)
        else
            factor=getValue(id$)
        end if
        exit function
    end if
    select case char$
    case "("
        call nextChar
        factor=expression()
        if error$<>"" then exit function
        if match(")")=0 then exit function
    case "-"
        call nextChar
        factor=-1*factor()
    case else
        factor=getNum()
    end select
end function

function getValue(var$)
    i=getIndex(var$)
    getValue=value(i)
end function

function getIndex(var$)
    for i=1 to count
        if var$(i)=var$ then
            getIndex=i
            exit function
        end if
    next i
    count=count+1
    var$(count)=var$
    getIndex=count
end function

function getID$()
    do while isAlpha()
        getID$=getID$;char$
        call nextChar
    loop
    if getID$="" then error$="Expected variable"
end function

function getNum()
    do while isDigit()
        n$=n$;char$
        call nextChar
    loop
    if n$="" then error$="Expected Number"
    if char$="." then
        n$=n$;"."
        call nextChar
        do while isDigit()
            n$=n$;char$
            call nextChar
        loop
    end if
    if char$="e" then
        n$=n$;"e"
        call nextChar
        do while isDigit()
            n$=n$;char$
            call nextChar
        loop
    end if
    getNum=val(n$)
end function

function getFunction(fname$)
    if match("(")=0 then exit function
    if instr(" pi ", " ";fname$;" ") then call param0
    if instr(" sin cos tan asin acos atan sqrt exp log ", " ";fname$;" ") then call param1 n
    if instr(" pow ", " ";fname$;" ") then call param2 n1, n2
    if error$<>"" then exit function
    select case fname$
    case "pi"
        ans=asn(1)*2
    case "sin"
        ans=sin(n)
    case "cos"
        ans=cos(n)
    case "tan"
        ans=tan(n)
    case "asin"
        ans=asn(n)
    case "acos"
        ans=acs(n)
    case "atan"
        ans=atn(n)
    case "sqrt"
        ans=sqr(n)
    case "exp"
        ans=exp(n)
    case "log"
        ans=log(n)
    case "pow"
        ans=n1 ^ n2
    case else
        error$="Unknown function"
    end select
    getFunction=ans
end function

sub param0
    if match(")")=0 then exit sub
end sub

sub param1 BYREF num
    num=expression()
    if error$<>"" then exit sub
    if match(")")=0 then exit sub
end sub

sub param2 BYREF num1, BYREF num2
    num1=expression()
    if error$<>"" then exit sub
    if match(",")=0 then exit sub
    num2=expression()
    if error$<>"" then exit sub
    if match(")")=0 then exit sub
end sub

function isDigit()
    isDigit=(char$>="0" and char$<="9")
end function

function isAlpha()
    isAlpha=(char$>="a" and char$<="z")
end function

function match(c$)
    if char$=c$ then
        match=1
        call nextChar
    else
        error$="Expected ";c$
    end if
end function

sub nextChar
    pos=pos+1
    char$=mid$(inp$, pos, 1)
end sub
 
User IP Logged

<<Balajiganapathi>>
Stefan Pendl
Administrator
ImageImageImageImageImage


member is offline

Avatar

Let's talk JB ...


Homepage PM

Gender: Male
Posts: 3712
xx Re: FreeCalc - Expression evaluator
« Reply #7 on: Jul 5th, 2009, 04:04am »

The following changes will allow entering values in degrees instead of only radians.
Code:
    '---code removed, see code below
 
« Last Edit: Jul 5th, 2009, 06:15am by Stefan Pendl » User IP Logged

Stefan - Homepage

Remember to read the forum rules board wink

Just BASIC 1.01, Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
Stefan Pendl
Administrator
ImageImageImageImageImage


member is offline

Avatar

Let's talk JB ...


Homepage PM

Gender: Male
Posts: 3712
xx Re: FreeCalc - Expression evaluator
« Reply #8 on: Jul 5th, 2009, 06:20am »

There have been some things overseen, so I had to make some additional changes.
This one uses no additional function to convert between radians and degrees.
I have included some additions to the error messages, so the user might get a hint where to look at.
Code:
    global inp$, pos, char$, error$, count, useDegrees
    dim var$(100), value(100)

    ' default for angular units is radians
    useDegrees = 0

    print "FreeCalc 2.0"
    print "Welcome. Enter 'help' to view FreeCalc Help. Enter 'exit' to close FreeCalc"
    print
    print "Trigonometric Functions use Radians!"
    print

[input]
    input inp$
    if inp$="" then [input]
    inp$=lower$(inp$)
    gosub [command]
    if command then [input]
    ans$=JBeval$()
    if error$<>"" then print:ans$="ERROR: ";error$
    print ans$
    if error$<>"" then print
    goto [input]

'...

        print: print "Commands:"
        print "about   -> Displays information about FreeCalc"
        print "clear   -> Clears the screen"
        print "exit    -> Exit FreeCalc"
        print "help    -> FreeCalc help"
        print "var     -> Displays currently used variables and their values"
        print "degrees -> Values for trigonometric functions are degrees"
        print "radians -> Values for trigonometric functions are radians (the default)"
        print "Commands and variable names are not case sensitive": print

        case "clear"
        cls

        case "var"
        print "Total ";count;" variables stored in memory"
        for i=1 to count
            print var$(i);"=";value(i)
        next i

        case "degrees"
        print:print "Trigonometric Functions use Degrees!":print
        useDegrees = 1

        case "radians"
        print:print "Trigonometric Functions use Radians!":print
        useDegrees = 0

        case "about"
        print "FreeCalc"
        print "Version 2.0"
        print "Written by Balaji": print

        case else
        command=0
    end select
    return

function JBeval$()
    pos=0: error$=""
    call nextChar
    if char$=":" then
        JBeval$=assignment$()
    else
        JBeval$=str$(expression())
    end if
    if pos<=len(inp$) and error$="" then error$="Unknown Command: "; inp$
end function

'...

function getFunction(fname$)
    if match("(")=0 then exit function
    if instr(" pi ", " ";fname$;" ") then call param0
    if instr(" sin cos tan asin acos atan sqrt exp log ", " ";fname$;" ") then call param1 n
    if instr(" sin cos tan ", " ";fname$;" ") and (useDegrees = 1) then n = asn(1) / 90 * n
    if instr(" pow ", " ";fname$;" ") then call param2 n1, n2

'...

        case "pow"
        ans=n1 ^ n2

        case else
        error$="Unknown function: "; fname$
    end select
    if instr(" asin acos atan ", " ";fname$;" ") and (useDegrees = 1) then ans = 90 / asn(1) * ans
    getFunction=ans
end function

'...

 
User IP Logged

Stefan - Homepage

Remember to read the forum rules board wink

Just BASIC 1.01, Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: FreeCalc - Expression evaluator - EVAL() for J
« Reply #9 on: Mar 13th, 2011, 10:52am »

See
JB Programming Discussions\formulas as strings::Reply# 15
for a version which supports BASIC-like 2^3 (instead of C-like pow(2,3)). Many thanks to Stefan Pendl. wink
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
opal bow
Full Member
ImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 358
xx Re: FreeCalc - Expression evaluator - EVAL() for J
« Reply #10 on: Apr 6th, 2011, 05:34am »

just a quick question about this program, with negative numbers.

can it handle having a number put in like -60 in a formula for example 67*-39

or does it need to be entered like 67*(0-39)?
User IP Logged

Dave Smith

Systems for Engineers
http://www.systemsforengineers.co.uk
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: FreeCalc - Expression evaluator - EVAL() for J
« Reply #11 on: Apr 6th, 2011, 06:11am »

Really,
checking it yourself is faster then post question (and wait for answer).
I just checked - it works with 67*-39 OK

User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
opal bow
Full Member
ImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 358
xx Re: FreeCalc - Expression evaluator - EVAL() for J
« Reply #12 on: Apr 6th, 2011, 06:44am »

cheers i would have checked it, but no access to JB today untill probably about 8 or 9pm tonight, and by that point my sieve like brain would have forgot cheesy
User IP Logged

Dave Smith

Systems for Engineers
http://www.systemsforengineers.co.uk
opal bow
Full Member
ImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 358
xx Re: FreeCalc - Expression evaluator - EVAL() for J
« Reply #13 on: Apr 11th, 2011, 07:33am »

im not very good with functions and sub routines so i was wondering where would the changes need to be made to add new functions to this program?
User IP Logged

Dave Smith

Systems for Engineers
http://www.systemsforengineers.co.uk
opal bow
Full Member
ImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 358
xx Re: FreeCalc - Expression evaluator - EVAL() for J
« Reply #14 on: Apr 13th, 2011, 03:24am »

Something I have just noticed about this is that if you input:

2e3 you get a value of 2000 which is how it should be

but if you input 2e-3 you recieve a value of -1 when it should be 0.002

unless i am mistaken?


EDIT: with a bit of playing I have found had to make the "e" work with negative numbers,

Code:
if char$="e" then
n$=n$;"e"
call nextChar
do while IsDigit() 


needs to be changed to:

Code:
if char$="e" then
n$=n$;"e"
call nextChar
do while (char$>="0" and char$<="9") or char$="-" 
« Last Edit: Apr 13th, 2011, 04:07am by opal bow » User IP Logged

Dave Smith

Systems for Engineers
http://www.systemsforengineers.co.uk
Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls