(Numer na liście uporządkowanej w kolejności alfabetycznej) ID NAZWISKO Imię Punkty
np:
i posortować kandydatów w zależności od otrzymanych punktów ( zaczynając od tego co ma ich najmniej )7 25653 BOROWSKI Wiktor 160
Nowoposortowana lista ma się znaleźć w pliku, którego nazwę wprowadza użytkownik w czasie działania programu.
Oto moja próba rozwiązania tego problemu
Kod: Zaznacz cały
// sorting_list.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAX_FILE_NAME_LENGTH = 100;
const int MAX_NUMBER_OF_CANDIDATES = 500;
void initialiseCstring ( char fileName[], int MAX_FILE_NAME_LENGTH );
void sort (char name[]);
int main()
{
int i;
char fileName[MAX_FILE_NAME_LENGTH];
initialiseCstring (fileName , MAX_FILE_NAME_LENGTH);
cout<< " Program sorts a list of students, in ascending order (depending of points) \n "
<< " Enter the name of the file with sorted list \n ";
for ( i = 0 ; i < MAX_FILE_NAME_LENGTH ; i++)
{
cin.get ( fileName[i]);
if (fileName[i]=='\n')
break;
}
sort ( fileName );
cout<<endl;
system("PAUSE");
return 0;
}
void initialiseCstring (char Cstring[],int maxLength )
{
int i;
for ( i = 0; i< maxLength ; i++)
Cstring[i]='\0';
}
void sort( char name[])
{
int i;
ofstream outData;
ifstream inData;
int alphabeticalPlace[MAX_NUMBER_OF_CANDIDATES];
int ID[MAX_NUMBER_OF_CANDIDATES];
int points[MAX_NUMBER_OF_CANDIDATES];
int firstOutOfOrder, location ;
int temp;
string lastName;
string firstName;
inData.open ( "list.txt" );
outData.open (name);
if (!inData)
cout<< "LOAD FAILURE \n" ;
else
cout<< "LOAD COMPLETE \n" ;
while (inData)
{
for ( i = 0 ; i < MAX_NUMBER_OF_CANDIDATES ; i++)
inData >> alphabeticalPlace[i] >> ID[i] >> lastName[i] >> firstName[i] >> points[i];
for ( firstOutOfOrder = 1 ; firstOutOfOrder < MAX_NUMBER_OF_CANDIDATES ; firstOutOfOrder++)
if (points[firstOutOfOrder] < points[firstOutOfOrder - 1 ])
{
temp = points[firstOutOfOrder];
location = firstOutOfOrder;
do
{
points[location] = points[location-1];
location--;
}
while (location > 0 && points[location - 1 ] > temp ) ;
points[location] = temp;
}
for ( i = 0 ; i < MAX_NUMBER_OF_CANDIDATES ; i++)
outData << alphabeticalPlace[i] << ID[i] << lastName[i] << firstName[i] << points[i];
cout << alphabeticalPlace[i] << ID[i] << lastName[i] << firstName[i] << points[i];
cout<<"DONE! \n";
}
inData.close();
outData.close();
}