Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct {
- int y;
- int m;
- int d;
- int hh;
- int mm;
- } DateTime;
- struct SportType {
- char* name;
- int classical; // ??as??? 1 ? s??????? 0
- int individual; // at?µ??? 1 ? ?µad??? 0
- char *participant1;
- char *participant2;
- };
- struct Event {
- char *eventTitle;
- char *organizer;
- char *eventPlace;
- DateTime xronos;
- int official; // ep?s?µ?? 1 ? f?????? 0 ?a?a?t??a
- struct SportType *sport;
- struct Event *next;
- };
- typedef struct Event *EventType;
- typedef struct {
- char *title;
- char *description;
- int idNumber;
- char* tvProducer;
- DateTime startTime;
- DateTime endTime;
- EventType subjects;
- } EmmissionType;
- struct TVNodeStruct {
- EmmissionType *anEmmission;
- struct TVNodeStruct *next;
- };
- typedef struct TVNodeStruct *TVNodeType;
- int timeComparison(DateTime x,DateTime xo)
- {
- if (x.y>xo.y )
- return 1;
- if (x.y<xo.y)
- return 0;
- if (x.m>xo.m)
- return 1;
- if (x.m<xo.m)
- return 0;
- if (x.d>xo.d)
- return 1;
- if (x.d<xo.d)
- return 0;
- if (x.hh>xo.hh)
- return 1;
- if (x.hh<xo.hh)
- return 0;
- if (x.mm>xo.mm)
- return 1;
- if (x.mm<xo.mm)
- return 0;
- }
- TVNodeType insertEmmission(TVNodeType *lh,TVNodeType *lt,EmmissionType neo)
- {
- TVNodeType current,nextofcurrent;
- EmmissionType *temp;
- TVNodeType node=(TVNodeType)malloc(sizeof(struct TVNodeStruct));
- if (node==NULL)
- return 0;
- temp=(EmmissionType*)malloc(sizeof(EmmissionType));
- if (temp==NULL)
- return 0;
- temp->title=(char *)malloc(sizeof(char)* (strlen(neo.title)+1));
- if (temp->title==NULL)
- return 0;
- strcpy(temp->title,neo.title);
- temp->description==(char *)malloc(sizeof(char)* (strlen(neo.description)+1));
- if(temp->description==NULL)
- return 0;
- strcpy(temp->description,neo.description);
- temp->idNumber=neo.idNumber;
- temp->tvProducer=(char *)malloc(sizeof(char)* (strlen(neo.tvProducer)+1));
- if (temp->tvProducer==NULL)
- return 0;
- strcpy(temp->tvProducer,neo.tvProducer);
- temp->startTime.d=neo.startTime.d;
- temp->startTime.hh=neo.startTime.hh;
- temp->startTime.m=neo.startTime.m;
- temp->startTime.mm=neo.startTime.mm;
- temp->startTime.y=neo.startTime.y;
- temp->endTime.d=neo.endTime.d;
- temp->endTime.hh=neo.endTime.hh;
- temp->endTime.m=neo.endTime.m;
- temp->endTime.mm=neo.endTime.mm;
- temp->endTime.y=neo.endTime.y;
- temp->subjects=NULL;
- node->anEmmission=temp;
- if (*lh==NULL)
- {
- lh=&node;
- lt=&node;
- node->next=NULL;
- return node;
- }
- if (timeComparison(temp->startTime,(*lt)->anEmmission->startTime)==1)
- {
- (*lt)->next=node;
- node->next=NULL;
- lt=&node;
- return node;
- }
- if (timeComparison(temp->startTime,(*lh)->anEmmission->startTime)==0)
- {
- node->next=*lh;
- lh=&node;
- return node;
- }
- for (current=*lh;timeComparison(temp->startTime,current->next->anEmmission->startTime)==1;current=current->next);
- nextofcurrent=current->next;
- current->next=node;
- node->next=nextofcurrent;
- return node;
- }
- int insertEvent(TVNodeType *parent,struct Event neo )
- {
- EventType current;
- EventType temp=(EventType)malloc(sizeof(struct Event));
- if (temp==NULL)
- return 0;
- temp->eventPlace=(char*)malloc(sizeof(char)*(strlen(neo.eventPlace)+1));
- if (temp->eventPlace==NULL)
- return 0;
- strcpy(temp->eventPlace,neo.eventPlace);
- temp->eventTitle=(char*)malloc(sizeof(char)*(strlen(neo.eventTitle)+1));
- if (temp->eventTitle==NULL)
- return 0;
- temp->official=neo.official;
- temp->sport=(struct SportType*)malloc(sizeof(struct SportType));
- temp->sport->classical=neo.sport->classical;
- temp->sport->individual=neo.sport->individual;
- temp->sport->name=(char*)malloc(sizeof(char)*(strlen(neo.sport->name)+1));
- if (temp->sport->name==NULL)
- return 0;
- strcpy(temp->sport->name,neo.sport->name);
- temp->sport->participant1=(char*)malloc(sizeof(char)*(strlen(neo.sport->participant1)+1));
- if (temp->sport->participant1==NULL)
- return 0;
- strcpy(temp->sport->participant1,neo.sport->participant1);
- temp->sport->participant2=(char*)malloc(sizeof(char)*(strlen(neo.sport->participant2)+1));
- if (temp->sport->participant2==NULL)
- return 0;
- strcpy(temp->sport->participant2,neo.sport->participant2);
- temp->xronos.d=neo.xronos.d;
- temp->xronos.y=neo.xronos.y;
- temp->xronos.hh=neo.xronos.hh;
- temp->xronos.m=neo.xronos.m;
- temp->xronos.mm=neo.xronos.mm;
- if ((*parent)->anEmmission->subjects==NULL)
- {
- (*parent)->anEmmission->subjects=temp;
- temp->next=NULL;
- return 1;
- }
- for(current=(*parent)->anEmmission->subjects;current->next!=NULL;current=current->next);
- current->next=temp;
- temp->next=NULL;
- return 1;
- }
- DateTime *read_time(FILE *inp)
- {
- DateTime *current=( DateTime*)malloc(sizeof(DateTime));
- char str1[3],str2[5];
- char c;
- str1[2]='\0';
- str2[4]='\0';
- fgetc(inp);
- str2[0]=fgetc(inp);
- str2[1]=fgetc(inp);
- str2[2]=fgetc(inp);
- str2[3]=fgetc(inp);
- current->y=atoi(str2);
- fgetc(inp);
- str1[0]=fgetc(inp); // DONE ?
- str1[1]=fgetc(inp);
- current->m=atoi(str1);
- fgetc(inp);
- str1[0]=fgetc(inp);
- str1[1]=fgetc(inp);
- current->d=atoi(str1);
- fgetc(inp);
- str1[0]=fgetc(inp);
- str1[1]=fgetc(inp);
- current->hh=atoi(str1);
- fgetc(inp);
- str1[0]=fgetc(inp);
- str1[1]=fgetc(inp);
- current->mm=atoi(str1);
- fgetc(inp);
- return current;
- }
- struct SportType *read_sport(FILE *inp)
- { int i;
- char c;
- struct SportType *current=(struct SportType*)malloc(sizeof(struct SportType));
- current->name=(char*)malloc(101);
- current->participant1=(char*)malloc(101);
- current->participant2=(char*)malloc(101);
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp))
- current->name[i]=c;
- current->name[i]='\0';
- fscanf(inp,"<%d>",¤t->classical);
- fscanf(inp,"<%d>",¤t->individual);
- fgetc(inp);
- fgetc(inp); // g alagi grammis
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp))
- current->participant1[i]=c; // DONEEEEEEEEEEEEEE
- current->participant1[i]='\0';
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp))
- current->participant2[i]=c;
- current->participant2[i]='\0';
- fgetc(inp);
- fgetc(inp); // gia allagi grammis
- return current;
- }
- int searchsport(TVNodeType *nlh,TVNodeType *nlt,TVNodeType lh,char *sportis )
- {
- TVNodeType current;
- EventType current1;
- for (current=lh;current!=NULL;current=current->next)
- for (current1=current->anEmmission->subjects;current1!=NULL;current1=current1->next)
- if (strcmp(current1->sport->name,sportis)==0 && current1->official==1)
- {
- (insertEmmission(nlh,nlt,*current->anEmmission))->anEmmission->subjects=current->anEmmission->subjects;
- break;
- }
- return 1;
- }
- EmmissionType *read_emmission(FILE *inp)
- {
- int i;
- char c;
- EmmissionType *current=(EmmissionType*)malloc(sizeof(EmmissionType));
- fscanf(inp,"<%d>",¤t->idNumber);
- current->title=(char*)malloc(101);
- current->description=(char*)malloc(101);
- current->tvProducer=(char*)malloc(101);
- // current->endTime=(DateTime*)malloc(sizeof(DateTime));
- // current->startTime=(DateTime*)malloc(sizeof(DateTime));
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp))
- current->title[i]=c;
- current->title[i]='\0';
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp))
- current->description[i]=c;
- current->description[i]='\0';
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp))
- current->tvProducer[i]=c;
- current->tvProducer[i]='\0'; // DONEEEEEEEEEEEEEEEEEE
- fgetc(inp);
- fgetc(inp);
- //edo mpori na prepei na diabazo xaraktira g allagi grammis? tora tha kaleso t diabasma g Datetime
- current->startTime=*(read_time(inp));
- current->endTime=*(read_time(inp)); // edo ine sosto p ebala diefthinsi? afu dn ine pointer,k m erxete pointer apo t readtime
- fgetc(inp); // alagi grammis
- //pali edo prp na diabaso xaraktira alagis grammis?
- return current;
- }
- struct Event *read_event(FILE *inp)
- {
- int i;
- char c;
- struct Event *current=(struct Event*)malloc(sizeof(struct Event));
- current->eventPlace=(char*)malloc(101);
- current->eventTitle=(char*)malloc(101);
- current->organizer=(char*)malloc(101);
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp)) // DONEEEEEEEEEEEEEEEEE!!!!!!!!!!
- current->eventTitle[i]=c;
- current->eventTitle[i]='\0';
- fgetc(inp);
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp))
- current->organizer[i]=c;
- current->organizer[i]='\0';
- fgetc(inp);
- fgetc(inp); // ti pezi m t allagi grAMIS?
- current->xronos=*(read_time(inp));
- fgetc(inp);
- c=fgetc(inp);
- for (i=0;c!='>';i++,c=fgetc(inp))
- current->eventPlace[i]=c;
- current->eventPlace[i]='\0';
- fgetc(inp);
- fgetc(inp);
- scanf("<%d>",¤t->official);
- fgetc(inp);
- current->sport=(read_sport(inp));
- return current;
- }
- void printsport(TVNodeType lh)
- {
- TVNodeType i;
- struct Event *j;
- i=lh;
- j=lh->anEmmission->subjects;
- while (i!=NULL)
- printf("%s %s",lh->anEmmission->title,lh->anEmmission->tvProducer);
- printf("%d-%d-%d,%d:%d\n",&lh->anEmmission->startTime.y,&lh->anEmmission->startTime.m,&lh->anEmmission->startTime.d,&lh->anEmmission->startTime.hh,&lh->anEmmission->startTime.mm);
- while (j!=NULL)
- {
- printf("%s\n",j->eventTitle);
- j=j->next;
- }
- i=i->next;
- }
- //}
- int main (void)
- {
- FILE *inp;
- inp=fopen("input.txt","r");
- TVNodeType list_head;
- TVNodeType list_tail;
- TVNodeType new_list_head;
- TVNodeType new_list_tail;
- list_head=list_tail=new_list_head=new_list_tail=NULL;
- int i,j,gegonota,ekpompes,h;
- fscanf(inp,"<%d>",&gegonota);
- fgetc(inp);
- for(i=1;i<=gegonota;i++)
- {
- insertEmmission(&list_head,&list_tail,*read_emmission(inp));
- fscanf(inp,"<%d>",&ekpompes);
- for(j=1;j<=ekpompes;j++)
- if(!insertEvent(&list_head, *read_event(inp)))
- return 0; // edo sosta stello m pointer to read_event ? i mpori na to lambani lathos i sinartisi?
- }
- searchsport(&new_list_head,&new_list_tail,list_head,"football");
- // printsport(new_list_head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement