Skip to content

Instantly share code, notes, and snippets.

@jimmorrisson
Created April 10, 2016 08:56
Show Gist options
  • Save jimmorrisson/299a33481f8e78f18a52776e6b9a5516 to your computer and use it in GitHub Desktop.
Save jimmorrisson/299a33481f8e78f18a52776e6b9a5516 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
#include "Bazadanych.h"
using namespace std;
Bazadanych::Bazadanych()
{
uzywane = 0;
ladownosc = 5;
dane = new Pojazd[ladownosc];
}
Bazadanych::Bazadanych(const Bazadanych& inne)
{
uzywane = inne.uzywane;
uzywane = inne.uzywane;
dane= new Pojazd[ladownosc];
copy(inne.dane, inne.dane + uzywane, dane); //skopiuj od poczatku do konca ze starego do nowego
}
Bazadanych::~Bazadanych()
{
delete[]dane; //usuwa wskaznik
}
void Bazadanych::operator =(const Bazadanych& inne)
{
if (&inne == this)
{
return;
}
delete[]dane;
ladownosc = inne.ladownosc;
uzywane = inne.uzywane;
dane = new Pojazd[ladownosc];
copy(inne.dane, inne.dane + uzywane, dane);
}
void Bazadanych::zwieksz()
{
Pojazd *tmp;
tmp = new Pojazd[ladownosc + 5];
copy(dane, dane + uzywane, dane);
delete[]dane;
dane = tmp;
ladownosc += 5;
}
void Bazadanych::szukaj_id(int id)
{
int szukany = 0;
for (int i = 0; i < uzywane; i++)
{
if (dane[i].wez_id() == id)
{
cout << "Pojazd znaleziony! " << endl;
dane[i].wyjscie(cout);
szukany++;
}
if (szukany == 0)
{
cout << "Brak pojazdu z tym numerem id! " << endl;
}
}
}
void Bazadanych::dodaj(const Pojazd& pojazd)
{
if (uzywane >= ladownosc)
{
zwieksz();
}
dane[uzywane] = pojazd;
uzywane++;
}
void Bazadanych::wyswietl_wszystko() {
for (int i = 0; i < uzywane; i++) {
dane[i].wyjscie(cout);
}
}
void Bazadanych::usun(int id)
{
for (int i = 0; i < uzywane; i++)
{
if (dane[i].wez_id() == id)
{
dane[i] = dane[uzywane - i];
uzywane--;
}
}
}
void Bazadanych::zapisz(std::ostream& outs)
{
sort_rodzajpojazdu();
for (int i = 0; i < uzywane; i++) {
outs << dane[i];
}
}
void Bazadanych::wczytaj(std::istream& ins)
{
Pojazd tmp;
while (ins >> tmp)
{
if (uzywane >= ladownosc)
{
zwieksz();
}
dane[uzywane] = tmp;
uzywane++;
}
}
void Bazadanych::sort_rodzajpojazdu()
{
bool z = false;
Pojazd tmp;
while (!z)
{
z = true;
for (int i = 0; i<uzywane - 1; i++)
{
if (dane[i].wez_rodzajpojazdu() > dane[i + 1].wez_rodzajpojazdu())
{
z = false;
tmp = dane[i];
dane[i] = dane[i + 1];
dane[i + 1] = tmp;
}
}
}
}
#ifndef BAZADANYCH_H
#define BAZADANYCH_H
#include "pojazd.h"
class Bazadanych
{
public:
Bazadanych();
~Bazadanych();
Bazadanych(const Bazadanych& inne);
void operator =(const Bazadanych& inne);
void zmien_rozmiar();
void szukaj(std::string rodzajpojazdu);
void szukaj_id(int id);
void dodaj(const Pojazd& poj);
void wyswietl_wszystko();
void usun(int id);
void zapisz(std::ostream& outs);
void wczytaj(std::istream& ins);
void sort_rodzajpojazdu();
private:
void zwieksz();
int ladownosc;
Pojazd *dane;
int uzywane;
};
#endif
#include <iostream>
#include <fstream>
#include <string>
#include "pojazd.h"
#include "Bazadanych.h"
using namespace std;
int menu();
int main()
{
Bazadanych bazadanych;
Pojazd poj;
string nazwa_pliku;
cout << "Enter name of file (No file extension): ";
getline(cin, nazwa_pliku);
nazwa_pliku + ".txt";
ifstream fin(nazwa_pliku.c_str());
if (!fin.fail())
{
bazadanych.wczytaj(fin);
fin.close();
int wybor;
cin >> wybor;
while (wybor != 8)
{
wybor = menu();
switch (wybor)
{
case 1:
{
cin >> poj;
bazadanych.dodaj(poj);
break;
}
case 2:
{
system("cls");
string rodzajpojazdu;
cout << "wprowadz rodzaj pojazdu: ";
if (cin.peek() == '\n')cin.ignore();
getline(cin, rodzajpojazdu);
bazadanych.szukaj(rodzajpojazdu);
break;
}
case 3:
{
system("cls");
int id;
cout << "Wprowadz id pojazdu: ";
cin >> id;
bazadanych.szukaj_id(id);
break;
}
case 4:
{
system("cls");
cout << "Wszystkie pojazdy!" << endl;
bazadanych.wyswietl_wszystko();
break;
}
case 5:
{
system("cls");
int id;
cout << "Wprowadz id pojazdu do usuniecia ";
cin >> id;
bazadanych.usun(id);
break;
}
case 6:
{
break;
}
case 7:
{
system("cls");
bazadanych.sort_rodzajpojazdu();
cout << "wysortowane" << endl;
bazadanych.wyswietl_wszystko();
break;
}
case 8: {
break;
}
default: {
system("cls");
cout << "blabla" << endl;
break;
}
}
}
}
}
int menu()
{
int wybor;
cout << "1. Dodaj nowy pojazd." << endl;
cout << "2. Szukaj po rodzaju pojazdu" << endl;
cout << "3. Szukaj pojazdu po id." << endl;
cout << "4. Wyswietl wszystkie pojazdy" << endl;
cout << "5. Usun pojazd z bazy." << endl;
cout << "6. Wyjdz" << endl;
cout << "7. Sortowanie rodzaju pojazdu." << endl;
cout << "8. Wyjdz" << endl;
cin >> wybor;
return wybor;
}
#include <iostream>
#include<string>
#include "pojazd.h"
using namespace std;
Pojazd::Pojazd() //konstruktor
{
id = 0;
pojemnosc = 0;
predkoscmax = 0;
}
Pojazd::Pojazd(std::string nowy_rodzajpojazdu, int nowy_id, std::string nowy_marka, double nowy_pojemnosc, int nowy_predkoscmax) //copy construktor
{
rodzajpojazdu = nowy_rodzajpojazdu; //ustawia calosc regularnie
id = nowy_id;
marka = nowy_marka;
pojemnosc = nowy_pojemnosc;
predkoscmax = nowy_predkoscmax;
}
void Pojazd::wyjscie(ostream& outs)
{
if (outs == 'cout') //czy strumien rowny cout
{
outs << "Rodzaj pojazdu: " << rodzajpojazdu << endl;
outs << "Numer id: " << id << endl;
outs << "Marka/firma produkujaca: " << marka << endl;
outs << "Pojemnosc: " << pojemnosc << endl;
outs << "Predkosc maksymalna pojazdu: " << predkoscmax << endl;
}
else { //strumien z pliku
outs << rodzajpojazdu << endl;
outs << id << endl;
outs << marka << endl;
outs << pojemnosc << endl;
outs << predkoscmax << endl;
}
}
void Pojazd::input(istream& ins)
{
if (ins == "cin")
{
if (ins.peek() == '\n')ins.ignore(); //zapobiega uwzglednieniu przez program pustej lini w getline()
cout << "Rodzaj pojazdu: ";
getline(ins, rodzajpojazdu);
cout << "Podaj numer id: ";
ins >> id;
cout << "Podaj marke/firme produkujaca pojazd: ";
if (ins.peek() == '\n')ins.ignore();
getline(ins, marka);
cout << "Podaj pojemnosc: ";
ins >> pojemnosc;
cout << "Podaj maksymalna predkosc pojazdu: ";
ins >> predkoscmax;
}
else {
if (ins.peek() == '\n')ins.ignore();
getline(ins, rodzajpojazdu);
if (ins.peek() == '\n')ins.ignore();
ins >> id;
if (ins.peek() == '\n')ins.ignore();
getline(ins, marka);
ins >> pojemnosc;
ins >> predkoscmax;
}
}
ostream& operator <<(ostream& outs, Pojazd& tmp) //wywoluje funkcje wyzej za kazdym razem, gdy operator zostanie wywolany
{
tmp.wyjscie(outs);
return outs;
}
istream& operator >>(istream& ins, Pojazd& tmp) //wywoluje funkcje wyzej za kazdym razem, gdy operator zostanie wywolany
{
tmp.input(ins);
return ins;
}
#ifndef POJAZD_H
#define POJAZD_H
class Pojazd {
public:
Pojazd(); //konstruktor
Pojazd(std::string nowy_rodzajpojazdu, int nowy_id, std::string nowy_marka, double nowy_pojemnosc, int nowy_predkoscmax); //drugi konstruktor usun to potem
//dodaje dostep
std::string wez_rodzajpojazdu()const { return rodzajpojazdu; }
int wez_id()const { return id; }
std::string wez_marka()const { return marka; }
double wez_pojemnosc()const { return pojemnosc; }
int wez_predkoscmax()const { return predkoscmax; }
void wyjscie(std::ostream& outs); //funkcje wejscia/wyjscia
void input(std::istream& ins);
private:
std::string rodzajpojazdu;
int id;
std::string marka;
double pojemnosc;
int predkoscmax;
};
std::ostream& operator <<(std::ostream& outs, Pojazd& tmp); //dodaje operatory
std::istream& operator >>(std::istream& ins, Pojazd& tmp);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment