Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include "f.h"
  4.  
  5. int main(int argc, char **argv)
  6. {
  7.     int result;
  8.     clock_t time;
  9.  
  10.     if( argc<5 )
  11.     {
  12.         fprintf(stderr, "Usage: %s a b s t\n", argv[0]);
  13.         return 1;
  14.     }
  15.  
  16.     time = clock();
  17.     result = fun(argv[1], argv[2], argv[3], argv[4]);
  18.     printf("Time: %.2lf\n", (double)(clock() - time)/CLOCKS_PER_SEC);
  19.     if( result<0 )
  20.     {
  21.         switch( result )
  22.         {
  23.         case ERROR_OPEN_IN:
  24.             fprintf(stderr, "Can not open %s!\n", argv[1]);
  25.             return 2;
  26.         case ERROR_OPEN_OUT:
  27.             fprintf(stderr, "Can not open %s!\n", argv[2]);
  28.             return 2;
  29.         case ERROR_MEMORY:
  30.             fprintf(stderr, "Can not allocate memory!\n");
  31.             return 3;
  32.         default:
  33.             fprintf(stderr, "Unknown error with code %d!\n", result);
  34.             return 4;
  35.         }
  36.     }
  37.     printf("RESULT: %d\n", result);
  38.  
  39.     return 0;
  40. }
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include "f.h"
  45.  
  46. #define LEN 1024
  47. #define SIZE_ARRAY 256
  48.  
  49. char *strstr_spcf(const char *str1, const char *str2, const int *array_no_space);
  50.  
  51. int fun(const char *input, const char *output, const char *s, const char *t)
  52. {
  53.     FILE *in, *out;
  54.     int i, counter = 0, *array_no_space;
  55.     char *buff, *c, *words;
  56.  
  57.     if( !(in = fopen(input, "r")) )
  58.         return ERROR_OPEN_IN;
  59.     if( !(out = fopen(output, "w")) )
  60.     {
  61.         fclose(in);
  62.         return ERROR_OPEN_OUT;
  63.     }
  64.  
  65.     if( !(buff = (char *)malloc(LEN)) )
  66.     {
  67.         fclose(in);
  68.         fclose(out);
  69.         return ERROR_MEMORY;
  70.     }
  71.     if( !(array_no_space = (int *)malloc(SIZE_ARRAY*sizeof(int))) )
  72.     {
  73.         free(buff);
  74.         fclose(in);
  75.         fclose(out);
  76.         return ERROR_MEMORY;
  77.     }
  78.  
  79.     for( i = 0; i<SIZE_ARRAY; i++ )
  80.     {
  81.         array_no_space[i] = 0;
  82.     }
  83.     for( c = (char *)t; *c!='\0' && *c!='\n'; c++ )
  84.     {
  85.         array_no_space[(int)( (unsigned char)*c )] = 1;
  86.     }
  87.  
  88.     while( fgets(buff, LEN, in) )
  89.     {
  90.         words = (char *)s;
  91.         while( array_no_space[(int)( (unsigned char)*words )] && *words && *words!='\n' )
  92.                 words++;
  93.  
  94.         while( *words && *words!='\n' )
  95.         {
  96.             if( strstr_spcf(buff, words, array_no_space) )
  97.             {
  98.                 counter++;
  99.                 fprintf(out, "%s\n", buff);
  100.                 break;
  101.             }
  102.  
  103.             while( !array_no_space[(int)( (unsigned char)*words )] && *words && *words!='\n' )
  104.                 words++;
  105.             while( array_no_space[(int)( (unsigned char)*words )] && *words && *words!='\n' )
  106.                 words++;
  107.         }
  108.     }
  109.  
  110.     free(array_no_space);
  111.     free(buff);
  112.     fclose(in);
  113.     fclose(out);
  114.     return counter;
  115. }
  116. char *strstr_spcf(const char *str1, const char *str2, const int *array_no_space)
  117. {
  118.         char *c1, *c1_beg, *c2;
  119.  
  120.         for( c1_beg = (char *)str1, c1 = c1_beg; *c1_beg && *c1_beg!='\n'; c1_beg++, c1 = c1_beg )
  121.         {
  122.                 if( !array_no_space[(int)( (unsigned char)*c1 )] && c1!=str1 )
  123.                         continue;
  124.  
  125.                 while( array_no_space[(int)( (unsigned char)*c1 )] && *c1 && *c1!='\n' )
  126.                         c1++;
  127.  
  128.                 c2 = (char *)str2;
  129.  
  130.                 while( *c1==*c2 )
  131.                 {
  132.                         c1++;
  133.                         c2++;
  134.  
  135.                         if( (array_no_space[(int)( (unsigned char)*c1 )] || !*c1 || *c1=='\n')
  136.                                 && (array_no_space[(int)( (unsigned char)*c2 )] || !*c2 || *c2=='\n') )
  137.                         {
  138.                                 return c1_beg;
  139.                         }
  140.                 }
  141.         }
  142.  
  143.         return NULL;
  144. }
  145. #ifndef F_H
  146. #define F_H
  147. #define ERROR_OPEN_IN (-1)
  148. #define ERROR_OPEN_OUT (-2)
  149. #define ERROR_MEMORY (-3)
  150.  
  151. int fun(const char *input, const char *output, const char *s, const char *t);
  152.  
  153. #endif
  154. program: main.o f.o
  155.         gcc -o program -Wall main.o f.o
  156. main.o: main.c f.h
  157.         gcc -o main.o -Wall -c main.c
  158. fun.o: f.c f.h
  159.         gcc -o f.o -Wall -c f.c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement