Autor Beitrag
Dhakiyah
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Do 15.10.09 10:29 
Hallo!
Ich habe einen String in dem verschiedene Buchstaben stehen.

Beispiel:
u a

Der soll jetzt sortier werden, ich weiß aber absolut nicht wie...

Außerdem soll geprüft werden, ob der Großbuchstabe F in dem String enthalten ist, wenn JA, sollen die anderen enthaltenen Buchstaben gelöscht werden.

Kann mir jemand helfen?

Gehts mit einem Bubblesort oder gibt es eine Sort-Funktion?

LG

_________________
Es ist soooo flauschig !!!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 15.10.09 10:35 
Ja, es gibt eine Sort-Funktion ... Klasse findet man, wo sie dazugehört ...

Das mit dem F findet man hier im Forum an entsprechender Position ...

Ach ja, kurze semantische Umfrage (bitte alles Ankreuzen):
[ ] Dies ist eine Hausaufgabe
[ ] Ich habe im Unterricht nicht aufgepasst
[ ] Ich seh wieder mal keinen Wald

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 15.10.09 10:42 
Wenn die Zeichen eines Strings sortiert werden sollen, kommt man um eine eigene Sortierfunktion wohl nicht drumherum. Die Methode .Sort gibt es bei Stringlisten, die sortiert dann die Liste von Strings alphabetisch.

Das einfachste wäre Bubblesort, wenn die Strings länger werden (z.B. ein Kapitel aus einem Buch), dann sollte man etwas intelligenteres wie Quicksort nehmen. Oder - hier noch besser: Bucketsort, da es ja maximal 255 unterschiedliche Werte gibt (es sei denn, ihr arbeitet mit Unicode)

_________________
We are, we were and will not be.
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Do 15.10.09 10:43 
Wenn du keine Lust hast zu antworten bzw. ANSTÄNDIG zu helfen, lass es doch einfach.
So Sprüche helfen einem nicht weiter!!!
Sorry, das ich ein Anfänger bin, ich habe im Inet gesucht, finde aber lediglich Bubble sort und Sortieren von Arrays...

DANKE :autsch:


Edit: Das mit dem 'F' habe ich jetzt. BubbleSort kucke ich mir dann mal an.

_________________
Es ist soooo flauschig !!!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 15.10.09 10:55 
Ein Bubblesort für Strings könnte so aussehen
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function SortAString(aString: String): String;
var i,j: integer;
    c: Char;
    s: string;
begin
    s := aString;
    for i := 1 to length(s) do
      for j := 1 to length(s) - 1 do
        if s[j] > s[j+1then
        begin
            c := s[j];
            s[j] := s[j+1];
            s[j+1] := c;
        end;
    result := s;
end;


Funktionalität: Ok.
Performance: Verbesserungsfähig. :mrgreen:

_________________
We are, we were and will not be.
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Do 15.10.09 11:08 
Geht nicht...

Kommt die Fehlermeldung:
Ungenügende Forward- External-Deklaration...

Ich rufe doch die Funktion dann so auf, oder?:


SortAString(s_alt);

Ist dann s_alt sortiert?

_________________
Es ist soooo flauschig !!!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 15.10.09 11:10 
String sortieren (Charweise):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
function SortString(S: String): String;
var
    I: Integer;
    C: set of Char;
    K: Char;
begin
    Result := '';
    for I := 1 To length(S) do 
        C := [S[I]] + C;
    for K := low(C) to high(C) do
        if K in C then
            Result := Result + K;
end;


@Gausi: Ihhh, quadratisch, wo das doch in linear geht :P

@Dhakiyah: Fehlermeldung markieren und F1 drücken. Hilfe lesen hilft.

Und zu meinem ersten Post: Es war durchaus ein Lösungshinweis im Post enthalten. ;-)

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Do 15.10.09 11:21 
Es geht einfach nicht :bawling: :bawling: :bawling:

Und die Hilfe hilft mir auch nicht weiter. Ich verstehs nicht... :roll:

_________________
Es ist soooo flauschig !!!
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Do 15.10.09 11:28 
hast du der unit die procedure überhaupt bekannt gemacht? (also steht das function SortString(S: String): String; irgendwo ganz am anfang von deinem quelltext?)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Do 15.10.09 11:54 
function StringSort(var s: String): String;

steht da, wo die ganzen proceduren auch stehen...

_________________
Es ist soooo flauschig !!!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 15.10.09 11:58 
Aber das sollte keine Methode der Form sein, sondern eine freie Funktion, oder? Daher war das so gedacht:

Also so

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
interface 

type Tform1 = class ...

     end;

function SortString(S: String): String// kann man auch weglassen, wenn man die Funktion selbst direkt nach Implementation hinsetzt.

implementation

function SortString(S: String): String;
var
    I: Integer;
    C: set of Char;
    K: Char;
begin
    Result := '';
    for I := 1 To length(S) do 
        C := [S[I]] + C;
    for K := low(C) to high(C) do
        if K in C then
            Result := Result + K;
end;

_________________
We are, we were and will not be.
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Do 15.10.09 12:04 
ausblenden Delphi-Quelltext
1:
for K := low(C) to high(C) do					


Inkompatible Typen

_________________
Es ist soooo flauschig !!!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 15.10.09 12:08 
Dann nimm halt meine Funktion, und nicht die 1337-Hax0r-Variante von user profile iconBenBE. :dunce:

_________________
We are, we were and will not be.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 15.10.09 12:14 
Da du Delphi 2007 hast, geht die Version von user profile iconBenBE viel einfacher so. Nebenbei fehlt dort auch der Startwert für das Set.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function SortString(S: String): String;
var
  C: set of Char;
  K: Char;
begin
  Result := '';
  C := [];
  for K in S do
    Include(C, K);
  for K in C do
    Result := Result + K;
end;
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Do 15.10.09 12:14 
Für die Grenzen bei user profile iconBenBE low(Char) bzw. low(K), high analog.

Ist aber nicht das was du willst. Glaube ich, kurze Frage daher:

Eingansstring: aabcddcc

Soll Ausgabe dann abcd sein oder aabcccdd ?

Ersteres ist BenBE, zweiteres ein *Sort.

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Do 15.10.09 12:45 
Im String stehts so drin, Beispiel:

' u a' ODER ' f a'

und es soll dann:

' a u' bzw. ' a f'

rauskommen :)

Edit: Wenn ich jetzt SortString(s_alt) aufrufe, reicht das dann oder muss ich noch was machen, damit er den neuen Wert annimmt?

_________________
Es ist soooo flauschig !!!
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 15.10.09 12:49 
Du musst den Rückgabewert auch benutzen, z.B. in deinen String wieder hineinlegen. :zwinker:
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Do 15.10.09 12:55 
ES HAT GEKLAPPT !!!
JUHUUUUUUUUUUUUUUU..................

:dance: :dance2: :dance: :dance2: :dance: :dance2: :dance:

:dunce: :party: :beer: :dunce: :beer: :party: :dunce:

_________________
Es ist soooo flauschig !!!