Punteros Colores

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct color{ int codigo; char color[20]; } TColor,*PColor; typedef struct conjunto { PColor Arreglo; int tope; int max; } TConjunto, *PConjunto; PColor crearColor (int codigo, char color [20]) { PColor c; c=(PColor) malloc (sizeof(TColor)); c->codigo=codigo; strcpy(c->color,color); return c; } void liberarColor(PColor c){ free(c); } PConjunto crearConjuntoVacio(int n){ PConjunto A; A=(PConjunto)malloc(sizeof(TConjunto)); A->Arreglo=(PColor) malloc (sizeof(TColor)*n); A->tope=-1; A->max=n-1; return A; } void liberarConjunto(PConjunto A) { free(A->Arreglo); free(A); return; } int buscarColor (PConjunto A, int codigo) { int pos =-1; int i; for(i=0;i<=A->tope;i++) if(A->Arreglo[i].codigo==codigo)pos=i; return pos; } PConjunto insertarColor (PConjunto A, PColor c){ int pos; int i; if(A->tope==A->max)return A; pos=buscarColor(A,c->codigo); if(pos==-1){ A->tope++; A->Arreglo[A->tope]=*c; } free (c); return A; } PConjunto eliminarColor(PConjunto A, int pos) {int i; if(A->tope<pos)return A; if(A->tope==pos) { A->tope--; return A; } for(i=pos;i<A->tope;i++) {A->Arreglo[i]=A->Arreglo[i+1]; } A->tope--; return A; } int contarConjunto(PConjunto A) { return (A->tope+1); } int esConjuntoVacio(PConjunto A) { return (A->tope ==-1); } TColor copiarColor (PConjunto A, int pos) // { PColor c; if(pos <=A->tope) return A-> Arreglo[pos]; //manejo del error c = crearColor(0,""); return *c; } PConjunto unionConjunto(PConjunto A,PConjunto B) { int n,i; PConjunto G;PColor c; G=crearConjuntoVacio(100); n=contarConjunto (A); for(i=0;i<=n-1;i++) {c=crearColor(0,""); *c=copiarColor(A,i); G = insertarColor (G,c); } n=contarConjunto(B); for(i=0;i<=n-1;i++) {c=crearColor(0,""); *c=copiarColor(B,i); G=insertarColor(G,c); } return G; } PConjunto interseccionConjunto(PConjunto A, PConjunto B) {int i,n,pos; PConjunto G;PColor c; G=crearConjuntoVacio(100); n=contarConjunto(A); for(i=0;i<=n-1;i++) {pos=buscarColor(B,A->Arreglo[i].codigo); if(pos>=0) { c=crearColor(0,""); *c=copiarColor(B,pos); G=insertarColor(G,c); } } return G; } void guardarConjunto(PConjunto A,char *archivo){ FILE *f;int i; TColor tc; PColor pc; f= fopen(archivo,"wb"); for(i=0;i<=contarConjunto(A)-1;i++) { tc = copiarColor(A,i); pc =&tc; fwrite(pc,sizeof(tc),1,f); } fclose(f); return; } PConjunto cargarConjunto(char *archivo) { FILE *f; TColor tc; PColor pc; PConjunto A; A= crearConjuntoVacio(100); if(!(f=fopen(archivo,"rb"))) return NULL; while (fread(pc,sizeof(TColor),1,f)) {A=insertarColor(A,pc); } fclose(f); return A; } void imprimirColor(PColor c) { printf("......................\n"); printf("codigo:%i",c->codigo); printf(" color:%s \n",c->color); printf("\n"); } void imprimirConjunto(PConjunto A){ int i; for(i=0;i<=contarConjunto(A)-1;i++) printf("codigo:%d= Color:%s \n",A->Arreglo[i].codigo, A->Arreglo[i].color); printf("......................................\n"); } PConjunto restarConjunto(PConjunto A, PConjunto B) { int i, n, pos; PConjunto C;PColor c; C = crearConjuntoVacio(100); n = contarConjunto(A); for (i=0; i<=n-1; i++) { pos = buscarColor(B,A->Arreglo[i].codigo); if (pos==-1) {c = crearColor(0,""); *c = copiarColor(A,i); C = insertarColor(C,c); } } return C; } int main(int argc,char *argv[]) { PConjunto A,B,G,H,T,S,C,F,J,I; PColor c; A=crearConjuntoVacio(10); B=crearConjuntoVacio(10); c=crearColor(1,"blanco"); A=insertarColor(A,c); c=crearColor(2,"azul"); A=insertarColor(A,c); c=crearColor(3,"rojo"); A=insertarColor(A,c); c=crearColor(1,"amarillo"); B=insertarColor(B,c); c=crearColor(2,"azul"); B=insertarColor(B,c); c= crearColor(3,"rojo"); B= insertarColor(B,c); G= unionConjunto(A,B); H= interseccionConjunto(A,B); /* guardarConjunto(A,"Archivo"); T=cargarConjunto("Archivo"); imprimirConjunto(T); */ C = restarConjunto(A,B); F = restarConjunto(A,B); J = restarConjunto(B,A); I = interseccionConjunto(F,J); imprimirConjunto(I); imprimirConjunto(J); imprimirConjunto(A); imprimirConjunto(B); imprimirConjunto(C); system ("PAUSE"); return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.