Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #define MAXLINESIZE 256
- #define MAXROWS 100
- #define FALSE 0
- #define TRUE 1
- int stringLen(char row[MAXLINESIZE]){ // Prende il riempimento reale di quella riga
- int i = 0;
- char nowC = row[0];
- while(nowC != '\0' && nowC != '\n' && i < MAXLINESIZE){
- i++;
- nowC = row[i];
- }
- return i;
- }
- int rowInMatrix(char row[MAXLINESIZE], char matrix[][MAXLINESIZE], int matrixRows){
- int i;
- for(i=0;i<matrixRows;i++){
- int row1Len = stringLen(row);
- int rowMatrixLen = stringLen(matrix[i]);
- if (row1Len != rowMatrixLen) continue;
- int rowsEquals = TRUE;
- int k;
- for(k=0;k<row1Len;k++){
- if(tolower(row[k]) != tolower(matrix[i][k])){
- rowsEquals = FALSE;
- break;
- }
- }
- if(rowsEquals==TRUE) return TRUE;
- }
- return FALSE;
- }
- int main(){
- FILE *firstFile;
- FILE *secondFile;
- FILE *mergeFile;
- firstFile = fopen("1.txt", "r");
- secondFile = fopen("2.txt", "r");
- if(firstFile == NULL || secondFile == NULL){
- printf("Errore!");
- exit(1);
- }
- mergeFile = fopen("merge.txt", "w");
- char firstBuffer[MAXROWS][MAXLINESIZE];
- char secondBuffer[MAXROWS][MAXLINESIZE];
- char mergeBuffer[MAXROWS*2][MAXLINESIZE];
- char auxBuffer[MAXLINESIZE];
- int i = 0, j = 0; //i, j riempimento delle due matrici
- while(fgets(auxBuffer, MAXLINESIZE, firstFile) != NULL){
- memcpy(firstBuffer[i++], auxBuffer, MAXLINESIZE);
- }
- while(fgets(auxBuffer, MAXLINESIZE, secondFile) != NULL){
- memcpy(secondBuffer[j++], auxBuffer, MAXLINESIZE);
- }
- int iFile1 = 0, iFile2 = 0;
- int k = 0;
- while(1){
- if(firstBuffer[iFile1][0] <= secondBuffer[iFile2][0] && iFile1 < i || (iFile1 < i && iFile2 >= j)){
- //Se il nome a < b, oppure se a non < di b, quindi non toccherebbe a questo if
- //ma i nomi del secondo testo sono gia' stati inseriti tutti (iFile2 >= j)
- if( rowInMatrix(firstBuffer[iFile1], mergeBuffer, k) == FALSE ){
- //printf("%s, ci sta: %d \n", firstBuffer[iFile1], rowInMatrix(firstBuffer[iFile1], mergeBuffer, k) );
- memcpy(mergeBuffer[k], firstBuffer[iFile1], MAXLINESIZE);
- fputs(firstBuffer[iFile1], mergeFile);
- k++;
- }
- iFile1++;
- }else if(secondBuffer[iFile2][0] <= firstBuffer[iFile1][0] && iFile2 < j || (iFile2 < j && iFile1 >= i) ){
- // 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
- //printf("%s, ci sta: %d \n", secondBuffer[iFile2], rowInMatrix(secondBuffer[iFile2], mergeBuffer, k) );
- if( rowInMatrix(secondBuffer[iFile2], mergeBuffer, k) == FALSE ){
- memcpy(mergeBuffer[k], secondBuffer[iFile2], MAXLINESIZE);
- fputs(secondBuffer[iFile2], mergeFile);
- k++;
- }
- iFile2++;
- }else{ //Entrambi hanno finito
- break;
- }
- //printf("%s\n", mergeBuffer[k]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement