Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #define MAXLINESIZE 256
  5. #define MAXROWS 100
  6. #define FALSE 0
  7. #define TRUE 1
  8.  
  9. int stringLen(char row[MAXLINESIZE]){ // Prende il riempimento reale di quella riga
  10.     int i = 0;
  11.     char nowC = row[0];
  12.     while(nowC != '\0' && nowC != '\n' && i < MAXLINESIZE){
  13.         i++;
  14.         nowC = row[i];
  15.     }
  16.     return i;
  17. }
  18. int rowInMatrix(char row[MAXLINESIZE], char matrix[][MAXLINESIZE], int matrixRows){
  19.     int i;
  20.     for(i=0;i<matrixRows;i++){
  21.         int row1Len = stringLen(row);
  22.         int rowMatrixLen = stringLen(matrix[i]);
  23.         if (row1Len != rowMatrixLen) continue;
  24.         int rowsEquals = TRUE;
  25.         int k;
  26.         for(k=0;k<row1Len;k++){
  27.             if(tolower(row[k]) != tolower(matrix[i][k])){
  28.                 rowsEquals = FALSE;
  29.                 break;
  30.             }
  31.         }
  32.        
  33.         if(rowsEquals==TRUE) return TRUE;
  34.        
  35.     }
  36.     return FALSE;
  37. }
  38.  
  39. int main(){
  40.     FILE *firstFile;
  41.     FILE *secondFile;
  42.     FILE *mergeFile;
  43.     firstFile = fopen("1.txt", "r");
  44.     secondFile = fopen("2.txt", "r");
  45.     if(firstFile == NULL || secondFile == NULL){
  46.         printf("Errore!");
  47.         exit(1);
  48.     }
  49.     mergeFile = fopen("merge.txt", "w");
  50.     char firstBuffer[MAXROWS][MAXLINESIZE];
  51.     char secondBuffer[MAXROWS][MAXLINESIZE];
  52.     char mergeBuffer[MAXROWS*2][MAXLINESIZE];
  53.     char auxBuffer[MAXLINESIZE];
  54.     int i = 0, j = 0; //i, j riempimento delle due matrici
  55.     while(fgets(auxBuffer, MAXLINESIZE, firstFile) != NULL){
  56.         memcpy(firstBuffer[i++], auxBuffer, MAXLINESIZE);
  57.     }
  58.     while(fgets(auxBuffer, MAXLINESIZE, secondFile) != NULL){
  59.         memcpy(secondBuffer[j++], auxBuffer, MAXLINESIZE);
  60.     }
  61.     int iFile1 = 0, iFile2 = 0;
  62.     int k = 0;
  63.     while(1){
  64.         if(firstBuffer[iFile1][0] <= secondBuffer[iFile2][0] && iFile1 < i || (iFile1 < i && iFile2 >= j)){
  65.             //Se il nome a < b, oppure se a non < di b, quindi non toccherebbe a questo if
  66.             //ma i nomi del secondo testo sono gia' stati inseriti tutti (iFile2 >= j)
  67.             if( rowInMatrix(firstBuffer[iFile1], mergeBuffer, k) == FALSE ){
  68.                 //printf("%s, ci sta: %d \n", firstBuffer[iFile1], rowInMatrix(firstBuffer[iFile1], mergeBuffer, k) );
  69.                 memcpy(mergeBuffer[k], firstBuffer[iFile1], MAXLINESIZE);
  70.                 fputs(firstBuffer[iFile1], mergeFile);
  71.                 k++;
  72.             }
  73.             iFile1++;
  74.         }else if(secondBuffer[iFile2][0] <= firstBuffer[iFile1][0] && iFile2 < j || (iFile2 < j && iFile1 >= i) ){
  75.             // Stessa cosa, se b < a, oppure se b non < di a, quindi non toccherebbe a questo if, ma i nomi del primo sono gia' stati inseriti tutti
  76.                 //printf("%s, ci sta: %d \n", secondBuffer[iFile2], rowInMatrix(secondBuffer[iFile2], mergeBuffer, k) );
  77.             if( rowInMatrix(secondBuffer[iFile2], mergeBuffer, k) == FALSE ){
  78.                 memcpy(mergeBuffer[k], secondBuffer[iFile2], MAXLINESIZE);
  79.                 fputs(secondBuffer[iFile2], mergeFile);
  80.                 k++;
  81.             }
  82.             iFile2++;
  83.         }else{ //Entrambi hanno finito
  84.             break;
  85.         }
  86.         //printf("%s\n", mergeBuffer[k]);
  87.     }
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement