Skip to end of metadata
Go to start of metadata

a)

For dato:

function serializedDate = serializeDate ( date )
    serializedDate = sprintf('%02i.%02i.%04i', date.day, date.month, date.year);
end

For person:

function serializedPerson = serializePerson( person )
    serializedPerson = sprintf('%s#%s#%i',person.name, serializeDate(person.dateOfBirth), person.phone);
end

b)

For dato:

function deserialized = deserializeDate ( datestring )
    [tok, rest] = strtok(datestring, '.');
    day = str2num(tok);
    [tok, rest] = strtok(rest, '.');
    month = str2num(tok);
    tok = strtok(rest, '.');
    year = str2num(tok);
    deserialized = struct('day', day, 'month', month, 'year', year);
end 

For person:

function deserialized = deserializePerson ( personstring )
    [tok, rest] = strtok(personstring, '#');
    name = tok ;
    [tok, rest] = strtok(rest, '#');
    date = deserializeDate(tok);
    tok = strtok(rest, '#');
    phone = str2num(tok);
    deserialized = struct('name', name, 'dateOfBirth', date, 'phone', phone);
end

c)

function store ( filename , persons )
	fileID = fopen ( filename , 'w');
	for p = persons
		fprintf ( fileID , '%s\n', serializePerson (p));
	end
	fclose ( fileID );
end

d)

function listOfPersons = loadfile ( filename )
	listOfPersons = [];
	fileID = fopen ( filename , 'r');
	line = fgets ( fileID );
	while ischar ( line )
		person = deserializePerson ( line );
		listOfPersons = [ listOfPersons person ];
		line = fgets ( fileID );
	end
	fclose ( fileID );
end

Sidetype

Ferdig

Undersider, revidert

Undersider, til revisjon

løsning

80

0

0

  • No labels