Sorting

#include <vector>
#include <ostream>

#include <algorithm>


using namespace std;

enum Razeni{Vzestupne, Sestupne};

class PraceSCisly{

public:
vector<int> cisla;

PraceSCisly(){};
~PraceSCisly(){
cisla.clear();
};

void VlozitPrvek(int prvek);
void SeradPrvky(Razeni r);
int OdstranNNasobky(int zaklad);
void OdstranDuplicitu();
void ZamichejData();
void Vypis();
string toString();
int getInt(void);

friend ostream& operator <<(ostream& os, PraceSCisly&v)
{
int i = 1;
vector<int>::iterator it = v.cisla.begin();
for (it; it != v.cisla.end(); it++){
if (i == v.cisla.size()){
os << *it << endl;
}
else{
os << *it << ", ";
}

i++;
}
return os;
}


};

bool is_integer(int k){ return std::floor(k) == k;}


void PraceSCisly::VlozitPrvek(int prvek){

// osetrit spravny datovy typ
cisla.push_back((int)prvek);
}


void PraceSCisly::SeradPrvky(Razeni r){
switch (r){
case Vzestupne:
std::sort(cisla.begin(), cisla.end());
break;
case Sestupne:
std::sort(cisla.rbegin(), cisla.rend());
break;
default:
std::sort(cisla.begin(), cisla.end());
break;
}
}

void PraceSCisly::OdstranDuplicitu(){
int posledniNactene = NULL; // pro udrzeni posledniho cisla kvuli porovnavani s nasledujicim

vector<int> novyVektor; // vektor, ktery naplnim neduplicitnimi cisly

vector<int>::iterator it = cisla.begin();
for (it; it != cisla.end(); it++){
if (posledniNactene != *it){
novyVektor.push_back(*it);
}
posledniNactene = *it;

}
cisla = novyVektor;
}

int PraceSCisly::OdstranNNasobky(int zaklad){
if (zaklad != 0){
cout << "Odstranuji nasobky cisla: " << zaklad << endl;
int pocetDuplicit = 0;
vector<int> novyVektor;
vector<int>::iterator it = cisla.begin();
for (it; it != cisla.end(); it++){
if (*it % zaklad == 0){
pocetDuplicit++;
}
else{
novyVektor.push_back(*it);
}
}
cisla = novyVektor;
cout << "Pocet odstranenych duplicit: " << pocetDuplicit << endl;
return pocetDuplicit;
}
throw exception("Deleni nulou, nepripustne \n\n");
}

void PraceSCisly::Vypis(){

cout << "\nVypis cisel: ";
if (cisla.empty()){
cout << "Nejsou zadana zadna cisla" << endl;
}else {
vector<int>::iterator it = cisla.begin();
int i = 1;

for (it; it != cisla.end(); it++){
if (i == cisla.size()){
cout << *it << endl;
}
else{
cout << *it << ", ";
}

i++;
}

}
}

void PraceSCisly::ZamichejData(){
if (!cisla.empty()){
random_shuffle(cisla.begin(), cisla.end());
}
else{
throw exception("Seznam cisel je prazdny");
}
}
Please edit your post and put the code in code tags. Highlight the code and click the "<>" button on the right of the edit window. This will make it easier to comment on the code.

Then, please tell us what your specific question is. The subject indicates it's something to do with sorting, but can you be more specific?
Topic archived. No new replies allowed.