a)
Code Block | ||
---|---|---|
| ||
function personProgram ()
choice = -1;
P = [];
printHeader();
while choice ~= 0
printMenu();
choice = input ('Velg et tall: ');
clc
if choice == 0
continue;
end
P = process(P, choice);
end
printFooter();
end |
...
Code Block |
---|
function printFooter () fprintf('Programmet avsluttes .\n'); end |
e)
Code Block | ||
---|---|---|
| ||
function P = processLoad () fprintf ('Skal laste en fil \n'); end function processStore(P) fprintf('Skal lagre en fil \n'); end function processListPersons(P) fprintf('Skal skrive ut personene \n'); end function P = processNewPerson(P) fprintf('Skal legge til en ny person \n'); end function P = processChangePerson(P) fprintf('Skal endre en person \n'); end |
f)
Code Block | ||
---|---|---|
| ||
function P = process (P, choice ) fprintf ('\n'); switch choice case 1 P = processLoad(); case 2 processStore(P); case 3 processListPersons(P); case 4 P = processNewPerson(P); case 5 P = processChangePerson(P); otherwise fprintf ('%s\n', 'Ugyldig valg '); end fprintf ('\n'); end |
...
Code Block |
---|
function processStore(P) filename = input ('Hvilken fil vil du lagre til ? ', 's'); store(filename, P); end |
i)
Code Block | ||
---|---|---|
| ||
function processListPersons(P) listPersons(P); end % unødvendig, ja :) |
j)
Code Block |
---|
function [newListOfPersons] = processNewPerson(listOfPersons) newData =batchRegisterPersons promptPerson(); newListOfPersons = [listOfPersons,newData]; end |
...
Code Block |
---|
function P = processChangePerson (P)
for i = 1:length(P)
fprintf('%i. ', i);
printPerson(P(i));
end
index = input ('Hvilken person vil du endre ? ');
P(index) = promptPerson();
end |
i)
Code Block |
---|
function personProgram () choice = -1; P = []; printHeader(); while choice ~= 0 printMenu(); choice = input ('Velg et tall: '); clc if choice == 0 continue end P = process(P, choice); end willSave = input('Vil du lagre ulagrede endringer? [Y/N]', 's'); if strcmp(willSave, 'Y') process(P, 2); end printFooter(); end |
m)
Code Block |
---|
i process(P, choice)
...
case 6
processSearchPerson(P);
...
i printFooter()
...
fprintf('%s\n', '6. Søk etter person');
...
function processSearchPerson(P)
key = input('Hva heter personen?', 's');
for i = 1:length(P)
if strcmp(key, P(i).name)
printPerson(P(i));
pause(1);
return
end
end
fprintf('Kunne ikke finne %s\n', key);
end |