我收到了这份大学作业,包括以下内容:
如果单词是一个字母数组,而一个词汇是一个单词数组,则给定一个确定单词是否为另一个单词前缀的函数。 pascal中的代码是一个函数,该函数创建作为给定单词前缀的单词的单向链接列表。
函数的标头必须为以下内容:函数CompleteWord(前缀:WordType;词汇:VocabularyType):WordList;
WordType和VocabularyType只是顶部数组,其最大单元数由名为MaxVocabulary和MaxLength的常数确定
我设法编写了一个名为itsPrefix的布尔函数,该布尔函数确定一个给定的单词是否是另一个单词的前缀,所以我只剩下对功能CompleteWord进行编码,而且我也不允许使用以下pascal设施:使用crlscr,gotoxy,crt,readkey,longint,string,break,因此我只能使用诸如算术和布尔运算符之类的东西,如果在这种情况下重复使用。
因此,例如,如果我将函数应用于word HELL和vocalbulary HELLO,BIRD,TABLE,HELL,该函数将给我返回一个包含单词HELLO和HELL的列表
WordList被定义为:
type
WordList= ^cell;
cell= record
info : WordType;
following: WordList
end;
尽管遇到错误,但这是我设法编写的最好的代码
function CompleteWord(prefix: WordType; vocabulary: VocabularyType) : WordList;
var
g:integer;
ListaAUX:WordList;
begin
new(ListaAUX);
ListaAUX:=nil;
for g:=1 to MaxVocabulary do
if (itsPrefix(prefix,vocabulary[g])) then
begin
new(ListaAUX^.following);
ListaAUX:=ListaAUX^.following;
ListaAUX^.info:=vocabulary[g];
end;
ListaAUX^.following:=nil;
CompleteWord:=ListaAUX;
end;
注:布尔函数itsPrefix可以单独使用,也可以与其他函数配合使用,因此问题不存在,该函数的代码未提供,因为它使用了其他各种函数,这会使问题变得太大
有人可以帮我解决这个问题吗?我为完成这项工作付出了很多努力
我为您制作了一个程序(例如),向您展示您需要如何做。希望您了解我在此程序中编写的内容。通过此示例,您可以通过记录更改条件。我不想让你所有的工作,我只是一时冲动。祝你有美好的一天:D
Var c:char;
i,l,t:integer;
word,wordv:string;
Function CompleteWord(word,wordv:string;l:integer):boolean;
Var str:String;
Begin
for i:=l to length(word) do
Begin
t:=1;
str:=Copy(word,t,t+l);
if (str = wordv) then CompleteWord:=True;
t:=t+1;
End;
End;
Begin
c:='+';
write('Enter the part of the word desired '); read(wordv);
while c='+' do
Begin
write('Enter the next word: '); readln(word);
l:=length(word);
writeln(CompleteWord(word, wordv, l));
write('Do you want to continue? '); readln(c);
End;
write('End of the program');
readln();
End.