4
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class Patnik{
private:
char name[100];
int wagonClass; //1 or 2
bool bike;
public:
Patnik() {
name[0] = '\0';
wagonClass = 1;
bike = false;
}
Patnik(const char *_name, int _wagonClass, bool _bike) {
strcpy(name,_name);
wagonClass = _wagonClass;
bike = _bike;
}
~Patnik() {}
Patnik(const Patnik &p) {
strcpy(name,p.name);
wagonClass = p.wagonClass;
bike = p.bike;
}
Patnik &operator=(const Patnik &p) {
if (this!=&p) {
strcpy(name,p.name);
wagonClass = p.wagonClass;
bike = p.bike;
return *this;
}
}
friend ostream &operator<<(ostream &os, const Patnik &p);
bool getBike()const{return bike;}
int getClass(){return wagonClass;}
};
ostream &operator<<(ostream &os, const Patnik &p) {
os << p.name << endl;
os << p.wagonClass << endl;
os << p.bike << endl;
return os;
}
class Voz{
private:
char destination[100];
Patnik *patnici;
int numberOfP;
int numberOfBikes;
public:
Voz() {
destination[0] = '\0';
patnici = nullptr;
numberOfP = 0;
numberOfBikes = 0;
}
Voz(const char *_destination, int _numberOfBikes) {
strcpy(destination,_destination);
numberOfBikes = _numberOfBikes;
numberOfP = 0;
patnici = nullptr;
}
~Voz() {
delete[] patnici;
}
int getCurrentBikes() {
int sum = 0;
for (int i = 0; i<numberOfP; i++) {
if (patnici[i].getBike())
sum++;
}
return sum;
}
Voz &operator+=(const Patnik &p) {
if (!p.getBike() or numberOfBikes>0) {
Patnik *temp = new Patnik[numberOfP+1];
for (int i = 0; i<numberOfP; i++) {
temp[i] = patnici[i];
}
temp[numberOfP] = p;
if (patnici!=nullptr)
delete[] patnici;
patnici = temp;
numberOfP++;
}
return *this;
}
friend ostream &operator<<(ostream &os, const Voz &v);
void patniciNemaMesto() {
int classA = 0;
int classB = 0;
int max = numberOfBikes;
for (int i = 0; i<numberOfP; i++) {
if (patnici[i].getClass()==1 and patnici[i].getBike())
classA++;
else if (patnici[i].getClass()==2 and patnici[i].getBike())
classB++;
}
if (max >= classA) {
max -= classA;
classA = 0;
if (max>=classB)
classB=0;
else
classB-=max;
}
else {
classA -= max;
}
cout<<"Brojot na patnici od 1-va klasa koi ostanale bez mesto e: "<<classA<<endl;
cout<<"Brojot na patnici od 2-ra klasa koi ostanale bez mesto e: "<<classB<<endl;
}
};
ostream &operator<<(ostream &os, const Voz &v) {
os <<v.destination<<endl;
for (int i = 0; i<v.numberOfP; i++) {
os << v.patnici[i];
os << endl;
}
return os;
}
int main() {
Patnik p;
char ime[100], destinacija[100];
int n;
bool velosiped;
int klasa;
int maxv;
cin >> destinacija >> maxv;
cin >> n;
Voz v(destinacija, maxv);
//cout<<v<<endl;
for (int i = 0; i < n; i++){
cin >> ime >> klasa >> velosiped;
Patnik p(ime, klasa, velosiped);
//cout<<p<<endl;
v += p;
}
cout << v;
v.patniciNemaMesto();
return 0;
}Editor is loading...
Leave a Comment