Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include "f.h"
- int main(int argc, char **argv)
- {
- int result;
- clock_t time;
- if( argc<5 )
- {
- fprintf(stderr, "Usage: %s a b s t\n", argv[0]);
- return 1;
- }
- time = clock();
- result = fun(argv[1], argv[2], argv[3], argv[4]);
- printf("Time: %.2lf\n", (double)(clock() - time)/CLOCKS_PER_SEC);
- if( result<0 )
- {
- switch( result )
- {
- case ERROR_OPEN_IN:
- fprintf(stderr, "Can not open %s!\n", argv[1]);
- return 2;
- case ERROR_OPEN_OUT:
- fprintf(stderr, "Can not open %s!\n", argv[2]);
- return 2;
- case ERROR_MEMORY:
- fprintf(stderr, "Can not allocate memory!\n");
- return 3;
- default:
- fprintf(stderr, "Unknown error with code %d!\n", result);
- return 4;
- }
- }
- printf("RESULT: %d\n", result);
- return 0;
- }
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "f.h"
- #define LEN 1024
- #define SIZE_ARRAY 256
- char *strstr_spcf(const char *str1, const char *str2, const int *array_no_space);
- int fun(const char *input, const char *output, const char *s, const char *t)
- {
- FILE *in, *out;
- int i, counter = 0, *array_no_space;
- char *buff, *c, *words;
- if( !(in = fopen(input, "r")) )
- return ERROR_OPEN_IN;
- if( !(out = fopen(output, "w")) )
- {
- fclose(in);
- return ERROR_OPEN_OUT;
- }
- if( !(buff = (char *)malloc(LEN)) )
- {
- fclose(in);
- fclose(out);
- return ERROR_MEMORY;
- }
- if( !(array_no_space = (int *)malloc(SIZE_ARRAY*sizeof(int))) )
- {
- free(buff);
- fclose(in);
- fclose(out);
- return ERROR_MEMORY;
- }
- for( i = 0; i<SIZE_ARRAY; i++ )
- {
- array_no_space[i] = 0;
- }
- for( c = (char *)t; *c!='\0' && *c!='\n'; c++ )
- {
- array_no_space[(int)( (unsigned char)*c )] = 1;
- }
- while( fgets(buff, LEN, in) )
- {
- words = (char *)s;
- while( array_no_space[(int)( (unsigned char)*words )] && *words && *words!='\n' )
- words++;
- while( *words && *words!='\n' )
- {
- if( strstr_spcf(buff, words, array_no_space) )
- {
- counter++;
- fprintf(out, "%s\n", buff);
- break;
- }
- while( !array_no_space[(int)( (unsigned char)*words )] && *words && *words!='\n' )
- words++;
- while( array_no_space[(int)( (unsigned char)*words )] && *words && *words!='\n' )
- words++;
- }
- }
- free(array_no_space);
- free(buff);
- fclose(in);
- fclose(out);
- return counter;
- }
- char *strstr_spcf(const char *str1, const char *str2, const int *array_no_space)
- {
- char *c1, *c1_beg, *c2;
- for( c1_beg = (char *)str1, c1 = c1_beg; *c1_beg && *c1_beg!='\n'; c1_beg++, c1 = c1_beg )
- {
- if( !array_no_space[(int)( (unsigned char)*c1 )] && c1!=str1 )
- continue;
- while( array_no_space[(int)( (unsigned char)*c1 )] && *c1 && *c1!='\n' )
- c1++;
- c2 = (char *)str2;
- while( *c1==*c2 )
- {
- c1++;
- c2++;
- if( (array_no_space[(int)( (unsigned char)*c1 )] || !*c1 || *c1=='\n')
- && (array_no_space[(int)( (unsigned char)*c2 )] || !*c2 || *c2=='\n') )
- {
- return c1_beg;
- }
- }
- }
- return NULL;
- }
- #ifndef F_H
- #define F_H
- #define ERROR_OPEN_IN (-1)
- #define ERROR_OPEN_OUT (-2)
- #define ERROR_MEMORY (-3)
- int fun(const char *input, const char *output, const char *s, const char *t);
- #endif
- program: main.o f.o
- gcc -o program -Wall main.o f.o
- main.o: main.c f.h
- gcc -o main.o -Wall -c main.c
- fun.o: f.c f.h
- gcc -o f.o -Wall -c f.c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement