Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program uebung08;
- { Gruppe 10, Funktionen und Prozeduren :
- countWords : Zählt alle wörter im Satz
- getNthWord : Gibt Das n-te Wort aus
- getNthWordPos : Gibt die start- und endposition des n-ten wortes aus
- deletMultiples : SOLL alle doppelten wörter in einem Satz löschen }
- {$APPTYPE CONSOLE}
- {$R+,Q+,X-}
- uses
- System.SysUtils;
- function countWords(satz: string): byte;
- var
- zaehler, i: byte;
- begin
- zaehler := 0;
- if (length(satz) > 0) and (satz[1] <> ' ') then
- begin
- for i := 1 to length(satz) do
- begin
- if (satz[i] = ' ') then
- zaehler := zaehler + 1;
- end;
- zaehler := zaehler + 1;
- countWords := zaehler;
- end
- else
- countWords := 0;
- end;
- function getNthWord(satz: string; nPosition: byte): string;
- var
- zaehler: byte;
- begin
- if (countWords(satz) >= nPosition) and (nPosition > 0) then
- begin
- zaehler := 1;
- while zaehler <> nPosition do
- begin
- delete(satz, 1, pos(' ', satz));
- inc(zaehler);
- end;
- if pos(' ', satz) <> 0 then
- begin
- satz := copy(satz, 1, (pos(' ', satz)));
- for zaehler := 1 to length(satz) do
- if satz[zaehler] = ' ' then
- delete(satz, pos(' ', satz), 1);
- getNthWord := satz;
- end
- else
- getNthWord := satz;
- end
- else
- getNthWord := '';
- end;
- procedure getNthWordPos(satz: string; nPosition: byte; var pos1, pos2: byte);
- var
- i, zaehler: byte;
- begin
- { if ((nPosition <= countWords(satz)) and (nPosition > 0)) then
- begin
- pos1 := pos(getNthWord(satz,nPosition),satz);
- if nPosition = countwords(satz) then
- begin
- pos2 := length(satz);
- for i := 1 to countwords(satz)-1 do
- if satz[i] = ' ' then
- zaehler := zaehler +1;
- pos1 := pos2 - length(copy(satz,pos(getNthWord(satz,(countwords(satz)-1)),satz)))
- end;
- if nPosition < countwords(satz) then
- begin
- i:= pos1;
- repeat
- inc(i)
- until satz[i] = ' ';
- pos2:= i -1 ;
- end;
- end
- else
- begin
- pos1 := 0;
- pos2 := 0;
- end; }
- if nPosition >= 1 then
- begin
- pos1 := pos(getNthWord(satz, nPosition), satz);
- if (nPosition = countWords(satz)) then
- begin
- repeat
- inc(i);
- until (i = length(satz));
- pos2 := i;
- end
- else
- begin
- i := pos1;
- repeat
- inc(i);
- until satz[i] = ' ';
- pos2 := i - 1;
- end;
- end
- else
- begin
- pos1 := 0;
- pos2 := 0;
- end;
- // writeln('Position1: ', pos1, ' Position2: ', pos2);
- end;
- procedure deleteMultiples(satz: string);
- var
- p, o: string; //nur testweise
- i, k, posi1, posi2: byte;
- begin
- // so wie das auch testweise ist
- p := getNthWord(satz, (countWords(satz) - 1));
- o := getNthWord(satz, countWords(satz));
- while p = o do
- begin
- getNthWordPos(satz, countWords(satz), posi1, posi2);
- delete(satz, posi1, ((posi2 - posi1) + 1));
- if (satz[posi1] = ' ') then
- delete(satz, posi1, 1);
- p := getNthWord(satz, (countWords(satz) - 1));
- o := getNthWord(satz, countWords(satz));
- end;
- i := 1;
- while (i < (countWords(satz))) do
- begin
- k := 1;
- while (k <= countWords(satz)) do
- begin
- if ((i <> k) and (getNthWord(satz, i) = getNthWord(satz, k))) then
- begin
- getNthWordPos(satz, k, posi1, posi2);
- delete(satz, posi1, ((posi2 - posi1) + 1));
- if (satz[posi1] = ' ') then
- delete(satz, posi1, 1);
- k := k - 1;
- end;
- { else if (k = countWords(satz)) and (getNthWord(satz,(countwords(satz)-1)) = getNthWord(satz, countwords(satz))) then
- begin
- getNthWordPos(satz, k, { ((k = countWords(satz)) and (getNthWord(satz, (countWords(satz) - 1))
- = getNthWord(satz, countWords(satz)))) or posi1, posi2);
- delete(satz, posi1, posi2);
- if (satz[posi1] = ' ') then
- delete(satz, posi1, 1);
- k := k - 1;
- end; }
- { if ord(getNthWord(satz, countwords(satz))) = ord(getNthWord(satz,(countwords(satz)-1))) then
- begin
- getNthWordPos(satz, k, posi1, posi2);
- delete(satz, posi1, ((posi2 - posi1) + 1));
- if (satz[posi1] = ' ') then
- delete(satz, posi1, 1);
- k := k - 1;
- end; }
- k := k + 1;
- end;
- i := i + 1;
- end;
- writeln(satz);
- end;
- var
- eingabe, eingabe2: string;
- pos1, pos2: byte;
- begin
- // countwords
- writeln('writeln(countwords): ', countWords('')); // kein wort
- writeln('writeln(countwords): ', countWords('Du Arsch')); // 2 wörter
- writeln('writeln(countwords): ',
- countWords('Ich habe keine ahnung vom programmieren')); // 6 wörter
- // Getnthword
- writeln('Hallo Hallo 1 :', getNthWord('hallo Hallo', 1));
- writeln('Hallo Hallo 2:', getNthWord('hallo Hallo', 2));
- writeln('getnthword position 1:', getNthWord('Ich bin 12 jahre alt du sack',
- 1)); // satz mit 7 worten 1. wort soll ausgegeben werden
- writeln('getnthword position 7:', getNthWord('Ich bin 12 jahre alt du sack',
- 7)); // satz mit 7 worten 7.(letztes) wort soll ausgegeben werden
- writeln('getnthword position 4:', getNthWord('Ich bin 12 jahre alt du sack',
- 4)); // satz mit 7 worten 4.(bzw. irgendein mittleres) wort soll ausgegeben werden
- writeln('getnthword position 8:', getNthWord('Ich bin 12 jahre alt du sack',
- 8)); // satz mit 7 worten 8. (nicht vorhandenes) Wort soll ausgegeben werden
- writeln('getnthword position 0:', getNthWord('Ich bin 12 jahre alt du sack',
- 0)); // satz mit 7 worten 0. (nicht vorhandenes) Wort soll ausgegeben werden
- // NthWordPos
- pos1 := 1;
- pos2 := 1;
- getNthWordPos('Hallo Hallo', 2, pos1, pos2);
- writeln('Hallo Hallo :', pos1, ' ', pos2);
- getNthWordPos('Ich bin 12 jahre alt du sack', 1, pos1, pos2);
- writeln('position 1 und 2: ', pos1, ' ', pos2);
- // erstes wort (pos1: 1, pos2: 3)
- getNthWordPos('Ich bin 12 jahre alt du sack', 7, pos1, pos2);
- writeln('position 1 und 2: ', pos1, ' ', pos2);
- // letztes wort (pos1: 25, pos2: 28)
- getNthWordPos('Ich bin 12 jahre alt du sack', 3, pos1, pos2);
- writeln('position 1 und 2: ', pos1, ' ', pos2);
- // 3. wort (pos1: 9, pos2: 10)
- // Deletemultiples
- writeln('Satz: Hallo Hallo');
- deleteMultiples('Hallo Hallo');
- writeln('satz: Hallo Hallo du du du');
- deleteMultiples('Hallo Hallo du du du');
- writeln('satz: Hallo Hallo Hallo du');
- deleteMultiples('Hallo Hallo Hallo du');
- writeln('satz: Hallo Hallo due bist due Hallo due bist cool cool');
- deleteMultiples('Hallo Hallo due bist due Hallo due bist cool cool');
- readln;
- end.
- { bei delete multiples wird das vorletzte wort nicht gelöscht, wenn vorletztes und letztes gleich sind
- irgendwie geht das programm nicht in die if anweisung, obwohl es dies tun sollte. ZB. bei dem satz
- 'HALLO HALLO' i=1 (HALLO NR.1) und k=2 (HALLO NR.2) sollte die bedingung für die anweisung
- erfüllt sein, da k<>i = true und (HALLO = HALLO) = true
- soweit ist das gefixt (der hat bei getnthword noch ein leerzeichen mitgenommen)
- AAAABBBBEEEEERRRRR jetzt spackt der auch bei getnthwordpos bei dem selben fall (also
- zwei mal das selbe endwort) er gibt zb bei 'Hallo Hallo' als pos1: 1 und als pos2: 11
- (soll pos1: 7 und pos2: 11 sein da ich das zweite hallo haben will) an.
- und das liegt daran: pos1 := pos(getNthWord(satz, nPosition), satz); , nur hab ich
- kp wie ich das umschreiben soll also joa .....}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement