Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- #include <fstream>
- using namespace std;
- void wstaw(int tab[], int n){
- srand( time( NULL ) );
- tab[0]= rand()%n;
- for(int i=1; i< n; i++)
- tab[i] = tab[i-1]+rand()%n;
- }
- void drukuj(int tab[], int n)
- {
- for(int i= 0; i< n; i++)
- cout<<tab[i]<< " ";
- cout<<endl;
- }
- int wyszukiwanieLiniowe(int tab[], int n, int s)
- {
- int i = 0;
- while(i<n && tab[i]!=s)
- i+= 1;
- if(i>=n)
- return -1;
- else
- return i;
- }
- int wyszukiwanieBinarne(int tab[], int n, int s)
- {
- int lewy = 0;
- int prawy = n-1;
- bool znaleziono = false;
- while(lewy<=prawy && !znaleziono)
- {
- int srodek = (lewy+prawy)/2;
- if(tab[srodek] == s)
- {
- znaleziono = true;
- return srodek;
- }
- else
- if(s < tab[srodek])
- prawy = srodek - 1;
- else
- lewy = srodek + 1;
- }
- return -1;
- }
- struct Student{
- string indeks;
- string nazwisko;
- string kierunek;
- string semestr;
- string srednia;
- };
- int main()
- {
- /*const int N=20;
- int tab[N], s;
- wstaw(tab, N);
- drukuj(tab, N);
- cout<<"Podaj szukany element: ";
- cin>> s;
- cout<<"Index elementu (-1 gdy brak): "<<wyszukiwanieBinarne(tab, N, s);*/
- fstream plik;
- plik.open( "plik.txt", std::ios::in );
- const int N = 5;
- Student studenci[N];
- for(int i= 0; i< N; i++)
- {
- getline(plik, studenci[i].indeks);
- getline(plik, studenci[i].nazwisko);
- getline(plik, studenci[i].kierunek);
- getline(plik, studenci[i].semestr);
- getline(plik, studenci[i].srednia);
- }
- for(int i= 0; i< N; i++)
- {
- cout<<studenci[i].indeks<<"\t "<<studenci[i].nazwisko<<"\t"<<studenci[i].kierunek<<"\t "
- <<studenci[i].semestr<<"\t "<<studenci[i].srednia<<endl;
- }
- string kierunek = "";
- string semestr = "";
- cout<<endl;
- cout<<"1. Wyszukaj kierunek i semestr"<<endl;
- cout<<"2. Wyszukaj najwyzsza srednia"<<endl;
- cout<<"3. Wyszukaj powyzej sredniej"<<endl;
- int opcja;
- cin>>opcja;
- switch(opcja)
- {
- case 1:
- cout<<"Podaj kierunek: ";
- cin>>kierunek;
- cout<<"Podaj semestr: ";
- cin>>semestr;
- for(int i= 0; i< N; i++)
- {
- if(studenci[i].semestr == semestr && studenci[i].kierunek==kierunek)
- cout<<studenci[i].nazwisko<<", ";
- }
- break;
- case 2:
- float max = atof(studenci[0].srednia.c_str());
- for(int i= 1; i< N; i++)
- {
- if(atof(studenci[i].srednia.c_str())>max)
- max=atof(studenci[i].srednia.c_str());
- }
- cout<<endl;
- cout<<"Najwyzsza srednia: "<< max<<endl;
- cout<<"Studenci z taka srednia: ";
- for(int i=0 ;i< N; i++)
- {
- if(atof(studenci[i].srednia.c_str())==max)
- cout<<studenci[i].nazwisko<<", ";
- }
- break;
- case 3:
- cout<<"Podaj srednia: ";
- float srednia;
- cin>>srednia;
- cout<<"Studenci ze srednia powyzej podanej: "<<endl;
- for(int i=0 ;i<N;i++)
- {
- if(atof(studenci[i].srednia.c_str())> srednia)
- cout<<studenci[i].nazwisko<<endl;
- }
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement