Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.8 kB
3
Indexable
Never
%{



#include <stdio.h>



#include <stdlib.h>



#include <string.h>







#define MAX_DATE_LEN 6



#define MAX_CUSTID_LEN 20



#define MAX_TRANS_LEN 20







char date[MAX_DATE_LEN+1];



char max_custid[MAX_CUSTID_LEN+1];



int max_val = 0;



int valid_trans_count = 0;



int invalid=0;





%}







%%



\$[ ]*[A-Z][A-Z0-9]*[ ]+([ ]*(0[1-9]|[1-2][0-9]|[30])[/](0[1-9]|[1][0-2])[ ]+[0-9]+[;])+ { 



    char custid[MAX_CUSTID_LEN+1];



  

	yytext[0]=' ';

	

	int init_size = strlen(yytext);

	char delim[] = " ";



	char *ptr = strtok(yytext, delim);

	int n=strlen(ptr);

	

	

	strncpy(custid,ptr,n);

	custid[n]='\0';

	

	ptr = strtok(NULL, delim);

	

	

	





    



    int val = 0;



	



    



    char trans[MAX_TRANS_LEN+1];



    char *p = yytext + strlen(custid) + 2; 





   while(ptr != NULL)

{

	n=strlen(ptr);

    	strncpy(trans,ptr,n);

    	

    	trans[n]='\0';

	ptr = strtok(NULL, delim);

	n=strlen(ptr);

	ptr[n-1]='\0';

	val=atoi(ptr);

	

	ptr = strtok(NULL, delim);

    	

    	

            if (strcmp(trans, date) == 0) {



                valid_trans_count++;



                if (val > max_val) {

                	

                	



                    max_val = val;

                    n=strlen(custid);



                    strncpy(max_custid, custid, n);



                    max_custid[MAX_CUSTID_LEN] = '\0'; 



                }



            }





    }

 }









\/\/.* ; 



\$[ ]*[A-Z]+[0-9]+[ ]+.*(00|3[1-9]|[4-9][0-9]|[0-9][0-9][0-9]+)[/].* {









invalid=1;



}



\$[ ]*[A-Z]+[0-9]+[ ]+.*(0[1-9]|[1-2][0-9]|30)[/](00|1[3-9]|[2-9][0-9]|[0-9][0-9][0-9]+).* {







invalid=1;



}





. ;







%%







int main(void)



{





    FILE *fp = fopen("data.txt", "r");

    FILE *file = fopen("output.txt", "w");



    if (fp == NULL) {



        fprintf(stderr, "Error opening input file\n");



        exit(EXIT_FAILURE);



    }



    yyin = fp;







    printf("Enter the date (DD/MM format): ");



    scanf("%s", date);







    yylex();



	

	if(invalid==1)

	{

	fprintf(file, "$0$#");

	fclose(fp);

	return 0;



	}	

    fprintf(file, "$%d$", valid_trans_count);



    if (valid_trans_count > 0) {



        fprintf(file, "%s", max_custid);



    }

    fprintf(file, "#");





    fclose(fp);



    return 0;



}



int yywrap(){return(1);}