Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2013 18:10
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 anonymous/4684907 to your computer and use it in GitHub Desktop.
Save anonymous/4684907 to your computer and use it in GitHub Desktop.
#include "compito.h"
#include <cstring>
#include <cstdlib>
#define obj(a, b) (m[((a) - 'A') * r + (b) - 1])
Cinema::Cinema(int n, int p, const char* s){
if (n > 26 || p > 9){
cerr << "Cinema troppo grande. Termino il programma.\n";
exit(1);
}
if (strlen(s) > 30){
cerr << "Nome troppo lungo. Termino il programma.\n";
exit(1);
}
strcpy(name, s);
r = n;
c = p;
m = new bool[r * c];
for (int a = 0; a < r; ++a)
for (int b = 0; b < c; ++b)
m[a * r + b] = false;
}
ostream& operator<<(ostream& s, const Cinema& cc){
s << "Nome del cinema: " << cc.name << "\n\n ";
for (int a = 1; a <= cc.c; ++a)
s << a << ' ';
s << endl;
char z = 'A';
for (int a = 0; a < cc.r; ++a){
s << z++ << ' ';
for (int b = 0; b < cc.c; ++b)
switch (cc.m[a * cc.r + b]){
case true:
s << "* ";
break;
case false:
s << " ";
}
s << endl;
}
return s;
}
bool Cinema::prenota(char l, int j){
if (l - 'A' > r || j > c || j < 1 || l - 'A' < 0 || obj(l, j))
return false;
obj(l, j) = true;
return true;
}
bool Cinema::cancella(char l, int j){
if (l - 'A' > r)
return false;
if (j > c)
return false;
if (j < 1)
return false;
if (l - 'A' < 0)
return false;
if (!obj(l, j))
return false;
obj(l, j) = false;
return true;
}
int Cinema::operator!(){
int d = 0;
for (int a = 0; a < r * c; ++a)
if (!m[a])
++d;
return d;
}
Cinema& Cinema::operator=(const Cinema& cc){
if (this == &cc)
return *this;
strcpy(name, cc.name);
if (cc.r != r || cc.c != c){
r = cc.r; c = cc.c;
delete[] m;
m = new bool[r * c];
}
for (int a = 0; a < r * c; ++a)
m[a] = cc.m[a];
return *this;
}
Cinema& Cinema::operator+=(int k){
if (k < 1 || k > c)
return *this;
for (int a = 0; a < r; ++a)
for (int b = 0; b < c - k; ++b){
int d;
for (d = 0; d < k && !m[a * r + b + d]; ++d);
if (d == k){
for (d = 0; d < k; ++d)
m[a * r + b + d] = true;
return *this;
}
}
return *this;
}
#include <iostream>
using namespace std;
class Cinema{
bool* m;
char name[31];
int r;
int c;
public:
Cinema(int, int, const char*);
bool prenota(char, int);
bool cancella(char, int);
friend ostream& operator<<(ostream&, const Cinema&);
int operator!();
Cinema& operator=(const Cinema&);
Cinema& operator+=(int);
~Cinema() {delete[] m;};
};
#include "compito.h"
int main(){
Cinema c(4, 5, "Puzza"); //Testing constructor
cout << c; //Testing operator<<
cout << c.prenota('A', 4);
cout << c.prenota('A', 1);
cout << c.prenota('B', 2);
cout << c.prenota('B', 4);
cout << c.prenota('C', 1);
cout << c.prenota('C', 3);
cout << c.prenota('C', 5);
cout << c.prenota('D', 4);
cout << c.prenota('D', 3);
cout << endl << c;
c.cancella('C', 1); //Testing cancella
cout << c.prenota('D', 1);
cout << c.cancella('C', 5);
cout << c.cancella('D', 1) << endl;
cout << c << "Il numero di posti liberi è: " << !c << endl; //Testing operator !
Cinema c1(7, 2, "Bla");
c1 = c; //Testing operator =
cout << c1;
c1 += 2; //Testing operator +=
cout << c1;
return 0; //Testing destructor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment