Sortier Programm Kürzen
-
Hallo und Guten tag
ich habe vorkurzem mit C++ angefangen und wollte mir ein Sortier Programm schreiben. Habe auch eins Fertig. Aber mein Problem ist wenn ich nun 10 oder 15 Zahlen Sortieren will wird das program ja Unendlich lang und ich muss zuviel schreiben. hat jemand ne idee wie man das kürzen kann möglich muss es ja sein =D. schon mal Danke für viele nette Beiträge.//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "CCALENDR" #pragma resource "*.dfm" int komma1=0, komma2=0, komma3=0; TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { float zahl1, zahl2, zahl3, klein, mittel, gross; zahl1 = StrToFloat(Edit1->Text); zahl2 = StrToFloat(Edit2->Text); zahl3 = StrToFloat(Edit3->Text); if((zahl1==zahl2) || (zahl1==zahl3) || (zahl2==zahl3)) { Label4->Caption="Du hast leider verloren..\n Zwei/Drei gleiche Zahlen!"; klein=zahl1; mittel=zahl2; gross=zahl3; } if((zahl1>zahl2) && (zahl1>zahl3)) { gross=zahl1; if(zahl2>zahl3){ klein=zahl3; mittel=zahl2; } if(zahl3>zahl2){ klein=zahl2; mittel=zahl3; } } if((zahl2>zahl1) && (zahl2>zahl3)) { gross=zahl2; if(zahl1>zahl3){ klein=zahl3; mittel=zahl1; } if(zahl3>zahl1){ klein=zahl1; mittel=zahl3; } } if((zahl3>zahl2) && (zahl3>zahl1)){ gross=zahl3; if(zahl2>zahl1){ klein=zahl1; mittel=zahl2; } if(zahl1>zahl2){ klein=zahl2; mittel=zahl1; } } if(RadioButton1->Checked) { Label1->Caption=FormatFloat("#,##0.####",(klein)); Label2->Caption=FormatFloat("#,##0.####",(mittel)); Label3->Caption=FormatFloat("#,##0.####",(gross)); } if(RadioButton2->Checked) { Label1->Caption=FloatToStr(gross) ; Label2->Caption=FloatToStr(mittel); Label3->Caption=FloatToStr(klein); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { Edit1->Clear(); Edit2->Clear(); Edit3->Clear(); Label1->Caption=""; Label2->Caption=""; Label3->Caption=""; Label4->Caption="Bitte Zahl eingeben. \n Bin wieder einsatzbereit"; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key) { //ASCII-Codes: 0-9 => 48-57 ; "," =>44 ; "-" => 45; BackSpace => 8 if ((Key==44) && (komma1<1)) { komma1++; if (((Key>=48) && (Key <=57)) | (Key ==44) | (Key == 45) | (Key == 8)) return; else Key = 0; } if(komma1>=1){ if (((Key>=48) && (Key <=57)) | (Key== 45) | (Key == 8)) return; else Key = 0; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit2KeyPress(TObject *Sender, char &Key) { //ASCII-Codes: 0-9 => 48-57 ; "," =>44 ; "-" => 45; BackSpace => 8 if ((Key==44) && (komma2<1)) { komma2++; if (((Key>=48) && (Key <=57)) | (Key ==44) | (Key== 45) | (Key == 8)) return; else Key = 0; } if(komma2>=1){ if (((Key>=48) && (Key <=57)) | (Key== 45) | (Key == 8)) return; else Key = 0; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit3KeyPress(TObject *Sender, char &Key) { //ASCII-Codes: 0-9 => 48-57 ; "," =>44 ; "-" => 45; BackSpace => 8 if ((Key==44) && (komma3<1)) { komma3++; if (((Key>=48) && (Key <=57)) | (Key ==44) | (Key== 45) | (Key == 8)) return; else Key = 0; } if(komma3>=1){ if (((Key>=48) && (Key <=57)) | (Key== 45) | (Key == 8)) return; else Key = 0; } } //---------------------------------------------------------------------------Gruß ZarDocK
-
Also was du machst ist ja echt umständlich
aber schon gut.
Such mal nach bubblesort oder quicksort (qsort).schirrmie
-
hmm... such mal nach array vorher, dann geht es dir vielleicht von selbst auf.
-
du könntest einen Sortieralgorithmus entwerfen für ein belibig großes Array von Zahlen ... wenn dir das für den Anfang zu komplex ist, kannst du natürlich einen der üblichen Sortieralgorithmen, die mein Vorvorredner schon angesprochen aht, nehmen.
-
am besten du nimmst dir einen der viiiielen sortieralgorithmen...
==> http://www.zfx.info/Tutorials.php?ID=79 <==
wie z.B.
Bubblesort...#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int eingabe_und_sortieren() { int liste[20]; int merker=0; int flag=0; int j=0; int i=0; srand(time(NULL)); srand(rand()); /* startwert für rand() */ cout << "alte liste:"<<endl; for(i=0;i<=20;i++) /* liste füllen und Ausgabe alte liste*/ { liste[i]=rand(); cout << liste[i] << endl; } cout << endl<< endl <<"Sortieren..."<<endl <<endl; for(int i = 0; i < 20; i++) /* sortieren */ /* wichtig ist die anzahl der durchläufe */ { for(int j = 0; j < (21 - (i + 1)); j++) /* die 21 ist eins mehr als die anzahl der zahlen */ { if(liste[j] > liste[j + 1]) { /* wenn liste[j] größer ist als [liste[j+1] */ merker = liste[j]; /* merke liste[j] */ liste[j] = liste[j + 1]; /* schreibe liste[j] in liste[j+1] */ liste[j + 1] = merker; /*schreibe merker in liste [j+1] */ } } } cout << endl << "neue liste:"<<endl; for(i=0;i<=20;i++) /* Ausgabe neue liste*/ { cout << liste[i] << endl; } return 0; } int main() /* Main - FKt */ { char c; eingabe_und_sortieren(); cin >> c; return 0; }hoffe es hilft dir ...
grüße
zen