Simple Fraction Calculator
Hi i am trying to make a simple fraction calculator and im new to programming, i didnt quite understand all of what was on the internet and so far have this. but it dont work
here is the whole page of code its quite short and below iv highlighted the code that dont work.
Code:
<html>
<head>
<title></title>
</head>
<body>
<center>
<p>Fraction A...............................Fraction B................................Answer</p>
<input id="txtnum1" type="text" /><input id="txtnum2" type="text" /><input id="txtnum3" type="text" disabled="disabled" /></br>
<input id="txtden1" type="text" /><input id="txtden2" type="text" /><input id="txtden3" type="text" disabled="disabled" /></br>
<input id="btncalc" type="button" value="Calculate" /><input id="btnsimp" type="button" value="simplify" />
</center>
</body>
</html>
<script language="vbscript">
dim num as integer
dim den as integer
Dim GCD As Integer = GCF(num, den)
sub btncalc_onclick()
if txtden1.value = txtden2.value then
txtden3.value = (cint(txtden1.value) + cint(txtden2.value)) / 2
txtnum3.value = cint(txtnum1.value) + cint(txtnum2.value)
else
txtnum3.value = cint(txtnum1.value * txtden2.value) + cint(txtnum2.value * txtden1.value)
txtden3.value = txtden1.value * txtden2.value
end if
end sub
sub btnsimp_onclick()
num = txtnum3.value
den = txtden3.value
txtnum3.value = txtnum3.value / GCD
txtden3.value = txtden3.value / GCD
end sub
Private Function GCF(ByVal x As Integer, ByVal y As Integer) As Integer
x = Math.Abs(x)
y = math.Abs(y)
Dim z As Integer
Do
z = x Mod y
If z = 0 Then Return y
x = y
y = z
Loop
end function
</script>
the code that dont work is this which is suppose to take a fraction and reduce/simplify it
Code:
sub btnsimp_onclick()
num = txtnum3.value
den = txtden3.value
txtnum3.value = txtnum3.value / GCD
txtden3.value = txtden3.value / GCD
end sub
Private Function GCF(ByVal x As Integer, ByVal y As Integer) As Integer
x = Math.Abs(x)
y = math.Abs(y)
Dim z As Integer
Do
z = x Mod y
If z = 0 Then Return y
x = y
y = z
Loop
end function
I cant work out why it dont work though.
Re: Simple Fraction Calculator
Edit: you are writting vb.net code dim GCF as integer = GCD(x,y)
Re: Simple Fraction Calculator
try this
Code:
<html>
<head>
<title></title>
</head>
<body>
<center>
<p>Fraction A...............................Fraction B................................Answer</p>
<input id="txtnum1" type="text" /><input id="txtnum2" type="text" /><input id="txtnum3" type="text" disabled="disabled" /></br>
<input id="txtden1" type="text" /><input id="txtden2" type="text" /><input id="txtden3" type="text" disabled="disabled" /></br>
<input id="btncalc" type="button" value="Calculate" /><input id="btnsimp" type="button" value="simplify" />
</center>
</body>
</html>
<script language="vbscript">
Dim num
Dim den
Sub btncalc_onclick()
If txtden1.Value = txtden2.Value Then
txtden3.Value = (CInt(txtden1.Value) + CInt(txtden2.Value)) / 2
txtnum3.Value = CInt(txtnum1.Value) + CInt(txtnum2.Value)
Else
txtnum3.Value = CInt(txtnum1.Value * txtden2.Value) + CInt(txtnum2.Value * txtden1.Value)
txtden3.Value = txtden1.Value * txtden2.Value
End If
End Sub
Sub btnsimp_onclick()
num = txtnum3.Value
den = txtden3.Value
txtnum3.Value = txtnum3.Value / GCD(num, den)
txtden3.Value = txtden3.Value / GCD(num, den)
End Sub
Function GCD(x, y)
x = Math.Abs(x)
y = Math.Abs(y)
Dim z
Do
z = x Mod y
If z = 0 Then Return Y
x = y
y = z
Loop
End Function
I am not sure if that will even work... what does return mean? if z = 0 you want z to be y? if so change to this
Code:
If z = 0 Then
z = y
End If
Re: Simple Fraction Calculator
In vbscript a Function returns a value by assigning it to the Function Name.
Your Function looks more like java script than vbscript.
try
Code:
Function GCD(x, y)
Dim z
x = Abs(x)
y = Abs(y)
Do
z = x Mod y
If z <> 0 Then
x = y
y = z
Else
GCD = y
End If
Loop Until GCD <> 0
End Function
Re: Simple Fraction Calculator
Is this supposed to be ASP.NET or what?
Re: Simple Fraction Calculator
this works perfect
Code:
Function GCD(x, y)
Dim z
x = Abs(x)
y = Abs(y)
Do
z = x Mod y
If z <> 0 Then
x = y
y = z
Else
GCD = y
End If
Loop Until GCD <> 0
End Function
thank you doogle & thanks max too your code is closer then mine was. i have been busting my head over that for ages now thank you so much also sorry for reposting