Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Improving a function with user input

1 回表示 (過去 30 日間)
Mitchell
Mitchell 2019 年 4 月 24 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I'm wanting to allow the user to input the lot_type within the parking_cost1 function to prevent the current need to keep inputing times to finish the function. Do I just need to use another switch case?
function parking_cost1
%This function takes the input of days, hour, min and calculates the cost
%for each parking lot in order of 'Short Term','Long Term', 'Intermediate',
%and 'Economy. Times will need to be input in sucession.
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 for the short term parking lot \n',cost)
end
lot_type='Long 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 for the long term parking lot\n',cost)
end
lot_type='Intermediate';
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 for the intermediate term parking lot\n',cost)
end
lot_type='Economy';
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 for the economy parking lot\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
case 'Intermediate'
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 $8
cost=min(cost,8*nth_days);
end
case 'Long 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 $7
cost=min(cost,7*nth_days);
end
case 'Economy'
if 0<=time && time<=15
cost=0;
elseif 16<=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 $5
cost=min(cost,5*nth_days);
end
end
end
  3 件のコメント
Mitchell
Mitchell 2019 年 4 月 25 日
Yes, I wanting the user to input the time once and then select a lot. What do you mean by a helper function?
Stephen23
Stephen23 2019 年 4 月 25 日
"What do you mean by a helper function?"
Repeating code is generally a bad idea. One easy way to avoid this it to simply place the relevant code in a function and call that function as required.

回答 (0 件)

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by