Quadratic equation in Oz
Example for versions
Mozart 1.4.0
Oz is a strongly typed language, so all type conversions have to be done explicitly. Coefficients are converted in two steps (first to int, next to float) because converting string “1” into a float directly causes an error (“1.” converts into float just fine). ~
denotes unary minus.
functor
import
Application System Open
define
local
A B C D
class TextFile from Open.file Open.text end
StdIn = {New TextFile init(name:stdin)}
in
{System.showInfo "A = "}
A = {Int.toFloat {String.toInt {StdIn getS($)}}}
if A==0 then
{System.showInfo "Not a quadratic equation."}
{Application.exit 0}
end
{System.showInfo "B = "}
B = {Int.toFloat {String.toInt {StdIn getS($)}}}
{System.showInfo "C = "}
C = {Int.toFloat {String.toInt {StdIn getS($)}}}
D = B*B - 4.0*A*C
if D==0.0 then
{System.showInfo "x = "#{Float.toString ~0.5*B/A}}
{Application.exit 0}
end
if D>0.0 then
{System.showInfo "x1 = "#{Float.toString ~0.5*(B-{Sqrt D})/A}}
{System.showInfo "x2 = "#{Float.toString ~0.5*(B+{Sqrt D})/A}}
else
{System.showInfo "x1 = ("#{Float.toString ~0.5*B/A}#","#{Float.toString 0.5*{Sqrt ~D}/A}#")"}
{System.showInfo "x2 = ("#{Float.toString ~0.5*B/A}#","#{Float.toString ~0.5*{Sqrt ~D}/A}#")"}
end
{Application.exit 0}
end
end
Comments
]]>blog comments powered by Disqus
]]>