Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Fraction Simplifying Calculator 1

Status
Not open for further replies.

Sneapy

Technical User
Nov 1, 2012
14
0
0
GB
Hey

Im in the middle of creating a fraction calculator using vb script and i need it too simplify the fractions. Would be great if someone could give me a hand with this its been racking my brains...


This is what i have so far, the bottom part of the script under the btnsimp, is my attempt of getting it to work...


Code:
<html>
<head>
    <title>Fraction Calculator</title>
</head>
<body bg color="DCDCDC">
<center>

<font size="6"><b><i>Fraction Calculator</i></b></font>


<p></p>

<input style="width:20px" id="txt1" type="text" />
<input style="width:20px" id="txt2" type="text" /><p><input style="width:20px" id="txt3" type="text" />
<input style="width:20px" id="txt4" type="text" />
</p>
<p><input id="ans01" type="text" readonly="true"/></p>
<p><input id="ans02" type="text" readonly="true" /></p>
<p></p>
<p><input id="btnp" type="button" value="Plus" />
<input id="btns" type="button"  value="Subtract" /></p>
<p><input id="btnd" type="button" value="Divide" />
<input id="btnx" type="button" value="Multiple" /> </p>
<p><input id="btnsimp" type="button" value="Simplify Fraction" /></p>
<p id="ans3"></p>
<p id="ans4"></p>

<script language=vbscript> 

sub btnp_OnClick()
    ans01.innertext=CInt(txt1.value)*CInt(txt4.value)+CInt(txt3.value)*CInt(txt2.value)
    
    ans02.innertext=CInt(txt3.value)*CInt(txt4.value)
    
    end sub

    sub btns_OnClick()
    ans01.innertext=CInt(txt1.value)*CInt(txt4.value)-CInt(txt3.value)*CInt(txt2.value)
    
    ans02.innertext=CInt(txt3.value)*CInt(txt4.value)
    
    end sub

    sub btnd_OnClick()
    ans01.innertext=CInt(txt1.value)*CInt(txt4.value)
    
    ans02.innertext=CInt(txt3.value)*CInt(txt2.value)
    
    end sub

    sub btnx_OnClick()
    ans01.innertext=CInt(txt1.value)*CInt(txt2.value)
    
    ans02.innertext=CInt(txt3.value)*CInt(txt4.value)
    
    end sub

    sub btnsimp_OnClick()
    if ans01.value= "even" then
    ans01.innertext=CInt(ans01.value/2)
    end if
    if ans02.value="even" then
    ans02.innertext=CInt(ans02.value/2)
    end if
    end sub
</script>
 
In order to simplify a fraction, you need to find the greatest common denominator for dividend and divisor.
Check this out here and adapt to VBScript:

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
you need to find the greatest common denominator for dividend and divisor.
The link you posted is only part of the solution. It shows how to find the Greatest Common Divisor. However, we need the divisor in order to find the dividend - just a small tweak.

Code:
function gcd(d, e)
  d1 = d : e1 = e
  while (e <> 0)
    temp = d mod e   
    d = e
    e = temp
  wend
  gcd = (d1 / d) & "/" & (e1 / d)
end function

-Geates
 
Correction. For logic's sake, the function should be called "simplify". And, because VB's modulator can only modulate integers, you'll need to find an alternative gcd() function.

Code:
msgbox simplify(4, 16)
msgbox simplify(16, 4)
msgbox simplify(5, 17)
msgbox simplify(4.125, 33)
msgbox simplify(4.125, 34)

function simplify(d, e)
	d1 = d : e1 = e
	d = gcd(d, e)
	dividend = (d1 / d)
	divisor = (e1 / d)
	if (divisor = 1) then 
		simplify = dividend
	else
		simplify = dividend & "/" & divisor
	end if
end function

function gcd(a, b)
	while (a <> b) 
		if (a > b) then
			a = a - b
		else
			b = b - a
		end if
	wend
	gcd = a
end function

-Geates
 
thanks for the help guys,

but im not very good at vb script took me longenough to do what i did, could you help explain it a bit more :)
 
The best way is to use [tt]msgbox[/tt] to see what's going on. For example:

Code:
function gcd(a, b)
	[red]msgbox "The two values are a:" & a & " and b:" & b[/red]
	while (a <> b) 
		if (a > b) then
			[red]msgbox a & " is greater than " & b & ".  Subtracting " & b & " from " & a[/red]
			a = a - b
		else
			[red]msgbox b & " is greater than " & a & ".  Subtracting " & a & " from " & b[/red]
			b = b - a
		end if
	wend
	[red]msgbox "a equals b. Therefore, the GCD is " & a[/red]

	gcd = a
end function

Or, execute the code on paper. Given a = 5 and b = 15, this is how the gcd() function runs

a = 5, b = 15
1) a < b: b = 15 - 5: b = 10

a = 5, b = 10
2) a < b: b = 10 - 5: b = 5

a = 5: b = 5
3) a = b: return a

gcd(5, 15) = 5


-Geates

 
you know when you refered to the the link that was posted by another user, i cant make sense of the link.

i know im being stupid but if you start from the beggining from when you press the simplify button that would be a great help

thanks :)
 
You're not being stupid. You're just mis- or un- informed and you can't, generally, be faulted for that.

Code:
msgbox simplify(5, 15) 'results in 1/3

function simplify(d, e)
	d1 = d : e1 = e
	denominator = gcd(d, e)
	dividend = (d1 / denominator)
	divisor = (e1 / denominator)
	if (divisor = 1) then 
		simplify = dividend
	else
		simplify = dividend & "/" & divisor
	end if
end function

function gcd(a, b)
	while (a <> b) 
		if (a > b) then
			a = a - b
		else
			b = b - a
		end if
	wend
	gcd = a
end function

The variable names a, b, d, and e are just names. I could have named them Pink, Fluffy, Dancing, and Unicorns but that would have been ridiculous! Although, to reduce confusion, I renamed "d" to "denominator" in "denominator = gcd(5, 15)"

1) simplify(5, 15)
2) d1 = d: e1 = e <- Store values d and e in variables d1 and e1, respectively, for later use
3) denominator = 5 <- Result from gcd(5, 15) as shown previously

Now that we have the denominator, we divid the original values by the denominator to get the simplified values

4) dividend = (d1 / denominator) = (5 / 5) = 1
5) divisor = (e1 / denominator) = (15 / 5) = 3

Put it all together

6) simplify = dividend & "/" & divisor = "1/3"

-Geates



 
right so do i just pop this at the bottom of my code?
 
sorry i havent replied, but i popped in the bottom of the code but nothing happens
 
There are several error in your OP HTML file that prevent the rest of the code from executing properly. I created an HTML-implemented solution for you but it is behaving very oddly. I started thread thread215-1699016 to get some help with it. Once the solution is concreate I will post it here.

-Geates

 
...variable types, pf!

This is how I would implement the solution into HTML.
Code:
<html>
	<head>
		<title>Fraction Calculator</title>
	</head>

	<script language="vbscript"> 
		sub btnsimp_OnClick()
			answer.innerHTML = simplify(txt1.value, txt2.value)
		end sub
		
		function simplify(d, e)
			d1 = d : e1 = e
			denominator = gcd(d, e)
			dividend = (d1 / denominator)
			divisor = (e1 / denominator)
			if (divisor = 1) then 
				simplify = dividend
			else
				simplify = dividend & "/" & divisor
			end if
		end function
		
		function gcd(a, b)
			a = cint(a)
			b = cint(b)
			while (a <> b) 
				if (a > b) then
					a = a - b
				else
					b = b - a
				end if
			wend
			gcd = a
		end function 
	</script> 
	
	<body bgcolor="DCDCDC">
		<h3><b><i>Fraction Calculator</i></b></h3>
	
		<input id="txt1" type="text" /><br>
		<input id="txt2" type="text" /><br>
		<input id="btnsimp" type="button" value="Simplify Fraction" />
		
		<p id="answer"></p>
	</body>
</html>

-Geates

 
appriecate the help mate ;)

really really do

i have one more favour i need ;)
do you think you could explain what does what so i can develop it, if you dont have the time or dont want too, dont worry youve done more than enough haha, but if you can be great lol ;)

Thanks again
 
Sure, first and foremost, this solution will only work in IE. The reason being is that VBS is a proprietary Microsoft programming language and so is only supported by IE. If you can guarentee that this Fraction Calculator will be used in IE only then you have nothing to worry about. If you can't guarentee it, then you'll probably want to port it over to JavaScript, as it is a cross-browser language.

We want to load our scripts before we load any of the HTML BODY elements (this is not required but highly advisible) - putting our VBS between the <script> tags allows us to do this. Once loaded, we load our HTML BODY elements. Notice the three <input> tags, they are loaded as objects. Each object and its properties can be accessed simply by it's name or id (Microsoft has added support this. In other browsers, you have to access the object by using the Document Object Model (DOM)).

Notice the id of our submit button. Using it, we can execute a function when an event is triggered. In this case, the function is [tt]btnsimp_OnClick()[/tt] and the event is [tt]onClick[/tt]. By naming a sub routine objectID_event we tell the browser to execute the sub routine when the object is clicked. Alternatively, you could name the sub routine something arbitrary and trigger it's execution by using the inline event declaration in the HTML element. For example, if our sub routine were called [tt]snowman[/tt], the submit <input> element would be: [tt]<input id="btnsimp" type="submit" [red]onClick="snowman()"[/red]>[/tt]

Also notice the lonely <p> element with the id of [tt]answer[/tt]. It can be filled with text using object.property = value as seen in the [tt]btnsimpl_OnClick()[/tt] sub routine. In this case, we are assigning the result of simplify() to the .innerHTML property of the <p> object name 'answer'. In order to get a value from simpilfy(), we want to pass the values of our other two <input> to the routine. This is done in the same object.property manner. The result is [tt] [tt]answer.innerHTML = simplify(txt1.value, txt2.value)[/tt]. Because [tt]simplify()[/tt] is a function (not a sub) we can return the value by assigning it to the name of the function: [tt]simplify = value[/tt].

All this being said, I'm beginning to think you were asking for an explanation of the vbs code (which would make sense as this is the VBScript forum). It's just math :) I recommend using echoing the values after every step of the routines so you can clearly see what's happening. Here's a "cut/paste" example.

Code:
<html>
	<head>
		<title>Fraction Calculator</title>
	</head>

	<script language="vbscript"> 
		sub btnsimp_OnClick()
			msgbox "The 'btnsimp' button was clicked"
			answer.innerHTML = simplify(txt1.value, txt2.value)
		end sub
		
		function simplify(d, e)
			msgbox "Executing simplify(d, e) with the values d=" & d & " and e=" & e
			d1 = d : e1 = e  'Store values d and e in temperary variable or later use
			denominator = gcd(d, e)
			msgbox "The GCD of " & d & " and " & e & " is " & denominator 
			dividend = (d1 / denominator)
			divisor = (e1 / denominator)
			msgbox d1 & "/" & denominator & " = " & dividend & " and  " & e1 & "/" & denominator & " = " & divisor
			if (divisor = 1) then 
				simplify = dividend
			else
				simplify = dividend & "/" & divisor
			end if
			msgbox "Therefore, the simplification of " & d1 & " and " & e1 & " is " & simplify
		end function
		
		function gcd(a, b)
			msgbox "Executing gcd(a, b) with the values a=" & a & " and b=" & b
			'Values 'a' and 'b' are passed in as strings.  Doing math with strings is a no-no. For all intents and po
			'For example: '50' + '32' = '5032', not 82.  Therefore, we need to make sure they are integers by converting them
			a = cint(a) 
			b = cint(b)
			msgbox "do this loop while 'a' does not equal 'b' (" & a & " <> " & b & ")"
			while (a <> b) 
				if (a > b) then
					msgbox "'a' is GREATER than 'b' (" & a & " > " & b & ").  Subtracting 'b' from 'a'"
					a = a - b
				else
					msgbox "'a' is LESS than 'b' (" & a & " < " & b & ").  Subtracting 'a' from 'b'"
					b = b - a
				end if
				msgbox "Values: a=" & a & " and b=" & b
			wend
			gcd = a
		end function 
	</script> 
	
	<body bgcolor="DCDCDC">
		<h3><b><i>Fraction Calculator</i></b></h3>
	
		<input id="txt1" type="text" /><br>
		<input id="txt2" type="text" /><br>
		<input id="btnsimp" type="button" value="Simplify Fraction" />
		
		<p id="answer"></p>
	</body>
</html>

-Geates

 
im sorry i cant make heads or tails of the vbscript code, i dont know what the & signs are all over the place and dont understand the d1=d,

just things like that :(

im pulling my hair out over it [hairpull3]
 
& is an ampersand, it is used to concatenate string. the [tt]d1 = d[/tt] is just assigning the value of 'd' to a variable name 'd1'. The value of 'd' is passed to the function.

Here is an example up the above concepts. Copy the code to a text file and save it a a .vbs file (e.g. test.vbs). Double click it to run. Modify and experienment with the code.

Code:
name = "George " & "Washington"
printStuff name

sentence = name & " was our first President."
printStuff sentence

someAmbiguousVariable = sentence & "  However, this ""fact"" is often debated amongst historians"
printStuff someAmbiguousVariable

sub printStuff(text)
   msgbox text
end sub

-Geates

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top