Advertisement
Paszta

Taco - bisekcja, liniowe

Mar 24th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8. struct student
  9. {
  10.     string imie;
  11.     string nazwisko;
  12.     int ocena;
  13. };
  14.  
  15. void wczytaj_dane_studentow(string sciezka,struct student* &studenci, int &liczba_studentow);
  16. //int podzialFlagaPolski(struct student* studenci, int rozmiar);
  17. int flagapolska(struct student x[], short n);
  18. void wys (struct student x[], int p, int k);
  19. int flagafrancuska(struct student x[], short n, int *ko);
  20.  
  21. int main()
  22. {
  23.     student* st = nullptr;
  24.     int liczba_studentow;
  25.     int k,*ko, p;
  26.     ko = new int;
  27.     wczytaj_dane_studentow("studenci.csv", st, liczba_studentow);
  28.  
  29.     for (int i = 0; i< liczba_studentow; i++)
  30.         cout << st[i].imie<<" "<<st[i].nazwisko<<" "<<st[i].ocena<<endl;
  31.  
  32.     k=flagapolska( st, liczba_studentow);
  33.     cout << "Zdali studenci :\n\n";
  34.     wys (st, 0, k+1);
  35.     cout << "" << endl;
  36.     cout << "" << endl;
  37.     cout << "Nie zdali studenci :\n\n";
  38.     wys (st, k+1, liczba_studentow);
  39.  
  40.     p=flagafrancuska( st, liczba_studentow, ko);
  41.       cout << "reszta 0 :\n\n";
  42.     wys (st, 0, p+1);
  43.     cout << "" << endl;
  44.     cout << "" << endl;
  45.     cout << "reszta 1 :\n\n";
  46.     wys (st, p+1, *ko);
  47.      cout << "" << endl;
  48.     cout << "" << endl;
  49.     cout << "reszta 2 :\n\n";
  50.     wys (st, *ko, liczba_studentow);
  51.  
  52.     return 0;
  53. }
  54.  
  55. void wczytaj_dane_studentow(string sciezka,struct student* &studenci, int &liczba_studentow)
  56. {
  57.     if (studenci!=nullptr)
  58.         return;
  59.  
  60.     ifstream plik;
  61.     plik.open(sciezka);
  62.     string linia;
  63.     plik >> liczba_studentow; //wczytanie l. studentow z pliku
  64.     studenci = new student[liczba_studentow];
  65.     plik>>linia; //dokonczenie wczytywania linijki z l. studentow z pliku
  66.     string temp_ocena,temp_imie,temp_nazwisko;
  67.  
  68.     for(int i=0; i<liczba_studentow; i++)
  69.     {
  70.         plik>>linia;
  71.         istringstream ss(linia);
  72.         getline(ss, temp_imie, ';');
  73.         getline(ss, temp_nazwisko, ';');
  74.         getline(ss, temp_ocena);
  75.  
  76.         studenci[i].imie = temp_imie;
  77.         studenci[i].nazwisko = temp_nazwisko;
  78.         studenci[i].ocena = atoi(temp_ocena.c_str());
  79.     }
  80.     plik.close();
  81. }
  82.  
  83. /*int podzialFlagaPolski(struct student* studenci, int liczba_studentow){
  84. int i=0;
  85. int j=liczba_studentow-1;
  86. while(i<j){
  87.     while(studenci[i].ocena <= 10 and i<liczba_studentow-1){
  88.             i++;}
  89.     while(studenci[j].ocena > 10 and j>=0){
  90.             j--;}
  91.     if(i<j){
  92.         swap(studenci[i], studenci[j]);
  93.         i++;
  94.         j--;}
  95.     }
  96. if(studenci[i].ocena<=10){ return i+1;}
  97. else{return i;}
  98. } */
  99. int flagapolska(struct student x[], short n)
  100. {
  101.  int p=0, k=n-1;
  102.  int ind;
  103.  while(p<k)
  104.  {
  105.     while(p<n-1 && x[p].ocena>=10) p++;
  106.     while(k>=0 && x[k].ocena < 10) k--;
  107.     if(p<k)
  108.     {
  109.         swap(x[p],x[k]);
  110.         ind = k;
  111.     }
  112.    if(x[p].ocena > 10) ind=p;
  113.    else ind=p-1;
  114.  }
  115. return k;
  116. }
  117. void wys (struct student* x, int p, int k)
  118. {
  119.     for(int i=p; i<k; i++)
  120.     {
  121.         cout << "imie: " << x[i].imie << " ocena = " << x[i].ocena << endl;
  122.     }
  123. }
  124.  
  125. int flagafrancuska(struct student x[], short n, int* ko)
  126. {
  127.  int p=-1,s=0 ,k=n;
  128.  while(s<k)
  129.  {
  130.     if(x[s].ocena%3 == 0){
  131.         p++;
  132.         swap(x[p], x[s]);
  133.         s++;}
  134.     else{
  135.         if(x[s].ocena%3==2){
  136.             k--;
  137.             swap(x[k], x[s]);}
  138.             else s++;
  139.     }
  140.  
  141.  }
  142. *ko=k;
  143. return p;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement