Skip to content

Instantly share code, notes, and snippets.

@piotrek-k
Created June 10, 2016 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrek-k/f27b47405cea45e2532bb83e58cca187 to your computer and use it in GitHub Desktop.
Save piotrek-k/f27b47405cea45e2532bb83e58cca187 to your computer and use it in GitHub Desktop.
Prosta baza danych filmów z użyciem struktur
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
using namespace std;
struct Film
{
string tytul;
string rezyser;
int lp;
string rok;
};
vector<Film> pobierzDane(){
vector<Film> dane;
ifstream odczyt("filmy.txt");
int counter=0;
Film pojedynczyFilm;
string linia;
if(odczyt){
while(getline(odczyt, linia))
{
//char a[100];
//odczyt>>a;
if(counter==1){ //tytul
pojedynczyFilm.tytul = linia;
}
else if(counter == 2){ //rezyser
pojedynczyFilm.rezyser = linia;
}
else if(counter == 3){ //rok
pojedynczyFilm.rok = linia;
dane.push_back(pojedynczyFilm);
pojedynczyFilm = Film();
counter = -1;
}
//else if(counter == 3){ //lp
// pojedynczyFilm.lp = std::atoi( a );
//}
counter++;
}
}
return dane;
}
void usun(){
}
int main(){
vector<Film> filmy = pobierzDane();
//cout << filmy[0].tytul;
cout << "Dane z bazy:" << endl;
for(int x=0; x<filmy.size(); x++)
{
cout << "------FILM: " << x+1 <<endl;
cout << "Tytul: " << filmy[x].tytul <<endl;
cout << "Rezyser: " << filmy[x].rezyser <<endl;
cout << "Rok: " << filmy[x].rok <<endl<<endl;
}
cout << endl << endl;
cout << "Komendy: " << endl;
cout << "Dopisz nowe: wpisz 1" << endl;
cout << "Usun wszystko: wpisz 2" << endl;
cout << "Dodaj nowy rekord: wpisz 3" << endl;
string komenda;
cin >> komenda;
if(komenda == "1"){
int N;
cout << "Wpisywanie nowych danych. Podaj n: " << endl;
cin >> N;
Film *listaFilmow = new Film [N];
for(int a=0; a<N; a++){
cout << "Podaj dane dotyczace filmu: " <<endl;
cout << "Tytul: " <<endl;
cin >> listaFilmow[a].tytul;
cout << "Rezyser: " <<endl;
cin >> listaFilmow[a].rezyser;
cout << "Rok: " <<endl;
cin >> listaFilmow[a].rok;
listaFilmow[a].lp = a+1;
}
ofstream plik("filmy.txt");
for(int a=0; a<N; a++){
cout << "Tytul: " << listaFilmow[a].tytul << endl;
cout << "Rezyser: " << listaFilmow[a].rezyser << endl;
cout << "Rok: " << listaFilmow[a].rok << endl;
plik << "-----------FILM " << listaFilmow[a].lp << endl;
plik << listaFilmow[a].tytul << endl;
plik << listaFilmow[a].rezyser << endl;
plik << listaFilmow[a].rok << endl;
}
plik.close();
}
else if(komenda == "2"){
ofstream plik("filmy.txt");
plik << "";
plik.close();
}
else if(komenda == "3"){
ofstream plik("filmy.txt");
for(int x=0; x<filmy.size(); x++)
{
plik << "-----------FILM " << x+1 <<endl;
plik << filmy[x].tytul <<endl;
plik << filmy[x].rezyser <<endl;
plik << filmy[x].rok <<endl;
}
plik << "-----------FILM " << filmy.size() <<endl;
string nowytytul;
cout << "Tytul nowego filmu: " << endl;
cin >> nowytytul;
plik << nowytytul <<endl;
string nowyrezyser;
cout << "Rezyser nowego filmu: " << endl;
cin >> nowyrezyser;
plik << nowyrezyser<<endl;
string nowyrok;
cout << "Rok nowego filmu: " << endl;
cin >> nowyrok;
plik << nowyrok<<endl;
plik.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment