Advertisement
filomancio

Compito.cpp

Dec 18th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include "Compito.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. Palazzo::Palazzo(int N)
  6. {
  7.    if(N<=0)
  8.      max=1;
  9.    else
  10.      max=N;
  11.    p = new bool*[max];
  12.    j=1;
  13.    p[j-1]=new bool[j];
  14.    p[j-1][j-1] =false;
  15. }
  16.  
  17.  
  18. Palazzo::~Palazzo() //destruktor
  19. {
  20.   for(int i=0; i<j; i++) //elimino le finestre
  21.     delete[] p[i];
  22.   delete[] p; //uccido il palazzo
  23. }
  24.  
  25.  
  26. Palazzo::Palazzo(const Palazzo p1)
  27. {
  28.    max=p1.max;
  29.    j=p1.j;
  30.    p = new bool*[max];
  31.    for(int i=0; i<j; i++){
  32.      p[i]= new bool[i+1];
  33.      for(int k=0; k<i+1; k++)
  34.         p[i][k]=p1.p[i][k];
  35.    }
  36. }
  37.  
  38.  
  39. bool Palazzo::aggiungi()
  40. {
  41.    if(j==max)
  42.       return false;
  43.    j++;
  44.    p[j-1]=new bool[j];
  45.    for(int k=0; k<j; k++)
  46.         p[j-1][k]=false;
  47.    return true;
  48. }
  49.  
  50.  
  51. bool Palazzo::cambia(int piano, int finestra)
  52. {
  53.    if(piano>j || piano<=0 || finestra>piano)
  54.       return false;
  55.    p[piano-1][finestra-1]=!p[piano-1][finestra-1];
  56.    return true;
  57. }
  58.  
  59.  
  60. Palazzo& operator%=(const Palazzo& p1)
  61. {
  62.    if(j!=p1.j)
  63.      return *this;
  64.    for(int i=0; i<j; i++)
  65.       for(int k=0; k<i+1; k++)
  66.          if(p[i][k] && !p1.p[i][k])
  67.         p[i][k] = false;
  68.    return *this;
  69. }
  70.  
  71.  
  72. int operator!() const
  73. {
  74.    int f=0;
  75.    for (int i=0; i<j; i++)
  76.       for(int k=0; k<i+1; k++)
  77.          if(p[i][k])
  78.             f++;
  79.    return f;
  80. }
  81.  
  82.  
  83. ostream& operator<<(ostream& os, const Palazzo& p1)
  84. {
  85.    os<<"<"<<p1.j<<">\n";
  86.    for(int i=0; i<p1.j; i++){
  87.       os<<"Piano "<<i+1<<":";
  88.       for(int k=0; k<=i; k++){
  89.          if(p1.p[i][k])
  90.             os<<" Aperta";
  91.          else
  92.             os<<" Chiusa";
  93.       }
  94.       os<<endl;
  95.    }
  96.    return os;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement