Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. program uebung08;
  2.  
  3. { Gruppe 10, Funktionen und Prozeduren :
  4. countWords : Zählt alle wörter im Satz
  5. getNthWord : Gibt Das n-te Wort aus
  6. getNthWordPos : Gibt die start- und endposition des n-ten wortes aus
  7. deletMultiples : SOLL alle doppelten wörter in einem Satz löschen }
  8.  
  9. {$APPTYPE CONSOLE}
  10. {$R+,Q+,X-}
  11.  
  12. uses
  13. System.SysUtils;
  14.  
  15. function countWords(satz: string): byte;
  16.  
  17. var
  18. zaehler, i: byte;
  19. begin
  20.  
  21. zaehler := 0;
  22. if (length(satz) > 0) and (satz[1] <> ' ') then
  23. begin
  24. for i := 1 to length(satz) do
  25. begin
  26. if (satz[i] = ' ') then
  27. zaehler := zaehler + 1;
  28. end;
  29. zaehler := zaehler + 1;
  30. countWords := zaehler;
  31. end
  32. else
  33. countWords := 0;
  34.  
  35. end;
  36.  
  37. function getNthWord(satz: string; nPosition: byte): string;
  38.  
  39. var
  40. zaehler: byte;
  41.  
  42. begin
  43.  
  44. if (countWords(satz) >= nPosition) and (nPosition > 0) then
  45. begin
  46. zaehler := 1;
  47.  
  48. while zaehler <> nPosition do
  49. begin
  50. delete(satz, 1, pos(' ', satz));
  51. inc(zaehler);
  52. end;
  53.  
  54. if pos(' ', satz) <> 0 then
  55. begin
  56. satz := copy(satz, 1, (pos(' ', satz)));
  57. for zaehler := 1 to length(satz) do
  58. if satz[zaehler] = ' ' then
  59. delete(satz, pos(' ', satz), 1);
  60. getNthWord := satz;
  61. end
  62. else
  63. getNthWord := satz;
  64.  
  65. end
  66. else
  67. getNthWord := '';
  68.  
  69. end;
  70.  
  71. procedure getNthWordPos(satz: string; nPosition: byte; var pos1, pos2: byte);
  72. var
  73. i, zaehler: byte;
  74.  
  75. begin
  76. { if ((nPosition <= countWords(satz)) and (nPosition > 0)) then
  77. begin
  78. pos1 := pos(getNthWord(satz,nPosition),satz);
  79. if nPosition = countwords(satz) then
  80. begin
  81. pos2 := length(satz);
  82. for i := 1 to countwords(satz)-1 do
  83. if satz[i] = ' ' then
  84. zaehler := zaehler +1;
  85. pos1 := pos2 - length(copy(satz,pos(getNthWord(satz,(countwords(satz)-1)),satz)))
  86. end;
  87. if nPosition < countwords(satz) then
  88. begin
  89. i:= pos1;
  90. repeat
  91. inc(i)
  92. until satz[i] = ' ';
  93. pos2:= i -1 ;
  94. end;
  95.  
  96.  
  97. end
  98. else
  99. begin
  100. pos1 := 0;
  101. pos2 := 0;
  102. end; }
  103.  
  104. if nPosition >= 1 then
  105. begin
  106. pos1 := pos(getNthWord(satz, nPosition), satz);
  107. if (nPosition = countWords(satz)) then
  108. begin
  109. repeat
  110. inc(i);
  111. until (i = length(satz));
  112. pos2 := i;
  113. end
  114. else
  115. begin
  116. i := pos1;
  117. repeat
  118. inc(i);
  119. until satz[i] = ' ';
  120.  
  121. pos2 := i - 1;
  122. end;
  123.  
  124. end
  125. else
  126. begin
  127. pos1 := 0;
  128. pos2 := 0;
  129. end;
  130.  
  131.  
  132. // writeln('Position1: ', pos1, ' Position2: ', pos2);
  133. end;
  134.  
  135. procedure deleteMultiples(satz: string);
  136. var
  137. p, o: string; //nur testweise
  138. i, k, posi1, posi2: byte;
  139.  
  140. begin
  141. // so wie das auch testweise ist
  142. p := getNthWord(satz, (countWords(satz) - 1));
  143. o := getNthWord(satz, countWords(satz));
  144.  
  145. while p = o do
  146. begin
  147. getNthWordPos(satz, countWords(satz), posi1, posi2);
  148. delete(satz, posi1, ((posi2 - posi1) + 1));
  149. if (satz[posi1] = ' ') then
  150. delete(satz, posi1, 1);
  151. p := getNthWord(satz, (countWords(satz) - 1));
  152. o := getNthWord(satz, countWords(satz));
  153. end;
  154. i := 1;
  155. while (i < (countWords(satz))) do
  156. begin
  157. k := 1;
  158. while (k <= countWords(satz)) do
  159. begin
  160. if ((i <> k) and (getNthWord(satz, i) = getNthWord(satz, k))) then
  161. begin
  162. getNthWordPos(satz, k, posi1, posi2);
  163. delete(satz, posi1, ((posi2 - posi1) + 1));
  164. if (satz[posi1] = ' ') then
  165. delete(satz, posi1, 1);
  166. k := k - 1;
  167. end;
  168. { else if (k = countWords(satz)) and (getNthWord(satz,(countwords(satz)-1)) = getNthWord(satz, countwords(satz))) then
  169. begin
  170. getNthWordPos(satz, k, { ((k = countWords(satz)) and (getNthWord(satz, (countWords(satz) - 1))
  171. = getNthWord(satz, countWords(satz)))) or posi1, posi2);
  172. delete(satz, posi1, posi2);
  173. if (satz[posi1] = ' ') then
  174. delete(satz, posi1, 1);
  175. k := k - 1;
  176. end; }
  177.  
  178. { if ord(getNthWord(satz, countwords(satz))) = ord(getNthWord(satz,(countwords(satz)-1))) then
  179. begin
  180. getNthWordPos(satz, k, posi1, posi2);
  181. delete(satz, posi1, ((posi2 - posi1) + 1));
  182. if (satz[posi1] = ' ') then
  183. delete(satz, posi1, 1);
  184. k := k - 1;
  185. end; }
  186.  
  187. k := k + 1;
  188. end;
  189. i := i + 1;
  190. end;
  191.  
  192. writeln(satz);
  193.  
  194. end;
  195.  
  196. var
  197. eingabe, eingabe2: string;
  198. pos1, pos2: byte;
  199.  
  200. begin
  201. // countwords
  202. writeln('writeln(countwords): ', countWords('')); // kein wort
  203. writeln('writeln(countwords): ', countWords('Du Arsch')); // 2 wörter
  204. writeln('writeln(countwords): ',
  205. countWords('Ich habe keine ahnung vom programmieren')); // 6 wörter
  206.  
  207. // Getnthword
  208. writeln('Hallo Hallo 1 :', getNthWord('hallo Hallo', 1));
  209. writeln('Hallo Hallo 2:', getNthWord('hallo Hallo', 2));
  210.  
  211. writeln('getnthword position 1:', getNthWord('Ich bin 12 jahre alt du sack',
  212. 1)); // satz mit 7 worten 1. wort soll ausgegeben werden
  213. writeln('getnthword position 7:', getNthWord('Ich bin 12 jahre alt du sack',
  214. 7)); // satz mit 7 worten 7.(letztes) wort soll ausgegeben werden
  215. writeln('getnthword position 4:', getNthWord('Ich bin 12 jahre alt du sack',
  216. 4)); // satz mit 7 worten 4.(bzw. irgendein mittleres) wort soll ausgegeben werden
  217. writeln('getnthword position 8:', getNthWord('Ich bin 12 jahre alt du sack',
  218. 8)); // satz mit 7 worten 8. (nicht vorhandenes) Wort soll ausgegeben werden
  219. writeln('getnthword position 0:', getNthWord('Ich bin 12 jahre alt du sack',
  220. 0)); // satz mit 7 worten 0. (nicht vorhandenes) Wort soll ausgegeben werden
  221.  
  222. // NthWordPos
  223. pos1 := 1;
  224. pos2 := 1;
  225. getNthWordPos('Hallo Hallo', 2, pos1, pos2);
  226. writeln('Hallo Hallo :', pos1, ' ', pos2);
  227. getNthWordPos('Ich bin 12 jahre alt du sack', 1, pos1, pos2);
  228. writeln('position 1 und 2: ', pos1, ' ', pos2);
  229. // erstes wort (pos1: 1, pos2: 3)
  230. getNthWordPos('Ich bin 12 jahre alt du sack', 7, pos1, pos2);
  231. writeln('position 1 und 2: ', pos1, ' ', pos2);
  232. // letztes wort (pos1: 25, pos2: 28)
  233.  
  234. getNthWordPos('Ich bin 12 jahre alt du sack', 3, pos1, pos2);
  235. writeln('position 1 und 2: ', pos1, ' ', pos2);
  236. // 3. wort (pos1: 9, pos2: 10)
  237.  
  238. // Deletemultiples
  239. writeln('Satz: Hallo Hallo');
  240. deleteMultiples('Hallo Hallo');
  241. writeln('satz: Hallo Hallo du du du');
  242. deleteMultiples('Hallo Hallo du du du');
  243. writeln('satz: Hallo Hallo Hallo du');
  244. deleteMultiples('Hallo Hallo Hallo du');
  245. writeln('satz: Hallo Hallo due bist due Hallo due bist cool cool');
  246. deleteMultiples('Hallo Hallo due bist due Hallo due bist cool cool');
  247.  
  248. readln;
  249.  
  250. end.
  251.  
  252. { bei delete multiples wird das vorletzte wort nicht gelöscht, wenn vorletztes und letztes gleich sind
  253. irgendwie geht das programm nicht in die if anweisung, obwohl es dies tun sollte. ZB. bei dem satz
  254. 'HALLO HALLO' i=1 (HALLO NR.1) und k=2 (HALLO NR.2) sollte die bedingung für die anweisung
  255. erfüllt sein, da k<>i = true und (HALLO = HALLO) = true
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265. soweit ist das gefixt (der hat bei getnthword noch ein leerzeichen mitgenommen)
  266. AAAABBBBEEEEERRRRR jetzt spackt der auch bei getnthwordpos bei dem selben fall (also
  267. zwei mal das selbe endwort) er gibt zb bei 'Hallo Hallo' als pos1: 1 und als pos2: 11
  268. (soll pos1: 7 und pos2: 11 sein da ich das zweite hallo haben will) an.
  269. und das liegt daran: pos1 := pos(getNthWord(satz, nPosition), satz); , nur hab ich
  270. kp wie ich das umschreiben soll also joa .....}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement