int main() { stringset strset; SetNew(&strset, StringCompare, NULL); assert(SetAdd(&strset, "lion")); assert(SetAdd(&strset, "tiger")); assert(SetAdd(&strset, "bear")); SetAdd(&strset, "dorothy"); SetAdd(&strset, "scarecrow"); SetAdd(&strset, "witch"); SetAdd(&strset, "tin man"); SetAdd(&strset, "wizard"); assert(!SetAdd(&strset, "lion")); assert(!SetAdd(&strset, "tiger")); assert(!SetAdd(&strset, "bear")); SetPrint(&strset); printf("\nSet contains %d elements\n", SetSize(&strset)); char *dorothy = SetSearch(&strset, "dorothy"); char *elphaba = SetSearch(&strset, "elphaba"); printf("\n"); printf("Found dorothy: %s\n", dorothy == NULL ? "no" : "yes"); printf("Found elphaba: %s\n", elphaba == NULL ? "no" : "yes"); SetDestroy(&strset); return 0; }
PtSet SetCreateFile (char *pnomef) { PtSet Set; FILE *PtF; unsigned int Cardinal, I; char Elemento; /* abertura com validacao do ficheiro para leitura - opening the text file for reading */ if ( (PtF = fopen (pnomef, "r")) == NULL) { Error = NO_FILE; return NULL; } /* leitura do cardinal do conjunto do ficheiro - reading the set's cardinal from the text file */ fscanf (PtF, "%u", &Cardinal); fscanf (PtF, "%*c"); /* criação do set vazio - creating an empty set */ if ((Set = SetCreate ()) == NULL) { fclose (PtF); return NULL; } /* leitura dos elementos do conjunto do ficheiro - reading the set's elements from the text file */ for (I = 0; I < Cardinal ; I++) { fscanf (PtF, "%c", &Elemento); fscanf (PtF, "%*c"); if (!SetInsertElement (Set, Elemento)) { SetDestroy (&Set); fclose (PtF); return NULL; } } fclose (PtF); /* fecho do ficheiro - closing the text file */ Error = OK; return Set; /* devolve o set criado - returning the new set */ }
PtSet SetIntersection (PtSet pset1, PtSet pset2) { PtSet intersection; unsigned int i; PtNode currentNode; /* Verify sets */ if (!ValidSets(pset1, pset2)) return NULL; /* Calls the constructor */ if ((intersection = SetCreate()) == NULL) return NULL; /* Verifies the existance of the elements of pset1 in pset2 */ currentNode = pset1->Head; for (i = 0; i < pset1->Cardinal; i++) { if (SetSearchElement(pset2, *(currentNode->PtElem))) { if (SetInsertElement(intersection, *(currentNode->PtElem)) == 0 && Error == NO_MEM) { SetDestroy(&intersection); return NULL; } } currentNode = currentNode->PtNext; } Error = OK; return intersection; }
PtSet SetCopy (PtSet pset) { PtSet copy; unsigned int i; PtNode currentNode; /* Verifies if set exists */ if (pset == NULL) { Error = NO_SET; return NULL; } /* Call the constructor to create a new set */ if ((copy = SetCreate()) == NULL) return NULL; /* Copy all nodes and cardinal info */ currentNode = pset->Head; for (i = 0; i < pset->Cardinal; i++) { if (SetInsertElement(copy, *(currentNode->PtElem)) == 0 && Error == NO_MEM) { SetDestroy(©); break; } currentNode = currentNode->PtNext; } Error = OK; return copy; }
double ProblemAproximace1(Problem* prb, Stav* s, char** str) { double sum=0.0; double min=UINT_MAX; Set* m = SetInvertCopy(s->m); SetRemove(m, s->v); SetAdd(m, 0); //kam muze jit *str = calloc(m->count*PROBLEM_BACKTRACK, sizeof(char)); strcpy(*str, ""); int* filter = SetArray(m); int i,x,y,next=-1; x=s->v; while(m->count>0) { for(i=0; i<m->count; i++) { y=filter[i]; if(y==0 && m->count>1) continue; //do startovniho vrcholu az nakonec min = UINT_MAX; next=-1; if(prb->g->mat[x][y]>=0 && prb->g->mat[x][y]<min) { min=prb->g->mat[x][y]; next = y; } } sum+=min; if(next<0) break; SetRemove(m, next); x = next; free(filter); filter = SetArray(m); sprintf(*str, "%s%i ", *str, next); } SetDestroy(m); free(filter); return sum; }
void CMissile::Run() { double x = this->GetX(); double y = this->GetY(); x += m_speed * Screen::Instance().ElapsedTime() * DegCos(m_angle); y += m_speed * Screen::Instance().ElapsedTime() * -DegSin(m_angle); if (x - m_radius <= 0 || x + m_radius >= Screen::Instance().GetWidth() || y - m_radius <= 0 || y + m_radius >= Screen::Instance().GetHeight()) SetDestroy(true); else SetPosition(x, y); }
PtSet SetSymmetricDifference (PtSet pset1, PtSet pset2) { PtSet diff; unsigned int i; PtNode currentNode; /* Verify sets */ if (!ValidSets(pset1, pset2)) return NULL; /* Calls the constructor */ if ((diff = SetCreate()) == NULL) return NULL; /* Verifies the existance of the elements of pset1 in pset2 (and vice-versa) and save those who aren't commun */ currentNode = pset1->Head; for (i = 0; i < pset1->Cardinal; i++) { if (!SetSearchElement(pset2, *(currentNode->PtElem))) { if (SetInsertElement(diff, *(currentNode->PtElem)) == 0 && Error == NO_MEM) { SetDestroy(&diff); return NULL; } } currentNode = currentNode->PtNext; } currentNode = pset2->Head; for (i = 0; i < pset2->Cardinal; i++) { if (!SetSearchElement(pset1, *(currentNode->PtElem))) { if (SetInsertElement(diff, *(currentNode->PtElem)) == 0 && Error == NO_MEM) { SetDestroy(&diff); return NULL; } } currentNode = currentNode->PtNext; } Error = OK; return diff; }
PtSet SetReunion (PtSet pset1, PtSet pset2) { PtSet reunion; unsigned int i; PtNode currentNode; /* Verify sets */ if (!ValidSets(pset1, pset2)) return NULL; /* Calls the constructor */ if ((reunion = SetCreate()) == NULL) return NULL; /* Copy all nodes and cardinal info */ currentNode = pset1->Head; for (i = 0; i < pset1->Cardinal; i++) { if (SetInsertElement(reunion, *(currentNode->PtElem)) == 0) { if (Error == NO_MEM) { SetDestroy(&reunion); return NULL; } } currentNode = currentNode->PtNext; } currentNode = pset2->Head; for (i = 0; i < pset2->Cardinal; i++) { if (SetInsertElement(reunion, *(currentNode->PtElem)) == 0) { if (Error == NO_MEM) { SetDestroy(&reunion); return NULL; } } currentNode = currentNode->PtNext; } Error = OK; return reunion; }
double ProblemHeuristika3(Problem* prb, Stav* s) { //soucet minimalnich ohodnoceni hran vedoucich do vrcholu do kterych obch. cest. jeste nevesel double sum=0; double min; Set* m2 = SetInvertCopy(s->m); //kam muze jit Set* m1 = SetCopy(m2); //odkud muze jit SetRemove(m1, s->v); SetAdd(m1, 0); int* filter1 = SetArray(m1); int* filter2 = SetArray(m2); int i,j,x,y; for(i=0; i<m1->count; i++) { x=filter1[i]; min = UINT_MAX; for(j=0; j<m2->count; j++) { y=filter2[j]; if(prb->g->mat[y][x]>=0 && prb->g->mat[y][x]<min) min=prb->g->mat[y][x]; } if(min!=UINT_MAX) sum+=min; } SetDestroy(m1); SetDestroy(m2); free(filter1); free(filter2); return sum; }
SArray* ProblemNaslednici(Problem* prb, Stav* s) { SArray* p = SArrayCreate(ProblemStavCompare); Set* mv = SetCopy(s->m); SetAdd(mv,s->v); Stav* n; int i; for(i=0; i<prb->g->n; i++) { if(prb->g->mat[s->v][i]>=0 && (!SetIsIn(mv,i) || (i==0 && mv->count==prb->g->n))) { n = ProblemStavCreate(prb,i,mv); SArrayAdd(p, (void*) n); } } SetDestroy(mv); return p; }
void ProblemStavDestroy(Stav* s) { SetDestroy(s->m); free(s); }
int main (void) { PtSet set1 = NULL, set2 = NULL, set3 = NULL; char filename[21], car; int st; system ("clear"); printf ("\nLer conjunto do ficheiro - Read set from a text file\n"); do { printf ("Nome do ficheiro (Filename) -> "); st = scanf ("%20[^\n]", filename); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); set1 = SetCreateFile (filename); printf ("\nConjunto lido do ficheiro - Set acquired from text file %s\n", filename); WriteSet (set1); printf ("\nLer conjunto do teclado [# para terminar] - Read set from keyboard [# to stop]\n"); set2 = SetCreate (); do { do { printf ("Elemento do conjunto (Set's element) ? "); st = scanf ("%c", &car); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); car = toupper (car); if (isupper(car)) SetInsertElement (set2, car); } while (car != '#'); printf ("\nConjunto lido do teclado - Set acquired from keyboard\n"); WriteSet (set2); printf ("\nEscrever conjunto no ficheiro - Storing the set in a text file\n"); do { printf ("Nome do ficheiro (Filename) -> "); st = scanf ("%20[^\n]", filename); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); SetStoreFile (set2, filename); set3 = SetReunion (set1, set2); printf ("\nConjunto Reuniao - Union set\n"); WriteSet (set3); SetDestroy (&set3); set3 = SetIntersection (set1, set2); printf ("\nConjunto Interseccao - Intersection set\n"); WriteSet (set3); SetDestroy (&set3); set3 = SetSymmetricDifference (set1, set2); printf ("\nConjunto Diferença - Symmetric Difference set\n"); WriteSet (set3); printf ("\nDestruir os conjuntos - Releasing the sets\n"); SetDestroy (&set1); SetDestroy (&set2); SetDestroy (&set3); WriteSet (set3); return 0; }