My first calculator program...
Author |
Message |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Mon Nov 08, 2004 6:13 pm Post subject: My first calculator program... |
|
|
My first calculator program. Please dont laugh...
code: |
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
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
cool dude
![](http://www.taylorstrategicmarketing.com/images/king.jpg)
|
Posted: Mon Nov 08, 2004 6:27 pm Post subject: (No subject) |
|
|
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! |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Mon Nov 08, 2004 6:33 pm Post subject: (No subject) |
|
|
why do you repeat conditions?
code: |
if s = "+" or s = "+"
|
? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Paul
![](http://i12.photobucket.com/albums/a207/paulbian/DDRDuck.png)
|
Posted: Mon Nov 08, 2004 6:40 pm Post subject: (No subject) |
|
|
lol, no one will laugh at urs after they see my poor first calculator. Rofl, it sux bad. It seemed like ages ago... lol
http://www.compsci.ca/v2/viewtopic.php?t=3452 |
|
|
|
|
![](images/spacer.gif) |
myob
|
Posted: Mon Nov 08, 2004 9:27 pm Post subject: (No subject) |
|
|
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) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon Nov 08, 2004 11:01 pm Post subject: (No subject) |
|
|
As has been mentioned, there's no need to repeat conditions. Additionally, you should be using "elsif" and "else".
code: | 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 |
|
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Sun Nov 21, 2004 10:04 pm Post subject: ---- Version 2 ---- |
|
|
thanks for your help people, i edited the program a lil bit and called it Version 2. I LIKE IT!!!
code: |
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
|
|
|
|
|
|
![](images/spacer.gif) |
MattyGG
|
Posted: Tue Nov 23, 2004 11:55 am Post subject: (No subject) |
|
|
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 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Tue Nov 23, 2004 3:03 pm Post subject: (No subject) |
|
|
Quote: 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...
code: | procedure getEquation |
We're getting information about "a" and "d", so those are the parameters for the procedure. We're also getting an "operator".
code: | 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.
code: | 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.
Is redundant.
code: | 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.
code: | put "Calculator program V2"
put "Made by Tanmaya. Grade 9 Macs student"
put "Copyright 2004-05" |
Can easily be in a separate procedure.
code: | 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:
code: | %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.
code: | 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:
code: | 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. ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
Delos
![](http://www.members.shaw.ca/rfolz/delos_avatar.gif)
|
Posted: Tue Nov 23, 2004 4:40 pm Post subject: (No subject) |
|
|
Quote:
code: | put "....................................."
put "Notice: "
put "Arc ->tan, cos, sin are not supported"
put "....................................."
|
Quote:
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.
MysticVegeta, take wtd's advice, it's good stuff it is. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Wed Nov 24, 2004 3:59 am Post subject: (No subject) |
|
|
A further modification would involve using a record type to hold information about the equation:
code: | 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) |
|
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Fri Nov 26, 2004 7:28 am Post subject: (No subject) |
|
|
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. ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|