Wege, Sortierung, 4 Möglichkeiten
Von: Andreas Ott (aok@nospam.arcornews.de) [Profil]
Datum: 22.05.2009 22:50
Message-ID: <4a171003$0$31346$9b4e6d93@newsspool4.arcor-online.net>
Newsgroup: de.comp.lang.iso-c++
Datum: 22.05.2009 22:50
Message-ID: <4a171003$0$31346$9b4e6d93@newsspool4.arcor-online.net>
Newsgroup: de.comp.lang.iso-c++
http://www1.minpic.de/bild_anzeigen.php?idr043&keys473177&ende
Hallo,
ich muss eine Liste nach 4 Arten sortieren. (u.a. Mäander)
Wie sehen die Fälle 1,2,3,4 aus?
Habt Ihr konkrete Beispiele?
Gibt es einen guten Link über Alogrithmen in C++, C#
Mein Code, leider geht es nicht.
Die 4 Algorithmen benötige ich.
Wer kann helfen? Danke.
Grüße Andreas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Sort_Wege.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//
#include "stdafx.h"
#include "Sort_Wege.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Das einzige Anwendungsobjekt
CWinApp theApp;
using namespace std;
#pragma once
#include <vector>
#include <algorithm>
#include <conio.h>
#include <ctype.h>
class CSortWay
{
public:
CSortWay(){}
CSortWay(double x, double y) { X = x; Y = y; }
~CSortWay(){}
typedef std::vector<CSortWay> data;
public:
double X;
double Y;
static double SortCase1(const CSortWay var1,const CSortWay var2)
{
double x1 = var1.X;
double y1 = var1.Y;
double x2 = var2.X;
double y2 = var2.Y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
static double SortCase2(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;
double x2 = var2.X;
double y2 = var2.Y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
static double SortCase3(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;
double x2 = var2.X;
double y2 = var2.Y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
static double SortCase4(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;
double x2 = var2.X;
double y2 = var2.Y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
};
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// MFC initialisieren und drucken. Bei Fehlschlag Fehlermeldung
aufrufen.
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: Den Fehlercode an Ihre Anforderungen anpassen.
_tprintf(_T("Schwerwiegender Fehler bei der
MFC-Initialisierung\n"));
nRetCode = 1;
}
else
{
CSortWay::data mylistSortWay;
mylistSortWay.push_back(CSortWay(10,10));
mylistSortWay.push_back(CSortWay(10,20));
mylistSortWay.push_back(CSortWay(10,30));
mylistSortWay.push_back(CSortWay(10,40));
mylistSortWay.push_back(CSortWay(30,10));
mylistSortWay.push_back(CSortWay(30,20));
mylistSortWay.push_back(CSortWay(30,30));
mylistSortWay.push_back(CSortWay(30,40));
mylistSortWay.push_back(CSortWay(50,10));
mylistSortWay.push_back(CSortWay(50,20));
mylistSortWay.push_back(CSortWay(50,30));
mylistSortWay.push_back(CSortWay(50,40));
mylistSortWay.push_back(CSortWay(70,10));
mylistSortWay.push_back(CSortWay(70,20));
mylistSortWay.push_back(CSortWay(70,30));
mylistSortWay.push_back(CSortWay(70,40));
mylistSortWay.push_back(CSortWay(90,10));
mylistSortWay.push_back(CSortWay(90,20));
mylistSortWay.push_back(CSortWay(90,30));
mylistSortWay.push_back(CSortWay(90,40));
// ** Case 1
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase1);
int count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 2
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase2);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 3
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase3);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 4
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase4);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
getch();
}
return nRetCode;
}
Die Ausgabe ist FALSCH!
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 30.00, y= 10.00
Pos=5 x= 30.00, y= 20.00
Pos=6 x= 10.00, y= 40.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00
Pos=9 x= 50.00, y= 10.00
Pos x= 50.00, y= 20.00
Pos x= 50.00, y= 30.00
Pos x= 50.00, y= 40.00
Pos x= 70.00, y= 10.00
Pos x= 70.00, y= 20.00
Pos x= 70.00, y= 30.00
Pos x= 70.00, y= 40.00
Pos x= 90.00, y= 10.00
Pos x= 90.00, y= 20.00
Pos x= 90.00, y= 30.00
Pos x= 90.00, y= 40.00
[ Auf dieses Posting antworten ]Antworten
- James (25.05.2009 22:33)
- SG (26.05.2009 16:52)
- Andreas Ott (30.05.2009 08:55)
- Andreas Ott (02.06.2009 16:59)
- SG (02.06.2009 19:05)
- Andreas Ott (03.06.2009 10:33)
- Andreas Ott (03.06.2009 10:29)
- Andreas Ott (08.06.2009 12:30)
- SG (08.06.2009 16:04)
- Andreas Ott (02.06.2009 16:39)
- Nils (03.06.2009 00:48)
- Andreas Ott (04.06.2009 09:44)
