Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For dato:

Code Block
function serializedserializedDate = serializeDate ( date )
	d = int2str ( date . day );
	m = int2str ( date . month );
	y = int2str ( date . year );
	datestring =  serializedDate = sprintf('%02i.%02i.%02i%04i', d,m,y);
end

Eller:

Code Block
 function serialized = serializeDate ( date )
    d = int2str ( date . day );
    m = int2str ( date . month );
    y = int2str ( date . year );
    if date . day < 10
        d = strcat ('0', d);
    end
    if date . month < 10
        m = strcat ('0', m);
    end
    serialized = strcat (d, '.', m, '.', y, date.month, date.year);
end

 

For person:

Code Block
function [serializedPerson personSerial ] = serialize_personserializePerson( person )
	personSerial    serializedPerson = sprintf('%s#%s#%s%s#%s#%i',person.name, serializeDate(person.dateOfBirth), person.phone);
end

Eller:

Code Block
function serialized = serializePerson ( person )
	n = person. name ;
	d = serializeDate (person.dateOfBirth);
	p = int2str(person.phone);
	serialized = strcat(n, '#', d, '#', p);
end

b)

Har mange løsninger. Deriblant: 

Code Block
function deserialized = deserializeDate ( datedatestring )
	    [tok, rest] = strtok(datedatestring, '.');
	d    day = str2num(tok);
	    [tok, rest] = strtok(rest, '.');
	m    month = str2num(tok);
	    tok = strtok(rest, '.');
	y    year = str2num(tok);
	    deserialized = struct('day', dday, 'month', mmonth, 'year', yyear);
end end 
Code Block
function deserialized = deserializePerson ( personpersonstring )
	    [tok, rest] = strtok(personpersonstring, '#');
	n    name = tok ;
	    [tok, rest] = strtok(rest, '#');
	d = deserializDate(tok);
	tok = strtok(rest, '#');
	p date = str2numdeserializeDate(tok);
	deserialized = struct('name', n, 'dateOfBirth', d, 'phone', p);
end

Eller:

Code Block
function [ dStruct ] = deserializeDate( datestring )
	[day, month, year] = strsplit(datestring, '.');
	dStruct=struct('day', day, 'month', month, 'year', year);
end
Code Block
 function deserialized = deserializePerson ( person )
	[name, dateSerial, phone] = strsplit(person, '#');
	date = deserializDate(dateSerial);
	 tok = strtok(rest, '#');
    phone = str2num(tok);
    deserialized = struct('name', name, 'dateOfBirth', date, 'phone', phone);
end

...

Code Block
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