Advertisement
CzarnyBarszcz

Zadanie 1

Oct 24th, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. //Zadanie 1
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. struct Student
  9. {
  10.     string imie,nazwisko;
  11.     unsigned int numer;//numer indeksu;
  12. };
  13. void wczytaj(int &n,Student *&student ,istream &plik)//Wczytywanie danych z pliku do tablicy dynamicznej
  14. {
  15.  
  16.     for(int i=0;i<n;i++)
  17.     {
  18.         plik>>student[i].imie;
  19.         plik>>student[i].nazwisko;
  20.         plik>>student[i].numer;
  21.     }
  22.    
  23. }
  24. void wyswietl(const int &n,Student *&student)//Wyswietlanie wszystkich studentow
  25. {
  26.     cout<<"***************************************\n";
  27.     for(int i=0;i<n;i++)
  28.     {
  29.         cout<<"Imie : "<<student[i].imie<<"\n";
  30.         cout<<"Nazwisko : "<<student[i].nazwisko<<"\n";
  31.         cout<<"Nr_indexu : "<<student[i].numer<<"\n";
  32.     }
  33.     cout<<"***************************************\n";
  34. }
  35. void wyswietl(Student *&student)//wyswietlanie pojedynczego studenta
  36. {
  37.     int a=0;
  38.     cout<<"Dane ktorego studenta chcesz wypisac? : ";
  39.     cin>>a;
  40.     a=a-1;//z racji ze tablica jest numerowana od 0 to dekrementuje zmienna a o 1
  41.     cout<<"***************************************\n";
  42.         cout<<"Imie : "<<student[a].imie<<"\n";
  43.         cout<<"Nazwisko : "<<student[a].nazwisko<<"\n";
  44.         cout<<"Nr_indexu : "<<student[a].numer<<"\n";
  45.     cout<<"***************************************\n";
  46. }
  47. Student edit(Student *&student)
  48. {
  49.     int a=0,wybor=0;
  50.     cout<<"Wybierz studenta ktorego dane chcesz edytowac : ";
  51.     cin>>a;
  52.     a-=1;
  53.     cout<<"[1] - Imie \n";
  54.     cout<<"[2] - Nazwisko \n";
  55.     cout<<"[3] - Nr_indexu \n";
  56.     cout<<"Wybierz ktory element chcesz edytowac : "<<endl;
  57.     cin>>wybor;
  58.     switch(wybor)
  59.     {
  60.         case 1:
  61.             {
  62.                 cout<<"Podaj nowe Imie : ";
  63.                 cin>>student[a].imie;
  64.                 break;
  65.             }
  66.         case 2:
  67.             {
  68.                 cout<<"Podaj nowe Nazwisko : ";
  69.                 cin>>student[a].nazwisko;
  70.                 break;
  71.             }
  72.         case 3:
  73.             {
  74.                 cout<<"Podaj nowe Nr_indexu : ";
  75.                 cin>>student[a].numer;
  76.                 break;
  77.             }
  78.     }
  79.     return student[a];
  80. }
  81. void zapisz(int &n,Student *&student)
  82. {
  83.     fstream wynik;
  84.     wynik.open("wynik.txt", ios::out);
  85.     wynik<<n<<"\n";
  86.     for(int i=0;i<n;i++)
  87.     {
  88.         wynik<<student[i].imie<<" ";
  89.         wynik<<student[i].nazwisko<<" ";
  90.         wynik<<student[i].numer<<"\n";
  91.     }
  92.    
  93.    
  94.     wynik.close();
  95. }
  96. int main()
  97. {
  98.     ifstream plik("dane.txt");
  99.     if(!plik.good())
  100.     {
  101.         cout<<"plik nie zostal wczytany"<<endl;
  102.     }
  103.     int n=0;
  104.     plik>>n;
  105.     Student * student = new Student[n];
  106.     wczytaj(n,student,plik);
  107.     wyswietl(student);
  108.     wyswietl(n,student);
  109.     edit(student);
  110.     wyswietl(n,student);
  111.     zapisz(n,student);
  112.     delete [] student;
  113.     return 0;
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement