Advertisement
majczel23000

Untitled

Jan 24th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <string>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. void wstaw(int tab[], int n){
  9.     srand( time( NULL ) );
  10.     tab[0]= rand()%n;
  11.     for(int i=1; i< n; i++)
  12.         tab[i] = tab[i-1]+rand()%n;
  13. }
  14.  
  15. void drukuj(int tab[], int n)
  16. {
  17.     for(int i= 0; i< n; i++)
  18.         cout<<tab[i]<< " ";
  19.     cout<<endl;
  20. }
  21.  
  22. int wyszukiwanieLiniowe(int tab[], int n, int s)
  23. {
  24.     int i = 0;
  25.     while(i<n && tab[i]!=s)
  26.         i+= 1;
  27.     if(i>=n)
  28.         return -1;
  29.     else
  30.         return i;
  31. }
  32.  
  33. int wyszukiwanieBinarne(int tab[], int n, int s)
  34. {
  35.     int lewy = 0;
  36.     int prawy = n-1;
  37.     bool znaleziono = false;
  38.     while(lewy<=prawy && !znaleziono)
  39.     {
  40.         int srodek = (lewy+prawy)/2;
  41.         if(tab[srodek] == s)
  42.         {
  43.             znaleziono = true;
  44.             return srodek;
  45.         }
  46.         else
  47.             if(s < tab[srodek])
  48.                 prawy = srodek - 1;
  49.             else
  50.                 lewy = srodek + 1;
  51.     }
  52.     return -1;
  53.  
  54. }
  55.  
  56. struct Student{
  57.     string indeks;
  58.     string nazwisko;
  59.     string kierunek;
  60.     string semestr;
  61.     string srednia;
  62. };
  63.  
  64. int main()
  65. {
  66.     /*const int N=20;
  67.     int tab[N], s;
  68.     wstaw(tab, N);
  69.     drukuj(tab, N);
  70.     cout<<"Podaj szukany element: ";
  71.     cin>> s;
  72.     cout<<"Index elementu (-1 gdy brak): "<<wyszukiwanieBinarne(tab, N, s);*/
  73.     fstream plik;
  74.     plik.open( "plik.txt", std::ios::in  );
  75.  
  76.     const int N = 5;
  77.     Student studenci[N];
  78.     for(int i= 0; i< N; i++)
  79.     {
  80.         getline(plik, studenci[i].indeks);
  81.         getline(plik, studenci[i].nazwisko);
  82.         getline(plik, studenci[i].kierunek);
  83.         getline(plik, studenci[i].semestr);
  84.         getline(plik, studenci[i].srednia);
  85.     }
  86.     for(int i= 0; i< N; i++)
  87.     {
  88.         cout<<studenci[i].indeks<<"\t "<<studenci[i].nazwisko<<"\t"<<studenci[i].kierunek<<"\t "
  89.             <<studenci[i].semestr<<"\t "<<studenci[i].srednia<<endl;
  90.     }
  91.     string kierunek = "";
  92.     string semestr = "";
  93.     cout<<endl;
  94.     cout<<"1. Wyszukaj kierunek i semestr"<<endl;
  95.     cout<<"2. Wyszukaj najwyzsza srednia"<<endl;
  96.     cout<<"3. Wyszukaj powyzej sredniej"<<endl;
  97.     int opcja;
  98.     cin>>opcja;
  99.     switch(opcja)
  100.     {
  101.     case 1:
  102.         cout<<"Podaj kierunek: ";
  103.         cin>>kierunek;
  104.         cout<<"Podaj semestr: ";
  105.         cin>>semestr;
  106.         for(int i= 0; i< N; i++)
  107.         {
  108.             if(studenci[i].semestr == semestr && studenci[i].kierunek==kierunek)
  109.                 cout<<studenci[i].nazwisko<<", ";
  110.         }
  111.         break;
  112.     case 2:
  113.         float max = atof(studenci[0].srednia.c_str());
  114.         for(int i= 1; i< N; i++)
  115.         {
  116.             if(atof(studenci[i].srednia.c_str())>max)
  117.                 max=atof(studenci[i].srednia.c_str());
  118.         }
  119.         cout<<endl;
  120.         cout<<"Najwyzsza srednia: "<< max<<endl;
  121.         cout<<"Studenci z taka srednia: ";
  122.         for(int i=0 ;i< N; i++)
  123.         {
  124.             if(atof(studenci[i].srednia.c_str())==max)
  125.                 cout<<studenci[i].nazwisko<<", ";
  126.         }
  127.         break;
  128.     case 3:
  129.         cout<<"Podaj srednia: ";
  130.         float srednia;
  131.         cin>>srednia;
  132.         cout<<"Studenci ze srednia powyzej podanej: "<<endl;
  133.         for(int i=0 ;i<N;i++)
  134.         {
  135.             if(atof(studenci[i].srednia.c_str())> srednia)
  136.                 cout<<studenci[i].nazwisko<<endl;
  137.         }
  138.         break;
  139.     }
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement