外部インタフェース/API | ![]() ![]() |
1. エントリを追加のために入力
関数pb_add
は、1つの引数、Properties
オブジェクトpb_htable
を受け取ります。pb_add
は、disp
を使ってエントリの入力を指示します。上向き矢印 (^
) キャラクタをラインデリミタとして使って、input
は名前を変数entry
に入力します。その後、 whileループ内で、inputを使ってエントリの別の行を変数line
に取り込みます。エントリが終了したことを示す行が空の場合は、コードはwhileループから出ます。行が空でない場合は、elseステートメントは、エントリに行を追加し、その後ラインデリミタを追加します。最後にstrcmp
はinputが入力されなかった可能性をチェックし、その場合はりターンします。
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;
2. 電話帳にエントリを追加
入力が完了した後で、pb_add
は、ハッシュキーname
(空白をアンダースコアに変更するためにpb_keyfilter
が呼び出されます)とentry
を使って、pb_htable
についてput
を呼び出します。その後、エントリが追加されたことを示すメッセージが表示されます。
pb_htable.put(pb_keyfilter(name),entry); disp ' ' disp(sprintf('%s has been added to the phone book.', name));
![]() | 関数pb_lookupの説明 | 関数pb_removeの説明 | ![]() |