Board Logo
« Simple Text Based Calculator »

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


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  Notify Send Topic Print
 thread  Author  Topic: Simple Text Based Calculator  (Read 718 times)
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Simple Text Based Calculator
« Thread started on: Sep 7th, 2016, 09:36am »

text based calculator:
Code:
st1$="firstNumber  secondNumber "
st2$="Base Exponant"
st3$="AnyNumber"
op$="+ - x / rem raisedto sqrt sqr other "
while word$(op$,wc+1)<>""
wc=wc+1
wend
print "Operators List "
for j=1 to wc
print j;" ";word$(op$,j)
next
input"ChooseOperation:";c$
n$=n$+word$(op$,val(c$))+" "
if val(c$)<6  then st$=st1$:t=2
if val(c$)=6 then st$=st2$:t=2
if val(c$)>6 then st$=st3$:t=1
for i=1 to t
input "Enter ";word$(st$,i);": ";k$
n$=n$+k$+" "
next
select case val(c$)
case 1,2,3,4,5,6
print word$(n$,2);word$(n$,1);word$(n$,3)
print"= ";
select case c$
case "1"
print val(word$(n$,2))+val(word$(n$,3))
case "2"
print val(word$(n$,2))-val(word$(n$,3))
case "3"
print val(word$(n$,2))*val(word$(n$,3))
case "4"
if val(word$(n$,3))<>0 then
print val(word$(n$,2))/val(word$(n$,3))
else
print "infinty or not defined"
end if
case "5"
print val(word$(n$,2))mod val(word$(n$,3))
case "6"
print val(word$(n$,2))^val(word$(n$,3))
end select
case 7,8,9
print word$(n$,1);" of ";word$(n$,2)
print"= ";
select case c$
case "7"
print sqr(val(k$))
case "8"
print val(k$)*val(k$)
case "9"
print"DoYourSelf"
end select
case else
print"invalid option"
end select

 
User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: Simple Text Based Calculator
« Reply #1 on: Sep 7th, 2016, 4:17pm »

Interesting.
Adding indents really adds readability (I did it just to understand what's going on):
Code:
st1$="firstNumber  secondNumber "
st2$="Base Exponant"
st3$="AnyNumber"
op$="+ - x / rem raisedto sqrt sqr other "

while word$(op$,wc+1)<>""
    wc=wc+1
wend
print "Operators List "
for j=1 to wc
    print j;" ";word$(op$,j)
next

input"ChooseOperation:";c$
n$=n$+word$(op$,val(c$))+" "
if val(c$)<6  then st$=st1$:t=2
if val(c$)=6 then st$=st2$:t=2
if val(c$)>6 then st$=st3$:t=1

for i=1 to t
    input "Enter ";word$(st$,i);": ";k$
    n$=n$+k$+" "
next

select case val(c$)
case 1,2,3,4,5,6
    print word$(n$,2);word$(n$,1);word$(n$,3)
    print"= ";
    select case c$
    case "1"
        print val(word$(n$,2))+val(word$(n$,3))
    case "2"
        print val(word$(n$,2))-val(word$(n$,3))
    case "3"
        print val(word$(n$,2))*val(word$(n$,3))
    case "4"
        if val(word$(n$,3))<>0 then
            print val(word$(n$,2))/val(word$(n$,3))
        else
            print "infinty or not defined"
        end if
    case "5"
        print val(word$(n$,2))mod val(word$(n$,3))
    case "6"
        print val(word$(n$,2))^val(word$(n$,3))
    end select
case 7,8,9
    print word$(n$,1);" of ";word$(n$,2)
    print"= ";
    select case c$
    case "7"
        print sqr(val(k$))
    case "8"
        print val(k$)*val(k$)
    case "9"
        print"DoYourSelf"
    end select
case else
    print"invalid option"
end select
 


Now, first two loops could be combined in one
Code:
print "Operators List "
while word$(op$,wc+1)<>""
    wc=wc+1
    print wc;" ";word$(op$,wc)
wend
 

It is clear that we use
val(word$(n$,2)) for operand 1 and
val(word$(n$,3)) for operand 2
So we can made these variables (op1 and op2) and substitute.
Also, in case of only operand, op1 still be valid so we can use it for single argument functions (7,8).
So after replacing I got this:
Code:
st1$="firstNumber  secondNumber "
st2$="Base Exponant"
st3$="AnyNumber"
op$="+ - x / rem raisedto sqrt sqr other "

print "Operators List "
while word$(op$,wc+1)<>""
    wc=wc+1
    print wc;" ";word$(op$,wc)
wend

input"ChooseOperation:";c$
n$=n$+word$(op$,val(c$))+" "
if val(c$)<6  then st$=st1$:t=2
if val(c$)=6 then st$=st2$:t=2
if val(c$)>6 then st$=st3$:t=1

for i=1 to t
    input "Enter ";word$(st$,i);": ";k$
    n$=n$+k$+" "
next

select case val(c$)
op1=val(word$(n$,2))
op2=val(word$(n$,3))
case 1,2,3,4,5,6
    print word$(n$,2);word$(n$,1);word$(n$,3)
    print"= ";
    select case c$
    case "1"
        print op1+op2
    case "2"
        print op1-op2
    case "3"
        print op1*op2
    case "4"
        if op2<>0 then
            print op1/op2
        else
            print "infinty or not defined"
        end if
    case "5"
        print op1 mod op2
    case "6"
        print op1^op2
    end select
case 7,8,9
    print word$(n$,1);" of ";word$(n$,2)
    print"= ";
    select case c$
    case "7"
        print sqr(op1)
    case "8"
        print op1*op1
    case "9"
        print"DoYourSelf"
    end select
case else
    print"invalid option"
end select
 

Same code, just a bit easier to read ;)

BTW if we check for x/0 , could as well check for sqr(negative) and negative^fractional - both of them error out.
« Last Edit: Sep 7th, 2016, 4:19pm by tsh73 » 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)
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Re: Simple Text Based Calculator
« Reply #2 on: Sep 7th, 2016, 11:16pm »

thank you tsh for your remarks and refinement,indeed it was a bit clumsy earlier.
User IP Logged

zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Re: Simple Text Based Calculator
« Reply #3 on: Sep 7th, 2016, 11:52pm »

on Sep 7th, 2016, 4:17pm, tsh73 wrote:
if we check for x/0 , could as well check for sqr(negative) and negative^fractional - both of them error out.

Here is the modified version basing on the remarks:
Code:
st1$="firstNumber  secondNumber "
st2$="Base Exponant"
st3$="AnyNumber"
op$="+ - x / rem raisedto sqrt sqr other "

print "Operators List "
while word$(op$,wc+1)<>""
    wc=wc+1
    print wc;" ";word$(op$,wc)
wend

input"ChooseOperation:";c$
n$=n$+word$(op$,val(c$))+" "
if val(c$)<6  then st$=st1$:t=2
if val(c$)=6 then st$=st2$:t=2
if val(c$)>6 then st$=st3$:t=1

for i=1 to t
    input "Enter ";word$(st$,i);": ";k$
    n$=n$+k$+" "
next

select case val(c$)
op1=val(word$(n$,2))
op2=val(word$(n$,3))
case 1,2,3,4,5,6
    print word$(n$,2);word$(n$,1);word$(n$,3)
    print"= ";
    select case c$
    case "1"
        print op1+op2
    case "2"
        print op1-op2
    case "3"
        print op1*op2
    case "4"
        if op2<>0 then
            print op1/op2
        else
            print "infinty or not defined"
        end if
    case "5"
        print op1 mod op2
    case "6"
        if op1<0 and int(op2)<>op2 then
          print "Sorry I don't know How to evaluate"
        else
          print op1^op2
        end if
    end select
case 7,8,9
    print word$(n$,1);" of ";word$(n$,2)
    print"= ";
    select case c$
    case "7"
        if op1<0 then print sqr((-1)*op1);"i" else print sqr(op1)
    case "8"
        print op1*op1
    case "9"
        print"DoYourSelf"
    end select
case else
    print"invalid option"
end select
 
User IP Logged

Pages: 1  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