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!

Mathematical Formula 2

Status
Not open for further replies.

axLW

Programmer
Feb 6, 2015
110
GB
Hello all.

I have a formula that calculates a price based on the number of miles.

Here is the formula:

For the first 1 mile - charge £19 flat rate
For the next 29 miles - charge £1.50 for every 1 mile
For the next 20 miles - charge £1.25 for every 1 mile
For the rest - charge £1.00 for every 1 mile

Here are some valid results for testing:

1 mile = £19
5 miles = £25
10 miles = £32.50
20 miles = £47.50
50 miles = £87.50
100 miles = £137.50

I think this can be acheived using a Select Case.

I have tried the following:
Code:
<%

Mileage = 10

[b]Sub GetPrice(ByVal Mileage As Integer)[/b]
Select Case Mileage

Case Is <=1
Price = 19

Case Is 1 To 30
Price = 19 + ((Mileage - 1) * 1.50)

End Select

End Sub

Response.Write Price 

%>

But already I'm stuck (Expected ')' on bold line..

I'm not sure if this is the best way to go about things (logically)...

Really could use some assistance.

Thanks


 
Hi there,

look at the following (warning: untested!). It might not look super elegant but it should do the trick just fine:
Code:
price=19

Select Case Mileage

Case Is >50
	for i = 51 to mileage
	  price=price+1	
	next i
	price=price+63.5

Case Is >30
	for i = 31 to mileage
	  price=price+1.25
	next i
	price=price+43,5

Case Is >=1
	for i= 2 to mileage
	  price=price+1.5
	next i

Case Else 'mileage=0
	price=0



"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.
 
Hello MakeItSo, thank you very much for the swift reply.

My actual formula is a bit more complex so I was hoping for something a little easier to read and edit.

I was hoping for a clear distinction between the prices to be charged and the additional miles to be added.

That way I could add further steps to my formula quite easily.

 
A: This "Sub GetPrice(ByVal Mileage As Integer)" is Visual Basic NOT VbScript


B:This code
Code:
Select Case Mileage

Case Is >50
	for i = 51 to mileage
	  price=price+1	
	next i
	price=price+63.5

Case Is >30
	for i = 31 to mileage
	  price=price+1.25
	next i
	price=price+43,5

Case Is >=1
	for i= 2 to mileage
	  price=price+1.5
	next i

Case Else 'mileage=0
	price=0

Is ALSO Visual Basic NOT VbScript

Select .. Case in VbScript can ONLY check for equality, it cannot do comparison testing


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks Chris, can you please advise me as to the best approach (or perhaps direct me towards a similar example) and I can try and get it working..?
 
Thanks Chris. It's the result of moving away from VBS for two years.... [blush]

"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.
 
@ axLW

You are not going to get a "simple" snippet of code for what you hope to do, the vbscript in ASP does not have the capabilities to parse your data and make such 'decisions'. It is going to require a call to a very convoluted function or instantiate a object class that encapsulates several functions and properties.



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Give me two or three hours and I'll put together a vbscript class that you can expand on.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Though it might take a little longer because I'll have to set up a temporary Windows machine to test on.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks a lot Chris. I'll be sure to send you a delicious fruit hamper in the post.
 
Isn't it just a bunch of sequential if statements?
Code:
price = 19
distance = distance - 1
if distance > 0 then price = price + min(distance, 29) * 1.5
distance = distance - 29
if distance > 0 then price = price + min(distance, 20) * 1.25
distance = distance - 20
if distance > 0 then price = price + distance * 1

function min(a,b)
    if a < b then
       min = a
    else
       min = b
    end if
end function
 
Okay back at it, sorry took longer than I anticipated to get ASP running on IIS7, and I had to sleep as well. (I must be getting 'old')

ASP Class encapsulation (called MileageCalcs file name mileage-calcs.asp)
Code:
<%

class MileageCalcs

dim i_BP1
dim i_BP2
dim i_BP3
dim f_Mult1
dim f_Mult2
dim f_Mult3
dim i_IC
dim b_IsError


'Properties with only a "Let" are write only
' properties with only a ""Get"" are read only'

public Property let PriceBreak1(ByVal val)
  i_BP1 = val
end property

public Property let PriceBreak2(ByVal val)
  i_BP2 = val
end property

public Property let PriceBreak3(ByVal val)
  i_BP3 = val
end property

public Property let Muliplier1(ByVal val)
	f_Mult1 = val
end property

public Property let Muliplier2(ByVal val)
	f_Mult2 = val
end property

public Property let Muliplier3(ByVal val)
	f_Mult3 = val
end property
public Property let Initial (ByVal val)
	i_IC = val
end property

public Property Get IsError()
	IsError = b_IsError
	' set true if a value of less than one is passed to .Calc
end property

public Function Calc(i_Mileage)
if i_mileage < 1 then
		b_IsError = True
		exit function
		tmp = -i_IC
end if
	i_Mileage = i_Mileage - 1
if i_Mileage > 0 then tmp = tmp + min(i_Mileage, i_BP1) * f_Mult1
	i_Mileage = i_Mileage - i_BP1
if i_Mileage > 0 then tmp = tmp + min(i_Mileage, i_BP2) * f_Mult2
	i_Mileage = i_Mileage - i_BP2
if i_Mileage > 0 then tmp = tmp + i_Mileage * f_Mult3
Calc=tmp + i_IC
end Function

function min(i_x,i_y)
    if i_x < i_y then
       min = i_x
    else
       min = i_y
    end if
end function 

public sub Initialize()

end sub
public sub terminate()

end sub
end class
%>

Bit more flexible than a fixed function.

If you want to set any of the values when the class is instantiated you can do so in the Initialize sub

How to include and use is below;

Code:
<!--#include virtual = "mileage-calcs.asp" -->
<%
Set calcs = new  MileageCalcs
calcs.PriceBreak1 = 29
calcs.pricebreak2 = 20
calcs.Initial = 19
Calcs.Muliplier1 = 1.5
Calcs.Muliplier2 = 1.25
Calcs.Muliplier3 = 1.0


response.write calcs.calc(100)
%>





Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Chris this looks GREAT!

Let me get my teeth into it :)
 
Chris this is brilliant. Works perfectly and I've managed to add more steps.

What do I owe you?!
 
Thing of beauty.

"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.
 
What do I owe you?!

A grand total of nothing at all, I don't code for financial gain these days, it's just good 'brain exercise' :)

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top