
-----------------------------------
MysticVegeta
Mon Nov 08, 2004 6:13 pm

My first calculator program...
-----------------------------------
My first calculator program. Please dont laugh...


View.Set ("graphics:300;400,position:middle;middle,nobuttonbar")
Window.Set (1, "Calculator Program")
put "Calculator program"
put "Made by Tanmaya. Grade 9 Macs student"
put "Copyright 2004-05"
loop
    put ""
    put "Enter the first number:"
    var a : int
    get a
    put "Enter the math function (+, -, *, /)"
    var s : string
    get s
    put "Enter the second number"
    var d : int
    get d
    put ""
    if s = "+" or s = "+"
            then
        put "=", a + d
    end if
    if s = "-" or s = "-"
            then
        put "=", a - d
    end if
    if s = "*" or s = "*"
            then
        put "=", a * d
    end if
    if s = "/" or s = "/"
            then
        put "=", a div d
    end if
end loop


-----------------------------------
cool dude
Mon Nov 08, 2004 6:27 pm


-----------------------------------
this is the wrong place to post something you did. this section is only for help. you should post  in the turing submissions or source code. anyways nice calculator. a suggestion would be by error proofing it so if the user enters a letter instead of a number the calcuator could say error or say numbers only. good job!

-----------------------------------
Tony
Mon Nov 08, 2004 6:33 pm


-----------------------------------
why do you repeat conditions?

if s = "+" or s = "+" 

?

-----------------------------------
Paul
Mon Nov 08, 2004 6:40 pm


-----------------------------------
lol, no one will laugh at urs after they see my poor first calculator. Rofl, it sux bad. It seemed like ages ago... lol  :lol: 
http://www.compsci.ca/v2/viewtopic.php?t=3452

-----------------------------------
myob
Mon Nov 08, 2004 9:27 pm


-----------------------------------
would be better if u put in error trap for it. soon you will learn enough about error traps (u gonna be da master of it in grade 12)

-----------------------------------
wtd
Mon Nov 08, 2004 11:01 pm


-----------------------------------
As has been mentioned, there's no need to repeat conditions.  Additionally, you should be using "elsif" and "else".

if s = "+" then
   put "=", a + d
elsif s = "-" then
   put "=", a - d
elsif s = "*" then
   put "=", a * d
elsif s = "/" then
   put "=", a div d
else
   put "Bad operator."
end if 

-----------------------------------
MysticVegeta
Sun Nov 21, 2004 10:04 pm

---- Version 2 ----
-----------------------------------
thanks for your help people, i edited the program a lil bit and called it Version 2. I LIKE IT!!! 


View.Set ("graphics:300;400,position:middle;middle,nobuttonbar")
Window.Set (1, "Calculator Program")
put "Calculator program V2"
put "Made by Tanmaya. Grade 9 Macs student"
put "Copyright 2004-05"
loop
    put ""
    put "Enter the first number:"
    var a : int
    get a
    put ""
    put "Enter the math function (+, -, *, /"
    put "s for sin, c for cos, t for tan"
    var s : string
    get s
    put ""
    put "Enter the second number"
    var d : int
    get d
    put ""
    put "....................................."

    %Basic Functions

    if s = "+" or s = "+"
            then
        put "=", a + d
    elsif
            s = "-" or s = "-"
            then
        put "=", a - d
    elsif
            s = "*" or s = "*"
            then
        put "=", a * d
    elsif
            s = "/" or s = "/"
            then
        put "=", a div d
    elsif

        %Trignometric Functions

            s = "t"
            then
        put "Tan ", a, " =", (sind (a)) / (cosd (a))
        put "Tan ", d, " =", (sind (d)) / (cosd (d))
    elsif
            s = "s"
            then
        put "Sin ", a, " =", sind (a)
        put "Sin ", d, " =", sind (d)
    elsif
            s = "c"
            then
        put "Cos ", a, " =", cosd (a)
        put "Cos ", d, " =", cosd (d)
    else
        put "Bad Operator"
    end if
    put "....................................."

    %Notice

    put ""
    put "....................................."
    put "Notice: "
    put "Arc ->tan, cos, sin are not supported"
    put "....................................."

    %Exit

    put ""
    put "....................................."
    put "Do you want to clear the screen? y/n"
    var ask : string
    get ask
    put "....................................."
    if ask = "y"
            then
        cls
    elsif ask = "n"
            then
        put ""
    end if

end loop


-----------------------------------
MattyGG
Tue Nov 23, 2004 11:55 am


-----------------------------------
It's better than the first one...but there should also be a way to quit the program when you want to...otherwise great job

-----------------------------------
wtd
Tue Nov 23, 2004 3:03 pm


-----------------------------------
    put ""
    put "Enter the first number:"
    var a : int
    get a
    put ""
    put "Enter the math function (+, -, *, /"
    put "s for sin, c for cos, t for tan"
    var s : string
    get s
    put ""
    put "Enter the second number"
    var d : int
    get d
    put ""
    put "....................................."

Now, you have this, but how about we box it up in a procedure?

We're doing output, so it should be a procedure.  We're "getting" an "equation", so we'll call it...

procedure getEquation

We're getting information about "a" and "d", so those are the parameters for the procedure.  We're also getting an "operator".

procedure getEquation (var a, d : int, var op : string)

So, with those as parameters, we no longer need to declare them inside the procedure.  Then of course, we can end the procedure.

procedure getEquation (var a, d : int, var op : string)
    put ""
    put "Enter the first number:"
    get a
    put ""
    put "Enter the math function (+, -, *, /"
    put "s for sin, c for cos, t for tan"
    get op
    put ""
    put "Enter the second number"
    get d
    put ""
    put "....................................."
end getEquation

So, we can now rewrite your program slightly.  oh, and we'll simplify your conditionals. 

s = "+" or s = "+"

Is redundant.

procedure getEquation (var a, d : int, var op : string)
    put ""
    put "Enter the first number:"
    get a
    put ""
    put "Enter the math function (+, -, *, /"
    put "s for sin, c for cos, t for tan"
    get op
    put ""
    put "Enter the second number"
    get d
    put ""
    put "....................................."
end getEquation

View.Set ("graphics:300;400,position:middle;middle,nobuttonbar")
Window.Set (1, "Calculator Program")
put "Calculator program V2"
put "Made by Tanmaya. Grade 9 Macs student"
put "Copyright 2004-05"
loop
    var a, d : int
    var op : string
    getEquation (a, d, op)

    %Basic Functions
    if s = "+" then
        put "=", a + d
    elsif s = "-" then
        put "=", a - d
    elsif s = "*" then
        put "=", a * d
    elsif s = "/" then
        put "=", a div d
    %Trignometric Functions
    elsif s = "t" then
        put "Tan ", a, " =", (sind (a)) / (cosd (a))
        put "Tan ", d, " =", (sind (d)) / (cosd (d))
    elsif s = "s" then
        put "Sin ", a, " =", sind (a)
        put "Sin ", d, " =", sind (d)
    elsif s = "c" then
        put "Cos ", a, " =", cosd (a)
        put "Cos ", d, " =", cosd (d)
    else
        put "Bad Operator"
    end if
    put "....................................."

    %Notice

    put ""
    put "....................................."
    put "Notice: "
    put "Arc ->tan, cos, sin are not supported"
    put "....................................."

    %Exit

    put ""
    put "....................................."
    put "Do you want to clear the screen? y/n"
    var ask : string
    get ask
    put "....................................."
    if ask = "y"
            then
        cls
    elsif ask = "n"
            then
        put ""
    end if

end loop
    
No, let's factor out some more code.

put "Calculator program V2"
put "Made by Tanmaya. Grade 9 Macs student"
put "Copyright 2004-05"

Can easily be in a separate procedure.

procedure showCredits
    put "Calculator program V2"
    put "Made by Tanmaya. Grade 9 Macs student"
    put "Copyright 2004-05"
end showCredits

And we can factor out:

    %Basic Functions
    if s = "+" then
        put "=", a + d
    elsif s = "-" then
        put "=", a - d
    elsif s = "*" then
        put "=", a * d
    elsif s = "/" then
        put "=", a div d
    %Trignometric Functions
    elsif s = "t" then
        put "Tan ", a, " =", (sind (a)) / (cosd (a))
        put "Tan ", d, " =", (sind (d)) / (cosd (d))
    elsif s = "s" then
        put "Sin ", a, " =", sind (a)
        put "Sin ", d, " =", sind (d)
    elsif s = "c" then
        put "Cos ", a, " =", cosd (a)
        put "Cos ", d, " =", cosd (d)
    else
        put "Bad Operator"
    end if
    put "....................................."
    
Into "evaluateEquation".  Oh, and we can use a case statement.

procedure evaluateEquation (a, d : int, op : string)
    case op of
        %Basic Functions
        label "+":
            put "=", a + d
        label "-":
            put "=", a - d
        label "*":
            put "=", a * d
        label "/":
            put "=", a div d
        %Trignometric Functions 
        label "t":
            put "Tan ", a, " =", (sind (a)) / (cosd (a))
            put "Tan ", d, " =", (sind (d)) / (cosd (d))
        label "s":
            put "Sin ", a, " =", sind (a)
            put "Sin ", d, " =", sind (d)
        label "c":
            put "Cos ", a, " =", cosd (a)
            put "Cos ", d, " =", cosd (d)
        label:
            put "Bad Operator"
    end case
    
    put "....................................."
end evaluateEquation

So, now the code looks like:

procedure getEquation (var a, d : int, var op : string)
    put ""
    put "Enter the first number:"
    get a
    put ""
    put "Enter the math function (+, -, *, /"
    put "s for sin, c for cos, t for tan"
    get op
    put ""
    put "Enter the second number"
    get d
    put ""
    put "....................................."
end getEquation

procedure evaluateEquation (a, d : int, op : string)
    case op of
        %Basic Functions
        label "+":
            put "=", a + d
        label "-":
            put "=", a - d
        label "*":
            put "=", a * d
        label "/":
            put "=", a div d
        %Trignometric Functions 
        label "t":
            put "Tan ", a, " =", (sind (a)) / (cosd (a))
            put "Tan ", d, " =", (sind (d)) / (cosd (d))
        label "s":
            put "Sin ", a, " =", sind (a)
            put "Sin ", d, " =", sind (d)
        label "c":
            put "Cos ", a, " =", cosd (a)
            put "Cos ", d, " =", cosd (d)
        label:
            put "Bad Operator"
    end case
    
    put "....................................."
end evaluateEquation

procedure showCredits
    put "Calculator program V2"
    put "Made by Tanmaya. Grade 9 Macs student"
    put "Copyright 2004-05"
end showCredits

View.Set ("graphics:300;400,position:middle;middle,nobuttonbar")
Window.Set (1, "Calculator Program")
showCredits
loop
    var a, d : int
    var op : string
    
    getEquation (a, d, op)

    evaluateEquation (a, d, op)

    %Notice

    put ""
    put "....................................."
    put "Notice: "
    put "Arc ->tan, cos, sin are not supported"
    put "....................................."

    %Exit

    put ""
    put "....................................."
    put "Do you want to clear the screen? y/n"
    var ask : string
    get ask
    put "....................................."
    if ask = "y"
            then
        cls
    elsif ask = "n"
            then
        put ""
    end if

end loop

 leave it to your imagination to further factor this code down.  :)

-----------------------------------
Delos
Tue Nov 23, 2004 4:40 pm


-----------------------------------

 put "....................................." 
    put "Notice: " 
    put "Arc ->tan, cos, sin are not supported" 
    put "....................................." 





Syntax   arccos (r : real) : real
 
Description   The arccos function is used to find the arc cosine of a value. The result is given in radians. For example, arccos (0.5) is p / 3. 

Syntax   arcsin (r : real) : real
 
Description   The arcsin function is used to find the arc sine of a value. The result is given in radians. For example, arcsin (0.5) is p / 6. 

Syntax   arctan (r : real) : real
 
Description   The arctan function is used to find the arc tangent of a value. The result is given in radians. For example, arctan (1) is p / 4. 


:wink:

MysticVegeta, take wtd's advice, it's good stuff it is.

-----------------------------------
wtd
Wed Nov 24, 2004 3:59 am


-----------------------------------
A further modification would involve using a record type to hold information about the equation:

type Equation :
   record
      a, d : integer := 0
      op : string := "+"
   end record

procedure getEquation (var eq : Equation) 
    put ""
    put "Enter the first number:"
    get eq.a
    put ""
    put "Enter the math function (+, -, *, /"
    put "s for sin, c for cos, t for tan"
    get eq.op
    put ""
    put "Enter the second number"
    get eq.d
    put ""
    put "....................................."
end getEquation

procedure evaluateEquation (eq : Equation)
    case eq.op of
        %Basic Functions
        label "+":
            put "=", eq.a + eq.d
        label "-":
            put "=", eq.a - eq.d
        label "*":
            put "=", eq.a * eq.d
        label "/":
            put "=", eq.a div eq.d
        %Trignometric Functions
        label "t":
            put "Tan ", eq.a, " =", (sind (eq.a)) / (cosd (eq.a))
            put "Tan ", eq.d, " =", (sind (eq.d)) / (cosd (eq.d))
        label "s":
            put "Sin ", eq.a, " =", sind (eq.a)
            put "Sin ", eq.d, " =", sind (eq.d)
        label "c":
            put "Cos ", eq.a, " =", cosd (eq.a)
            put "Cos ", eq.d, " =", cosd (eq.d)
        label:
            put "Bad Operator"
    end case
   
    put "....................................."
end evaluateEquation

var eq : Equation

getEquation (eq)
evaluateEquation (eq)

-----------------------------------
MysticVegeta
Fri Nov 26, 2004 7:28 am


-----------------------------------
Thanks wtd... i am not upto record yet, i think i will shorten it using the code you posted above. And oh yea, my turing version is the old one, so the arcin and arctan functions are not supported. thanks for your advice. :)
