www.gusucode.com > MATLAB2008应用程序接口编程技术源码程序 > MATLAB2008应用程序接口编程技术源码程序/code/第10章/10.7/pb_lookup.m

    function pb_lookup(pb_htable,name)
entry = pb_htable.get(pb_keyfilter(name));
if isempty(entry),
    disp(sprintf('The name %s is not in the phone book',name));
else
pb_display(entry);
end

%%%添加号码的子程序
function pb_add(pb_htable)
disp 'Type the name for the new entry, followed by Enter.'
disp 'Then, type the phone number(s), one per line.'
disp 'To complete the entry, type an extra Enter.'
name = input(':: ','s');
entry=[name '^'];
while 1
    line = input(':: ','s');
if isempty(line)
    break;
else
entry=[entry line '^'];
    end;
end;
if strcmp(entry, '^')
disp 'No name entered'
return;
end;
%%%添加对应的电话号码
pb_htable.put(pb_keyfilter(name),entry);
disp ' '
disp(sprintf('%s has been added to the phone book.', name));

%%%删除号码的子程序
function pb_remove(pb_htable,name)
if ~pb_htable.containsKey(pb_keyfilter(name))
disp(sprintf('The name %s is not in the phone book',name))
return
end;
r = input(sprintf('Remove entry %s (y/n)? ',name), 's');
if r == 'y'
%%%删除选中的电话号码
pb_htable.remove(pb_keyfilter(name));
disp(sprintf('%s has been removed from the phone book',name))
else
disp(sprintf('%s has not been removed',name))
end;


%%%修改号码的子程序
function pb_change(pb_htable,name)
entry = pb_htable.get(pb_keyfilter(name));
if isempty(entry)
disp(sprintf('The name %s is not in the phone book', name));
return;
else
pb_display(entry);
r = input('Replace phone numbers in this entry (y/n)? ','s');
if r ~= 'y'
return;
end;
end;
disp 'Type in the new phone number(s), one per line.'
disp 'To complete the entry, type an extra Enter.'
disp(sprintf(':: %s', name));
entry=[name '^'];
while 1
line = input(':: ','s');
if isempty(line)
break;
else
entry=[entry line '^'];
end;
end;
%%%完成电话号码的修改
pb_htable.put(pb_keyfilter(name),entry);
disp ' '
disp(sprintf('The entry for %s has been changed', name));

%%%显示电话号码列表的子程序
function pb_listall(pb_htable)
enum = pb_htable.propertyNames;
while enum.hasMoreElements
key = enum.nextElement;
%%%调用pb_display函数
pb_display(pb_htable.get(key));
end;

%%%显示号码的子程序
function pb_display(entry)
disp ' '
disp '-------------------------'
[t,r] = strtok(entry,'^');
while ~isempty(t) 
disp(sprintf(' %s',t));
    [t,r] = strtok(r,'^');
end;
disp '-------------------------'

function out = pb_keyfilter(key)
if ~isempty(findstr(key,' '))
out = strrep(key,' ','_');
else
out = strrep(key,'_',' ');
end;