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<4 )
- {
- fprintf(stderr, "Usage: %s a b s\n", argv[0]);
- return 1;
- }
- time = clock();
- result = fun(argv[1], argv[2], argv[3]);
- 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
- int str_comparison(const char *str1, const char *str2);
- int fun(const char *input, const char *output, const char *s)
- {
- FILE *in, *out;
- int counter = 0;
- char *buff;
- 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;
- }
- while( fgets(buff, LEN, in) )
- {
- if( !str_comparison(buff, s) )
- {
- counter++;
- fprintf(out, "%s\n", buff);
- }
- }
- free(buff);
- fclose(in);
- fclose(out);
- return counter;
- }
- int str_comparison(const char *str1, const char *str2)
- {
- char *c1, *c2;
- int flag;
- c1 = (char *)str1;
- c2 = (char *)str2;
- if( *c2=='\\' && ( *(c2 + 1)=='?' || *(c2 + 1)=='\\' ) )
- c2++;
- if( *c2 && *c2!= '\n' && *(c2 + 1)=='?' )
- flag = 1;
- else
- flag = 0;
- while( *c1 && *c1!='\n' && *c2 && *c2!='\n' )
- {
- if( flag )
- {
- if( !str_comparison(c1, c2 + 2) )
- return 0;
- if( *c1==*c2 )
- {
- c1++;
- c2 += 2;
- if( *c2 && *c2!= '\n' && *(c2 + 1)=='?' )
- flag = 1;
- else
- flag = 0;
- continue;
- }
- }
- if( *c1!=*c2 )
- return 1;
- c1++;
- c2++;
- if( *c2=='\\' && ( *(c2 + 1)=='?' || *(c2 + 1)=='\\' ) )
- c2++;
- if( *c2 && *c2!='\n' &&*(c2 + 1)=='?' )
- flag = 1;
- }
- if( (!*c1 || *c1=='\n') && *c2 && *c2!='\n' )
- {
- while( flag )
- {
- c2 += 2;
- if( !*c2 || *c2=='\n' || *(c2 + 1)!='?' )
- flag = 0;
- }
- if( *c2 && *c2!='\n' )
- return 1;
- else
- return 0;
- }
- if( *c1 && *c1!='\n' && (!*c2 || *c2=='\n') )
- return 1;
- return 0;
- }
- #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);
- #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