Advertisement
Guest User

alguma coisa

a guest
May 27th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <conio.h>
  5.  
  6. struct Vertice
  7. {
  8.     char label;
  9.     struct Vertice *prox;
  10. };
  11.  
  12. typedef struct Vertice v[10];
  13.  
  14.  
  15. void MudarCor(int cor);
  16. void CriarVert(struct Vertice **v);
  17. struct Vertice* Criar(char label);
  18. struct Vertice* Criar(char label);
  19. int checarLink(struct Vertice **l, char p, char s);
  20. struct Vertice * inserir(struct Vertice **l, char p, char s, int pos);
  21. void imprimir(struct Vertice **l);
  22.  
  23.  
  24.  
  25. int cores[10] = {1,2,3,4,5,6,7,8,9,10};
  26. int tam = 10;
  27.  
  28.  
  29. /*void alocar(v *vertices[10], int tam);
  30.  
  31. void alocar(v *vertices[10], int tam)
  32. {
  33.     tam = 10;
  34.     int i, w = 0;
  35.     *vertices= (v*)malloc(sizeof(v));
  36.     for(i=0; i<tam; i++)
  37.     {
  38.         vertices[i]->label = (char *)malloc(tam*sizeof(char));
  39.         vertices[i]->prox = NULL;
  40.         w++;
  41.     }
  42.  
  43.  
  44. }
  45. */
  46.  
  47. int main()
  48. {
  49.     int  i;
  50.     struct Vertice *l[tam];
  51.     for(i=0; i<tam; i++)
  52.     {
  53.  
  54.         l[i] = (struct Vertice*)malloc(sizeof(struct Vertice));
  55.         l[i]->label = 'a';
  56.         l[i]->prox = NULL;
  57.     }
  58.     l[1]->label = 'A';
  59.     CriarVert(l);
  60.     MudarCor(2);
  61. //    inserirVertice();
  62.     //  inserirAresta();
  63.     printf("eeeeaaaeee\n");
  64.     return 0;
  65.  
  66.  
  67. }
  68.  
  69. void CriarVert(struct Vertice **v)
  70. {
  71.     int i, op;
  72.     char d, p, s;
  73.     i=0;
  74.     printf("\n1- Para adicionar um vertice\n2- Para adicionar ligacao\n3- Para imprimir\n");
  75.     scanf("%d", &op);
  76.     while (op == 1)
  77.     {
  78.         printf("Digite o rotulo\n");
  79.         d = getche();
  80.         v[i] = Criar(d);
  81.         i++;
  82.         printf("\n1- Para adicionar um vertice\n2- Para adicionar ligacao\n");
  83.         scanf("%d", &op);
  84.     }
  85.     while(op == 2)
  86.     {
  87.         printf("\nDigite o primeiro rotulo da ligacao: ");
  88.         p = getche();
  89.         printf("\nDigite o segundo rotulo da ligacao: ");
  90.         s = getche();
  91.         checarLink(v, p, s);
  92.         printf("\n1- Para adicionar um vertice\n2- Para adicionar ligacao\n");
  93.         scanf("%d", &op);
  94.     }
  95.     while(op == 3)
  96.     {
  97.         imprimir(v);
  98.         printf("\n1- Para adicionar um vertice\n2- Para adicionar ligacao\n");
  99.         scanf("%d", &op);
  100.     }
  101. }
  102.  
  103. int checarLink(struct Vertice **l, char p, char s)
  104. {
  105.     int i, checkP = 0, checkS = 0, posP = 0, posS = 0;
  106.     for(i=0; i<tam; i++)
  107.     {
  108.         while(l[i] != NULL)
  109.         {
  110.             if(l[i]->label == p)
  111.             {
  112.                 checkP = 1;
  113.                 posP = i;
  114.                 break;
  115.             }
  116.             if(l[i]->label == s)
  117.             {
  118.                 checkS = 1;
  119.                 posS = i;
  120.                 break;
  121.             }
  122.         }
  123.     }
  124.     if(checkS == 1 && checkP == 1)
  125.     {
  126.         inserir(l, p, s, posP);
  127.         inserir(l, s, p, posS);
  128.     }
  129.     else if(checkS == 0)
  130.     {
  131.         return printf("\nSegundo rotulo nao encontrado!\n");
  132.     }
  133.     else if(checkP == 0)
  134.     {
  135.         return printf("\nPrimeiro rotulo nao encontrado!\n");
  136.     }
  137.     else if(checkS == 0 && checkP == 0)
  138.     {
  139.         return printf("\nOs rotulos nao foram encontrados!\n");
  140.     }
  141.  
  142.     return 0;
  143.  
  144. }
  145.  
  146. struct Vertice * inserir(struct Vertice **l, char p, char s, int pos)
  147. {
  148.     int i;
  149.     struct Vertice *novo[tam];
  150.     struct Vertice *atual[tam];
  151.     if (l[pos]==NULL)
  152.     {
  153.         *novo = (struct Vertice*)malloc(sizeof (struct Vertice));
  154.         for(i=0; i<tam; i++)
  155.         {
  156.             novo[i] = NULL;
  157.         }
  158.         novo[pos]->label= p;
  159.         novo[pos]->prox = NULL;
  160.         l[pos] = novo[pos];
  161.     }
  162.     else
  163.     {
  164.         for(i=0; i<tam; i++)
  165.         {
  166.             atual[i] = (struct Vertice*)malloc(sizeof(struct Vertice));
  167.             atual[i]->label = 'a';
  168.             atual[i]->prox = NULL;
  169.         }
  170.         *atual[pos] = *l[pos];
  171.         while(atual[pos]->prox != NULL)
  172.         {
  173.             atual[pos] = atual[pos]->prox;
  174.         }
  175.         atual[pos]->label = p;
  176.         *l[pos] = *atual[pos];
  177.     }
  178.     return *l;
  179.  
  180. }
  181.  
  182. void imprimir(struct Vertice **l)
  183. {
  184.     int i = 0, j = 0;
  185.     struct Vertice *p[tam];
  186.     for(i=0; i<tam; i++)
  187.     {
  188.         p[i] = (struct Vertice*)malloc(sizeof(struct Vertice));
  189.         p[i]->label = 'a';
  190.         p[i]->prox = NULL;
  191.     }
  192.     i=0;
  193.     for(j=0; j<tam; j++)
  194.     {
  195.         printf("\n%d.o vertice %c ->", j + 1, p[j]->label);
  196.         for(p[j] = l[j]; p[j] != NULL; p[j] = p[j]->prox)
  197.         {
  198.             printf("\t%c\n", p[j]->label);
  199.         }
  200.     }
  201.     system("pause");
  202. }
  203.  
  204. struct Vertice* Criar(char label)
  205. {
  206.     int i;
  207.     struct Vertice* novo = malloc(sizeof(struct Vertice));
  208.     novo->label = label;
  209.     for (i = 0; i < 10; i++)
  210.         novo->prox = NULL;
  211.     return novo;
  212. }
  213.  
  214. void MudarCor(int cor)
  215. {
  216.     HANDLE hOut;
  217.     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  218.     SetConsoleTextAttribute(hOut, cor);
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement