|
-
Mar 16th, 2000, 09:35 AM
#1
Thread Starter
Lively Member
You are to write a program that will find all the solutions to the following problem. Farmer Joe has exactly 100 dollars to spend on livestock. He needs to buy as many animals as possible. Cows cost 6 dollars, pigs cost 3 dollars, and sheep cost 4 dollars. Output all combinations of animals that Farmer Joe can purchase with out over-spending or under-spending.
Example:
Given the numbers above there are 81 solutions possible.
First solution is: 1 sheep, 0 pigs, 16 cows
Last solution is: 25 sheep, 0 pigs, 0 cows.
To receive an A on this assignment you should allow the user to enter a price for each animal and a total amount of money that Farmer Joe has to spend.
I have no idea how to go about this, and am probably screwed. Can anyone help me on this?
-
Mar 16th, 2000, 11:13 AM
#2
Frenzied Member
There's 2 basic ways of doing this
The good way will probably be about 20 lines of code, it can do it for as many animals as you like with the same 20 lines of code, which mens you can let your user control what animals, how many types of animal, prices of animals etc. but whoever you're doing the project for might not beleve it was you wot wrote it. It also uses collections and if you havn't been taught these yet your teacher will twig you've had some help. (I'm assuming you're doing this for a class)
If not You can do it the bad way which is how he expects you to do it.
Post which one you wan't and i'll give you a hand.
also, if you select the first way post if you've learnt about class modules yet cos that's the propper way to do it.
-
Mar 16th, 2000, 11:18 AM
#3
Thread Starter
Lively Member
That's alright. Something amazing happened...
I e-mailed my teacher, and he responded with a simple answer. I didn't think he would do it because it was due tomorrow and all... But the answer's really easy. So thanks for trying to help.
Sorry.
-
Mar 16th, 2000, 12:16 PM
#4
Frenzied Member
I would like to see the answer...
Apollo:Will you post the simple answer, please? I would like to see it!
Sam Finch:I would like to see your answer, too, please. (The first one, using class modules).
Thanks for sharing the knowledge!!!
~seaweed
-
Mar 16th, 2000, 08:25 PM
#5
Frenzied Member
Right I'm going to post the whole .frm file,
Code:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 5100
ClientLeft = 60
ClientTop = 345
ClientWidth = 7305
LinkTopic = "Form1"
ScaleHeight = 5100
ScaleWidth = 7305
StartUpPosition = 3 'Windows Default
Begin VB.ListBox lstResults
Height = 1815
Left = 1800
TabIndex = 11
Top = 5880
Visible = 0 'False
Width = 4815
End
Begin VB.CommandButton cmdCalculate
Caption = "Show Choices"
Height = 1215
Left = 240
TabIndex = 10
Top = 3840
Width = 3135
End
Begin VB.CheckBox chkUnderSpending
Caption = "Allow Under Spending"
Height = 615
Left = 1200
TabIndex = 9
Top = 3120
Width = 2055
End
Begin VB.TextBox txtMoney
Height = 285
Left = 2880
TabIndex = 8
Text = "100"
Top = 240
Width = 615
End
Begin VB.CommandButton cmdRemove
Caption = "Remove"
Height = 495
Left = 1200
TabIndex = 4
Top = 2640
Width = 2295
End
Begin VB.CommandButton cmdAdd
Caption = "Add"
Height = 495
Left = 1200
TabIndex = 3
Top = 2160
Width = 2295
End
Begin VB.TextBox txtPrice
Height = 495
Left = 1200
TabIndex = 2
Text = "6"
Top = 1680
Width = 2295
End
Begin VB.TextBox txtAnimal
Height = 495
Left = 1200
TabIndex = 1
Text = "Cow"
Top = 1200
Width = 2295
End
Begin VB.ListBox lstAnimals
Height = 3765
Left = 3600
TabIndex = 0
Top = 1200
Width = 3375
End
Begin VB.Label Label3
AutoSize = -1 'True
Caption = "Give Farmer Jones $"
Height = 195
Left = 1320
TabIndex = 7
Top = 240
Width = 1500
End
Begin VB.Label Label2
Caption = "Price"
Height = 495
Left = 120
TabIndex = 6
Top = 1800
Width = 855
End
Begin VB.Label Label1
Caption = "Name"
Height = 375
Left = 120
TabIndex = 5
Top = 1320
Width = 855
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim collPrices As New Collection
Dim collNames As New Collection
Private Sub cmdAdd_Click()
On Error Resume Next
AddAnimal txtAnimal.Text, CLng(txtPrice.Text)
End Sub
Private Sub cmdCalculate_Click()
Dim collReturn As Collection
Dim varTemp As Variant
lstResults.Clear
Set collReturn = CalculateChoices(CloneCollection(collNames), CloneCollection(collPrices), CLng(txtMoney.Text), chkUnderSpending.Value)
For Each varTemp In collReturn
lstResults.AddItem CStr(varTemp)
Next varTemp
Form1.WindowState = vbMaximized
lstResults.Move 0, 0, Form1.Width, Form1.Height
lstResults.Visible = True
End Sub
Private Sub cmdRemove_Click()
RemoveAnimal lstAnimals.ListIndex
End Sub
Private Sub AddAnimal(Name As String, Price As Long)
collPrices.Add Price
collNames.Add Name
lstAnimals.AddItem Name & " $" & CStr(Price)
End Sub
Private Sub RemoveAnimal(Index As Integer)
collPrices.Remove (Index)
collNames.Remove (Index)
lstAnimals.RemoveItem (Index)
End Sub
Private Sub Form_Load()
AddAnimal "Cow", 6
AddAnimal "Sheep", 4
AddAnimal "Pig", 3
End Sub
Private Function CloneCollection(Model As Collection) As Collection
Dim varTemp As Variant
Dim collOutput As New Collection
For Each varTemp In Model
collOutput.Add varTemp
Next varTemp
Set CloneCollection = collOutput
End Function
Private Function CalculateChoices(Names As Collection, Prices As Collection, SpendingMoney As Long, AllowUnderSpending As Boolean) As Collection
Dim lngPrice As Long
Dim strName As String
Dim collOutput As New Collection
Dim i As Integer
Dim dblMaxPurchace As Double
Dim collReturn As New Collection
Dim varTemp As Variant
lngPrice = Prices.Item(1)
strName = Names.Item(1)
Prices.Remove (1)
Names.Remove (1)
dblMaxPurchace = SpendingMoney / lngPrice
Select Case Prices.Count
Case 0
If AllowUnderSpending Then
For i = Fix(dblMaxPurchace) To 0 Step -1
collOutput.Add CStr(i) & " " & strName & IIf(i = 1, "", "s") & "." & vbTab & "$" & CStr(SpendingMoney - (i * lngPrice)) & " Remaining."
Next i
Else
If dblMaxPurchace = Fix(dblMaxPurchace) Then
collOutput.Add CStr(CLng(dblMaxPurchace)) & " " & strName & IIf(dblMaxPurchace = 1, "", "s") & "."
End If
End If
Case Else
For i = Fix(dblMaxPurchace) To 0 Step -1
Set collReturn = CalculateChoices(CloneCollection(Names), CloneCollection(Prices), SpendingMoney - (i * lngPrice), AllowUnderSpending)
For Each varTemp In collReturn
collOutput.Add CStr(i) & " " & strName & IIf(i = 1, "", "s") & ", " & varTemp
Next varTemp
Next i
End Select
Set CalculateChoices = collOutput
End Function
Private Sub lstResults_DblClick()
lstResults.Visible = False
Form1.WindowState = vbNormal
End Sub
basicly cut and paste everything in the code brackets to notepad and save it with a .frm extension. The add a form to an exe and load this file, Remove the other form and set this one as the startup object.
I changed my mind about class modules, they're usually what you want for this sort of thing but not today.
Most of the code is just obvious user interface stuff, the clonecollectinon function creates an identical copy of the collection you pass it so you can effectivly pass collections around by value.
the calculatechoices function is the meat of it, it's a bit bigger than I expected but what the hell.
what it does is gets the 2 collections, prices and names and checks how many items there are in the collection, if ther's just one it returns a collection with all the options if you only had one type of animal, ie you've got $100, Sheep are $5, how many sheep can you buy?
if there's more than one item in the collection then it removes the first item and calls itself with the new collection once for each option it has of how many animals to purchace ie you have $100, sheep are $5 so you can but 1,2,3,3,4,...,19 or 20 sheep.
when it gets it's back a collection it adds it's own data to it and adds it to it's own intermnal collection. then when it's exausted all it's buting options it returns that collection.
I'ts much harder to explain than it is to do, read through the code and try to see whats going on. or try running it through using F8.
Hope this helps
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|