#include #include using namespace std; // STRUTTURE enum colore {ROSSO, GIALLO, VERDE}; struct elem{ // struttura per la sfera colore col; elem* pun; }; struct Cubo{ char* nome; elem* testa; }; // FUNZIONI TESTO void Inizializza(Cubo& d, char* s){ d.nome = new char[strlen(s)+1]; strcpy(d.nome, s); d.testa = NULL; } void Aggiungi(Cubo& d, colore s){ elem *p, *q; for(q=d.testa; q!=NULL; q=q->pun){ // scorri la lista p=q; } elem* r = new elem; // crei nuovo elemento r->col = s; r->pun = NULL; if(q==d.testa){ // aggiungi in testa d.testa = r; }else{ // aggiungi il mezzo p->pun = r; } } void Elimina(Cubo& d, int n){ elem* q; for(int i=1; i<=n; i++){ if(d.testa==NULL){ return; } // se non ci sono più elementi da togliere ritorna q = d.testa; d.testa = d.testa->pun; delete q; } } void SvuotaCubo(Cubo& d){ int n = 0; for(elem* p=d.testa; p!=NULL; p=p->pun){ // conta il numero di sfere nel cubo n++; } Elimina(d, n); } void AggiungiCubo(Cubo& d1, Cubo d2){ int k=strlen(d1.nome)+strlen(d2.nome); char* new_nome = new char[k+1]; strcpy(new_nome, strcat(d1.nome, d2.nome)); delete[] d1.nome; d1.nome = new_nome; for(elem* q=d2.testa; q!=NULL; q=q->pun){ Aggiungi(d1, q->col); } } void StampaCubo(Cubo& d){ cout << d.nome <<": <"; for(elem* q= d.testa; q!=NULL; q=q->pun){ switch(q->col){ case VERDE: cout <<"VERDE"; break; case GIALLO: cout <<"GIALLO"; break; case ROSSO: cout <<"ROSSO"; } if(q->pun!=NULL){ cout << ", "; } } cout << '>' << endl; }