Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int tamanhoArquivo(char *entrada) {
  6.     int tam = 0;
  7.     int i = 0;
  8.     FILE *arquivo = fopen(entrada, "r");
  9.  
  10.     while (!feof(arquivo)) {
  11.         tam++;
  12.         fscanf(arquivo, "%d", &i);
  13.     }
  14.  
  15.     return tam - 1;
  16. }
  17.  
  18. int *leArquivo(char *entrada, int tam, int *vetor) {
  19.     FILE *arquivo = fopen(entrada, "r");
  20.  
  21.     if (arquivo) {
  22.         int i, a;
  23.         for (i = 0; i < tam; i++) {
  24.             fscanf(arquivo, "%d", &a);
  25.             vetor[i] = a;
  26.         }
  27.     }
  28.  
  29.     return vetor;
  30. }
  31.  
  32. void escreveArquivo(char *saida, int tam, int *vetor) {
  33.     FILE *arquivo = fopen(saida, "w");
  34.     for (int i = 0; i < tam; i++){
  35.         if ((i+1) != tam) {
  36.             fprintf(arquivo, "%d ", vetor[i]);
  37.         } else {
  38.             fprintf(arquivo, "%d\n", vetor[i]);
  39.         }
  40.     }
  41.     fclose(arquivo);
  42. }
  43.  
  44. void ordena(int *vetor, int primeiro, int ultimo) {
  45.     //quicksort
  46.     int a, j, temp, i;
  47.  
  48.     if (primeiro < ultimo) {
  49.         a = primeiro;
  50.         i = primeiro;
  51.         j = ultimo;
  52.  
  53.         while (i < j) {
  54.             while (vetor[i] <= vetor[a] && i < ultimo) i++;
  55.             while (vetor[j] > vetor[a]) j--;
  56.             if (i < j) {
  57.                 temp = vetor[i];
  58.                 vetor[i] = vetor[j];
  59.                 vetor[j] = temp;
  60.             }
  61.         }
  62.  
  63.         temp = vetor[a];
  64.         vetor[a] = vetor[j];
  65.         vetor[j] = temp;
  66.         ordena(vetor, primeiro, j - 1);
  67.         ordena(vetor, j + 1, ultimo);
  68.     }
  69. }
  70.  
  71. int removeRepeticoes(int tam, int *vetor) {
  72.     int i, j;
  73.     j = 0;
  74.  
  75.     int vetorAux[tam];
  76.     int tamAux = tam;
  77.  
  78.     //determina o tamanho do vetor final e remove as repeticoes
  79.  
  80.     for (i = 0; i < tam; i++) {
  81.         if ((i + 1) != tam) {
  82.             if( vetor[i] != vetor[i+1]){
  83.                 vetorAux[j] = vetor[i];
  84.                 j++;
  85.             } else {
  86.                 tamAux--;
  87.             }
  88.         } else {
  89.             vetorAux[j] = vetor[i];
  90.             break;
  91.         }
  92.     }
  93.  
  94.     for (i = 0; i < tamAux; i++){
  95.         vetor[i]=vetorAux[i];
  96.     }
  97.     return tamAux ;
  98. }
  99.  
  100. int *concatena(int tam1, int *vetor1, int tam2, int *vetor2) {
  101.     int tam3 = tam1 + tam2;
  102.  
  103.     /*
  104.      * Aloca o espaço de memória onde o conteúdo da concatenacao vai ser armazenado
  105.      */
  106.     int *vetorAux;
  107.     vetorAux = (int *) malloc((tam3) * sizeof(int));
  108.  
  109.  
  110.     int i;
  111.  
  112.     //concatena os dois vetores no vetorAux
  113.  
  114.     for (i = 0; i < tam1; i++) {
  115.         vetorAux[i] = vetor1[i];
  116.     }
  117.     for (i = tam1; i < tam3; i++) {
  118.         vetorAux[i] = vetor2[i - tam1];
  119.     }
  120.  
  121.     ordena(vetorAux, 0, tam3-1);
  122.     return vetorAux;
  123. }
  124.  
  125. int main(int argc, char **argv) {
  126.     /* Verifica o numero de argumentos e encerra o programa caso não esteja dentro do
  127.      * projetado
  128.      */
  129.     argv[1] = "/Users/liquuid/devel/ep1-ac/l1.txt";
  130.     argv[2] = "/Users/liquuid/devel/ep1-ac/l2.txt";
  131.     argv[3] = "/Users/liquuid/devel/ep1-ac/l3.txt";
  132.  
  133.     if (argc != 4) {
  134.         printf("Numero de parametros incorreto, exemplo: \n %s array1.txt array2.txt saida.txt\n", argv[0]);
  135.         return 1;
  136.     }
  137.  
  138.     char *entrada1 = argv[1];
  139.     char *entrada2 = argv[2];
  140.     char *saida = argv[3];
  141.  
  142.     int *vetor1, *vetor2, tam1, tam2, *vetorSaida;
  143.  
  144.     tam1 = tamanhoArquivo(entrada1);
  145.     tam2 = tamanhoArquivo(entrada2);
  146.  
  147.     vetor1 = (int *) malloc(tam1 * sizeof(int));
  148.     vetor2 = (int *) malloc(tam2 * sizeof(int));
  149.  
  150.  
  151.     leArquivo(entrada1, tam1, vetor1);
  152.     leArquivo(entrada2, tam2, vetor2);
  153.  
  154.     tam1 = removeRepeticoes(tam1, vetor1);
  155.     tam2 = removeRepeticoes(tam2, vetor2);
  156.  
  157.     vetorSaida = concatena(tam1, vetor1, tam2, vetor2);
  158.  
  159.     int tamFinal = removeRepeticoes(tam1 + tam2, vetorSaida);
  160.  
  161.     escreveArquivo(saida, tamFinal, vetorSaida);
  162.     //getch();
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement