Function not giving an output

6 次查看(过去 30 天)
The code I have is to find the cost of a parking bill. I want the code to return an error message if the inputs are not within my parameters but when I run it, there is not output. pt is a deliniation between long term and short term parking and time is suppose to be a 4 element row vector.
function y = Parking_Bill(pt,time)
if (pt == (1 || 2) && (size(time) == 4))
w = time(1);
d = time(2);
h = time(3);
m = time(4);
if pt == 1
if d>7
weeks = (d/7);
w_fixed = fix(weeks)+ w;
d_fixed = round(rem(d,7));
else
w_fixed = w;
d_fixed = d;
end
if h>24
days= (h/24);
d_fixed= fix(days);
h_fixed = rem(h,24)*24;
else
h_fixed = h;
end
if m > 60
hours = m/60;
h_fixed = fix(hours);
end
if w_fixed >= 1
weekly_bill= 50 * w_fixed;
else
weekly_bill = 0;
end
if d_fixed>=1
daily_bill = 8* d_fixed;
else
daily_bill=0;
end
if h_fixed >= 1
hourly_bill = 2+ (h_fixed-1);
else
hourly_bill = 0;
end
total_bill = weekly_bill + daily_bill + hourly_bill;
y = fprintf('Total bill =$%d.00', total_bill);
if pt == 2
if w > 0
weeks_to_minutes = (w * 10080);
end
if d > 0
days_to_minutes = (d * 1440);
end
if h > 0
hours_to_minutes = (h * 60);
end
if m > 30
minutes = (m/20);
m_fixed= fix(minutes) * 2;
else
m_fixed = 0;
end
total_bill = weeks_to_minutes + days_to_minutes + hours_to_minutes + m_fixed;
y = fprintf('Total bill =$%d.00', total_bill);
else
y = fprintf('Invaild input, check input');
return;
end
end
end
end
  3 个评论
the cyclist
the cyclist 2022-4-10
Rephrasing @KSSV's question:
Can you give specific examples of valid and invalid inputs?

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-4-10
function y = Parking_Bill(pt,time)
% if (pt == (1 || 2) && (size(time) == 4))
if (pt == 1 || pt == 2) && numel(time) == 4
w = time(1);
d = time(2);
h = time(3);
m = time(4);
if pt == 1
% calculate total_bill for pt == 1
% fprintf returns the number of bytes printed. you don't want
% that. use sprintf to return the actual character vector printed
% y = fprintf('Total bill =$%d.00', total_bill);
y = sprintf('Total bill =$%d.00', total_bill);
% if pt == 2
elseif pt == 2
% calculate total_bill for pt == 2
% y = fprintf('Total bill =$%d.00', total_bill);
y = sprintf('Total bill =$%d.00', total_bill);
else
% in this case sprintf/fprintf is not necessary
% (nothing formatted in y)
% y = fprintf('Invaild input, check input');
y = 'Invalid input (pt), check input';
% return is not necessary here since the function is about to
% return anyway (i.e., there's no more code to execute in this
% case)
% return;
end
% end
else
% need to return some output y in case pt is not 1 and not 2,
% or t does not have 4 elements
y = 'Invalid input (pt and/or time), check input';
end % end if (pt == 1 || pt == 2) && numel(t) == 4
end % end function

更多回答(1 个)

the cyclist
the cyclist 2022-4-10
编辑:the cyclist 2022-4-10
This condition almost certainly does not do what you expect:
(pt == (1 || 2) && (size(time) == 4))
The problem here is (at least) two-fold. The expression
(1||2)
is going to just evaluate to true. Then you are asking if
pt == true
which presumably is not what you mean. I think you want
(pt==1)||(pt==2)
Also,
size(time)
is always going to be a two-element vector -- row count and column count, so you are going to get a two-element vector from that expression.

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by