Help! Display Issues
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ashlee Rogers
am 27 Okt. 2020
Kommentiert: Ashlee Rogers
am 27 Okt. 2020
I have asked about this question before, but now I have corrected the mistakes in it and am almost done with it. However, I am having one more problem...I think the code is ok now, but for some reason, it will not display the output that relates to the input of the user...and I don't know what I am doing wrong here. I am very new to MATLAB, and I am just learning about creating functions and calling them into my script, so I am not entirely sure if this is right or not, but I am basing this on the notes I have from my course. Please help!
I was asked to create a script:
Assume that fees are established at an airport parking lot as follows: Fee for the first hour of parking is 25 cents. An additional 25 cents is due if a car is parked in the lot for up to 3 hours. A total of 75 cents is due for any car parked for more than 3 hours, up to 6 hours. One dollar is due if a car remains for more than six hours but not more than 24 hours. One dollar is due for each day or portion of a day thereafter, to a maximum of 7 days. No car may be parked in the lot for more than 7 days. Create a MATLAB script to read the car number (a five digit number) and a total parking time (in hours and minutes), then display the car number and the corresponding fee. The script should print the message 'illegal parking' if the parking time of the car doesnt correspond to any of the categories. The script should end if the input from the user is 99999 for the car number, the function should take, as parameters, the total parking time, perform the calculation of the fee, and return it. The main MATLAB script deals with the user input, calls the function, and displays the corresponding output. The output should look like:
10352 0.50
27665 0.25
32009 illegal parking
13422 2.00
14474 1.00
Here is my function code:
function fee = ParkingFees(t)
for t = 1:168;
if t == 1
fee = 0.25;
elseif (1 < t) && (t < 3)
fee = 0.50;
elseif (3 < t) && (t < 6)
fee = 0.75;
elseif (6 < t) && (t < 24)
fee = 1.00;
elseif (t < 24) && (t < 48)
fee = 2.00;
elseif (t < 48) && (t < 72)
fee = 3.00;
elseif (t < 72) && (t < 96)
fee = 4.00;
elseif (t < 96) && (t < 120)
fee = 5.00;
elseif (t < 120) && (t < 144)
fee = 6.00;
elseif (t < 144) && (t < 168)
fee = 7.00;
else
disp('Illegal Parking');
break
end
end
Here is my main code:
clc
clear
valid = true;
while valid
disp('Do you want to enter the amount of hours?')
answer = input('Enter 1 for YES and 0 for NO :');
if answer == 1
carNumber = input("Enter the car number: ");
t = input("Enter the amount of hours parked: ");
if carNumber >= 99999
valid = false;
else
ParkingFees(t);
end
else
valid = false;
end
end
2 Kommentare
Rik
am 27 Okt. 2020
You appear to have ignored the advice on the previous thread, so why should we help you now?
Akzeptierte Antwort
Rik
am 27 Okt. 2020
Bearbeitet: Rik
am 27 Okt. 2020
doc disp
As for your function:
function fee = ParkingFees(t)
%One line description of this function goes here
%
%function documentation (e.g. explanation of allowed inputs and expected outputs)
if t == 1
fee = 0.25;
elseif (1 < t) && (t < 3)
fee = 0.50;
elseif (3 < t) && (t < 6)
fee = 0.75;
elseif (6 < t) && (t < 24)
fee = 1.00;
elseif (t < 24) && (t < 48)
fee = 2.00;
elseif (t < 48) && (t < 72)
fee = 3.00;
elseif (t < 72) && (t < 96)
fee = 4.00;
elseif (t < 96) && (t < 120)
fee = 5.00;
elseif (t < 120) && (t < 144)
fee = 6.00;
elseif (t < 144) && (t < 168)
fee = 7.00;
else
%Pick anything that will allow you to easily check for illegal parking and use disp in the calling function.
fee=NaN;%or fee=-1;
end
end
3 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Physical Units finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!