Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 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<4 )
  11.     {
  12.         fprintf(stderr, "Usage: %s a b s\n", argv[0]);
  13.         return 1;
  14.     }
  15.  
  16.     time = clock();
  17.     result = fun(argv[1], argv[2], argv[3]);
  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.  
  48. int str_comparison(const char *str1, const char *str2);
  49.  
  50. int fun(const char *input, const char *output, const char *s)
  51. {
  52.     FILE *in, *out;
  53.     int counter = 0;
  54.     char *buff;
  55.  
  56.     if( !(in = fopen(input, "r")) )
  57.         return ERROR_OPEN_IN;
  58.     if( !(out = fopen(output, "w")) )
  59.     {
  60.         fclose(in);
  61.         return ERROR_OPEN_OUT;
  62.     }
  63.  
  64.     if( !(buff = (char *)malloc(LEN)) )
  65.     {
  66.         fclose(in);
  67.         fclose(out);
  68.         return ERROR_MEMORY;
  69.     }
  70.  
  71.     while( fgets(buff, LEN, in) )
  72.     {
  73.         if( !str_comparison(buff, s) )
  74.         {
  75.             counter++;
  76.             fprintf(out, "%s\n", buff);
  77.         }
  78.     }
  79.  
  80.     free(buff);
  81.     fclose(in);
  82.     fclose(out);
  83.     return counter;
  84. }
  85.  
  86. int str_comparison(const char *str1, const char *str2)
  87. {
  88.     char *c1, *c2;
  89.     int flag;
  90.  
  91.     c1 = (char *)str1;
  92.     c2 = (char *)str2;
  93.     if( *c2=='\\' && ( *(c2 + 1)=='?' || *(c2 + 1)=='\\' ) )
  94.         c2++;
  95.     if( *c2 && *c2!= '\n' && *(c2 + 1)=='?' )
  96.         flag = 1;
  97.     else
  98.         flag = 0;
  99.     while( *c1 && *c1!='\n' && *c2 && *c2!='\n' )
  100.     {
  101.         if( flag )
  102.         {
  103.             if( !str_comparison(c1, c2 + 2) )
  104.                 return 0;
  105.             if( *c1==*c2 )
  106.             {
  107.                 c1++;
  108.                 c2 += 2;
  109.                 if( *c2 && *c2!= '\n' && *(c2 + 1)=='?' )
  110.                     flag = 1;
  111.                 else
  112.                     flag = 0;
  113.  
  114.                 continue;
  115.             }
  116.         }
  117.  
  118.         if( *c1!=*c2 )
  119.             return 1;
  120.  
  121.         c1++;
  122.         c2++;
  123.  
  124.         if( *c2=='\\' && ( *(c2 + 1)=='?' || *(c2 + 1)=='\\' ) )
  125.             c2++;
  126.         if( *c2 && *c2!='\n' &&*(c2 + 1)=='?' )
  127.             flag = 1;
  128.     }
  129.  
  130.     if( (!*c1 || *c1=='\n') && *c2 && *c2!='\n' )
  131.     {
  132.         while( flag )
  133.         {
  134.             c2 += 2;
  135.             if( !*c2 || *c2=='\n' || *(c2 + 1)!='?' )
  136.                 flag = 0;
  137.         }
  138.  
  139.         if( *c2 && *c2!='\n' )
  140.             return 1;
  141.         else
  142.             return 0;
  143.     }
  144.     if( *c1 && *c1!='\n' && (!*c2 || *c2=='\n') )
  145.         return 1;
  146.     return 0;
  147. }
  148. #ifndef F_H
  149. #define F_H
  150. #define ERROR_OPEN_IN (-1)
  151. #define ERROR_OPEN_OUT (-2)
  152. #define ERROR_MEMORY (-3)
  153.  
  154. int fun(const char *input, const char *output, const char *s);
  155.  
  156. #endif
  157. program: main.o f.o
  158.         gcc -o program -Wall main.o f.o
  159. main.o: main.c f.h
  160.         gcc -o main.o -Wall -c main.c
  161. fun.o: f.c f.h
  162.         gcc -o f.o -Wall -c f.c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement