How to create a for/ while loop until a condition is met

2 visualizaciones (últimos 30 días)
Mitchell
Mitchell el 24 de Abr. de 2019
Comentada: Rik el 25 de Abr. de 2019
I'm creating a function to determine the cost of parking at an airport. I have specific time requirements for the time parked and the related cost. My question is how can I change my time=input line to incorporate weeks, hours, days, and mins. Also for each lot there is a daily maximum which once you reach you can't be charge any more that day. How do I change my code to account for multiple days in the same lot? Please let me know if you have any questions about what I am asking.
Short Term Parking Lot
Duration Cost
| 0-30 min | Free (day 1 only)
| 31-60 min | $2.00
| Each additional hour | $1.00
| Daily maximum | $14.00
function parking_meter
%This function gives you the total cost of parking for airfair for 4 types of parking lots at Northwest Arkansas Regional
%To determine the cost of the parking the user will then select a parking
%lot
cost=menu('Select a parking lot to calculate your parking cost, a', 'Short Term','Intermediate', 'Long Term', 'Economy')
switch cost
case 1
time=input('input the duration you parked in minutes \n')
if 0<=time && time<=30
disp('Free for one day')
elseif 31<=time && time<=60
disp('$2.00')
elseif time > 60
a_cost=time./60*1;
cost= 2+a_cost;
disp(cost)
end
%How do you incorporate the daily maximum of $14 11 additonal hours
...........
end
  1 comentario
Rik
Rik el 24 de Abr. de 2019
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 24 de Abr. de 2019
Editada: Rik el 24 de Abr. de 2019
You can use the min function to enforce a maximum outcome.
max_cost_allowed=14;
cost=min(cost,max_cost_allowed);
As a more general remark, it would make more sense to me to decouple the cost calculation from the input. That way you can more easily convert your code to a GUI if you want to and you are more free to play around with the input method.
function parking_cost
lot_type='Short Term';
a=input(['parking time in day,hour,minute format\n',...
'(you can skip leading zeros)\n'],'s');
b=regexp(a,'\d*','match');
c=cellfun(@str2double,b);
%fill c with zeros as needed
if numel(c)<3
c=[zeros(1,3-numel(c)) c];
end
time=24*60*c(1)+60*c(2)+c(3);
cost=parking_meter(lot_type,time);
if cost==0
fprintf('parking is free\n')
else
fprintf('total cost is $%.2f\n',cost)
end
end
function cost=parking_meter(lot_type,time)
%This function gives you the total cost of parking for airfair for 4 types
%of parking lots at Northwest Arkansas Regional
%types allowed: 'Short Term','Intermediate', 'Long Term', 'Economy'
%time should be entered in minutes
switch lot_type
case 'Short Term'
if 0<=time && time<=30
cost=0;
elseif 31<=time && time<=60
cost=2;
else
%first hour is $2, each additional is $1
cost=1+ceil(time/60);
nth_days=1+floor(time/(60*24));
%daily max $14
cost=min(cost,14*nth_days);
end
end
end
  9 comentarios
Mitchell
Mitchell el 24 de Abr. de 2019
I guess I haven't gotten the function to work then. What about the remaining lots?
Rik
Rik el 25 de Abr. de 2019
I hard-coded the short term, but you can use something similar to what you had before with menu. I would suggest using a cell array to store the options and use that to convert the number into the char.
And if my code is not working, what input are you entering?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by