a)

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

b)

function printHeader()
    fprintf('Velkommen til persondatabasen \n');
end

c)

 function printMenu ()
    fprintf('%s\n', '1. Hent database fra fil');
    fprintf('%s\n', '2. Lagre database til fil');
    fprintf('%s\n', '3. List alle personer');
    fprintf('%s\n', '4. Legg inn ny person');
    fprintf('%s\n', '5. Endre person');
    fprintf('%s\n', '0. Avslutt programmet');
end

d)

 function printFooter ()
    fprintf('Programmet avsluttes .\n');
end

e)

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)

 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

g)

 function P = processLoad()
    filename = input('Hvilken fil vil du laste ? ', 's');
    P = loadfile(filename);
end

h)

function processStore(P)
	filename = input ('Hvilken fil vil du lagre til ? ', 's');
	store(filename, P);
end

i)

function processListPersons(P)
	listPersons(P);
end 

j)

function [newListOfPersons] = processNewPerson(listOfPersons)
    newData = promptPerson();
    newListOfPersons = [listOfPersons,newData];
end

k)

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)

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)

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
  • No labels