Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct {
  5. int y;
  6. int m;
  7. int d;
  8. int hh;
  9. int mm;
  10. } DateTime;
  11.  
  12. struct SportType {
  13. char* name;
  14. int classical; // ??as??? 1 ? s??????? 0
  15. int individual; // at?µ??? 1 ? ?µad??? 0
  16. char *participant1;
  17. char *participant2;
  18. };
  19.  
  20. struct Event {
  21. char *eventTitle;
  22. char *organizer;
  23. char *eventPlace;
  24. DateTime xronos;
  25. int official; // ep?s?µ?? 1 ? f?????? 0 ?a?a?t??a
  26. struct SportType *sport;
  27. struct Event *next;
  28. };
  29. typedef struct Event *EventType;
  30.  
  31. typedef struct {
  32. char *title;
  33. char *description;
  34. int idNumber;
  35. char* tvProducer;
  36. DateTime startTime;
  37. DateTime endTime;
  38. EventType subjects;
  39. } EmmissionType;
  40.  
  41.  
  42. struct TVNodeStruct {
  43. EmmissionType *anEmmission;
  44. struct TVNodeStruct *next;
  45. };
  46. typedef struct TVNodeStruct *TVNodeType;
  47. int timeComparison(DateTime x,DateTime xo)
  48. {
  49. if (x.y>xo.y )
  50. return 1;
  51. if (x.y<xo.y)
  52. return 0;
  53. if (x.m>xo.m)
  54. return 1;
  55. if (x.m<xo.m)
  56. return 0;
  57. if (x.d>xo.d)
  58. return 1;
  59. if (x.d<xo.d)
  60. return 0;
  61. if (x.hh>xo.hh)
  62. return 1;
  63. if (x.hh<xo.hh)
  64. return 0;
  65. if (x.mm>xo.mm)
  66. return 1;
  67. if (x.mm<xo.mm)
  68. return 0;
  69. }
  70. TVNodeType insertEmmission(TVNodeType *lh,TVNodeType *lt,EmmissionType neo)
  71. {
  72. TVNodeType current,nextofcurrent;
  73. EmmissionType *temp;
  74. TVNodeType node=(TVNodeType)malloc(sizeof(struct TVNodeStruct));
  75. if (node==NULL)
  76. return 0;
  77.  
  78. temp=(EmmissionType*)malloc(sizeof(EmmissionType));
  79. if (temp==NULL)
  80. return 0;
  81. temp->title=(char *)malloc(sizeof(char)* (strlen(neo.title)+1));
  82. if (temp->title==NULL)
  83. return 0;
  84. strcpy(temp->title,neo.title);
  85. temp->description==(char *)malloc(sizeof(char)* (strlen(neo.description)+1));
  86. if(temp->description==NULL)
  87. return 0;
  88. strcpy(temp->description,neo.description);
  89. temp->idNumber=neo.idNumber;
  90.  
  91. temp->tvProducer=(char *)malloc(sizeof(char)* (strlen(neo.tvProducer)+1));
  92. if (temp->tvProducer==NULL)
  93. return 0;
  94. strcpy(temp->tvProducer,neo.tvProducer);
  95.  
  96. temp->startTime.d=neo.startTime.d;
  97. temp->startTime.hh=neo.startTime.hh;
  98. temp->startTime.m=neo.startTime.m;
  99. temp->startTime.mm=neo.startTime.mm;
  100. temp->startTime.y=neo.startTime.y;
  101.  
  102. temp->endTime.d=neo.endTime.d;
  103. temp->endTime.hh=neo.endTime.hh;
  104. temp->endTime.m=neo.endTime.m;
  105. temp->endTime.mm=neo.endTime.mm;
  106. temp->endTime.y=neo.endTime.y;
  107.  
  108. temp->subjects=NULL;
  109. node->anEmmission=temp;
  110.  
  111. if (*lh==NULL)
  112. {
  113. lh=&node;
  114. lt=&node;
  115. node->next=NULL;
  116. return node;
  117. }
  118. if (timeComparison(temp->startTime,(*lt)->anEmmission->startTime)==1)
  119. {
  120. (*lt)->next=node;
  121. node->next=NULL;
  122. lt=&node;
  123. return node;
  124.  
  125. }
  126. if (timeComparison(temp->startTime,(*lh)->anEmmission->startTime)==0)
  127. {
  128. node->next=*lh;
  129. lh=&node;
  130. return node;
  131. }
  132. for (current=*lh;timeComparison(temp->startTime,current->next->anEmmission->startTime)==1;current=current->next);
  133.  
  134. nextofcurrent=current->next;
  135. current->next=node;
  136. node->next=nextofcurrent;
  137. return node;
  138.  
  139. }
  140.  
  141. int insertEvent(TVNodeType *parent,struct Event neo )
  142. {
  143. EventType current;
  144. EventType temp=(EventType)malloc(sizeof(struct Event));
  145. if (temp==NULL)
  146. return 0;
  147.  
  148. temp->eventPlace=(char*)malloc(sizeof(char)*(strlen(neo.eventPlace)+1));
  149. if (temp->eventPlace==NULL)
  150. return 0;
  151. strcpy(temp->eventPlace,neo.eventPlace);
  152. temp->eventTitle=(char*)malloc(sizeof(char)*(strlen(neo.eventTitle)+1));
  153. if (temp->eventTitle==NULL)
  154. return 0;
  155. temp->official=neo.official;
  156. temp->sport=(struct SportType*)malloc(sizeof(struct SportType));
  157. temp->sport->classical=neo.sport->classical;
  158. temp->sport->individual=neo.sport->individual;
  159. temp->sport->name=(char*)malloc(sizeof(char)*(strlen(neo.sport->name)+1));
  160. if (temp->sport->name==NULL)
  161. return 0;
  162. strcpy(temp->sport->name,neo.sport->name);
  163. temp->sport->participant1=(char*)malloc(sizeof(char)*(strlen(neo.sport->participant1)+1));
  164. if (temp->sport->participant1==NULL)
  165. return 0;
  166. strcpy(temp->sport->participant1,neo.sport->participant1);
  167. temp->sport->participant2=(char*)malloc(sizeof(char)*(strlen(neo.sport->participant2)+1));
  168. if (temp->sport->participant2==NULL)
  169. return 0;
  170. strcpy(temp->sport->participant2,neo.sport->participant2);
  171. temp->xronos.d=neo.xronos.d;
  172. temp->xronos.y=neo.xronos.y;
  173. temp->xronos.hh=neo.xronos.hh;
  174. temp->xronos.m=neo.xronos.m;
  175. temp->xronos.mm=neo.xronos.mm;
  176.  
  177. if ((*parent)->anEmmission->subjects==NULL)
  178. {
  179. (*parent)->anEmmission->subjects=temp;
  180. temp->next=NULL;
  181. return 1;
  182. }
  183. for(current=(*parent)->anEmmission->subjects;current->next!=NULL;current=current->next);
  184. current->next=temp;
  185. temp->next=NULL;
  186. return 1;
  187. }
  188.  
  189. DateTime *read_time(FILE *inp)
  190. {
  191. DateTime *current=( DateTime*)malloc(sizeof(DateTime));
  192. char str1[3],str2[5];
  193. char c;
  194.  
  195. str1[2]='\0';
  196. str2[4]='\0';
  197. fgetc(inp);
  198.  
  199. str2[0]=fgetc(inp);
  200. str2[1]=fgetc(inp);
  201. str2[2]=fgetc(inp);
  202. str2[3]=fgetc(inp);
  203.  
  204. current->y=atoi(str2);
  205. fgetc(inp);
  206. str1[0]=fgetc(inp); // DONE ?
  207. str1[1]=fgetc(inp);
  208. current->m=atoi(str1);
  209. fgetc(inp);
  210. str1[0]=fgetc(inp);
  211. str1[1]=fgetc(inp);
  212. current->d=atoi(str1);
  213. fgetc(inp);
  214. str1[0]=fgetc(inp);
  215. str1[1]=fgetc(inp);
  216. current->hh=atoi(str1);
  217. fgetc(inp);
  218. str1[0]=fgetc(inp);
  219. str1[1]=fgetc(inp);
  220. current->mm=atoi(str1);
  221. fgetc(inp);
  222. return current;
  223. }
  224. struct SportType *read_sport(FILE *inp)
  225. { int i;
  226. char c;
  227. struct SportType *current=(struct SportType*)malloc(sizeof(struct SportType));
  228. current->name=(char*)malloc(101);
  229. current->participant1=(char*)malloc(101);
  230. current->participant2=(char*)malloc(101);
  231.  
  232. fgetc(inp);
  233. c=fgetc(inp);
  234. for (i=0;c!='>';i++,c=fgetc(inp))
  235. current->name[i]=c;
  236. current->name[i]='\0';
  237.  
  238. fscanf(inp,"<%d>",&current->classical);
  239. fscanf(inp,"<%d>",&current->individual);
  240. fgetc(inp);
  241. fgetc(inp); // g alagi grammis
  242.  
  243. fgetc(inp);
  244. c=fgetc(inp);
  245. for (i=0;c!='>';i++,c=fgetc(inp))
  246. current->participant1[i]=c; // DONEEEEEEEEEEEEEE
  247. current->participant1[i]='\0';
  248.  
  249. fgetc(inp);
  250. c=fgetc(inp);
  251.  
  252. for (i=0;c!='>';i++,c=fgetc(inp))
  253. current->participant2[i]=c;
  254. current->participant2[i]='\0';
  255. fgetc(inp);
  256. fgetc(inp); // gia allagi grammis
  257.  
  258.  
  259. return current;
  260. }
  261.  
  262. int searchsport(TVNodeType *nlh,TVNodeType *nlt,TVNodeType lh,char *sportis )
  263. {
  264. TVNodeType current;
  265. EventType current1;
  266. for (current=lh;current!=NULL;current=current->next)
  267. for (current1=current->anEmmission->subjects;current1!=NULL;current1=current1->next)
  268. if (strcmp(current1->sport->name,sportis)==0 && current1->official==1)
  269. {
  270. (insertEmmission(nlh,nlt,*current->anEmmission))->anEmmission->subjects=current->anEmmission->subjects;
  271.  
  272. break;
  273.  
  274. }
  275. return 1;
  276. }
  277. EmmissionType *read_emmission(FILE *inp)
  278. {
  279. int i;
  280. char c;
  281. EmmissionType *current=(EmmissionType*)malloc(sizeof(EmmissionType));
  282. fscanf(inp,"<%d>",&current->idNumber);
  283.  
  284. current->title=(char*)malloc(101);
  285. current->description=(char*)malloc(101);
  286. current->tvProducer=(char*)malloc(101);
  287. // current->endTime=(DateTime*)malloc(sizeof(DateTime));
  288. // current->startTime=(DateTime*)malloc(sizeof(DateTime));
  289.  
  290. fgetc(inp);
  291. c=fgetc(inp);
  292. for (i=0;c!='>';i++,c=fgetc(inp))
  293. current->title[i]=c;
  294. current->title[i]='\0';
  295.  
  296. fgetc(inp);
  297. c=fgetc(inp);
  298. for (i=0;c!='>';i++,c=fgetc(inp))
  299. current->description[i]=c;
  300. current->description[i]='\0';
  301.  
  302. fgetc(inp);
  303. c=fgetc(inp);
  304. for (i=0;c!='>';i++,c=fgetc(inp))
  305. current->tvProducer[i]=c;
  306. current->tvProducer[i]='\0'; // DONEEEEEEEEEEEEEEEEEE
  307. fgetc(inp);
  308. fgetc(inp);
  309.  
  310.  
  311. //edo mpori na prepei na diabazo xaraktira g allagi grammis? tora tha kaleso t diabasma g Datetime
  312. current->startTime=*(read_time(inp));
  313. current->endTime=*(read_time(inp)); // edo ine sosto p ebala diefthinsi? afu dn ine pointer,k m erxete pointer apo t readtime
  314. fgetc(inp); // alagi grammis
  315. //pali edo prp na diabaso xaraktira alagis grammis?
  316. return current;
  317. }
  318. struct Event *read_event(FILE *inp)
  319. {
  320. int i;
  321. char c;
  322. struct Event *current=(struct Event*)malloc(sizeof(struct Event));
  323.  
  324. current->eventPlace=(char*)malloc(101);
  325. current->eventTitle=(char*)malloc(101);
  326. current->organizer=(char*)malloc(101);
  327.  
  328. fgetc(inp);
  329. c=fgetc(inp);
  330. for (i=0;c!='>';i++,c=fgetc(inp)) // DONEEEEEEEEEEEEEEEEE!!!!!!!!!!
  331. current->eventTitle[i]=c;
  332. current->eventTitle[i]='\0';
  333. fgetc(inp);
  334.  
  335. fgetc(inp);
  336. c=fgetc(inp);
  337. for (i=0;c!='>';i++,c=fgetc(inp))
  338. current->organizer[i]=c;
  339. current->organizer[i]='\0';
  340. fgetc(inp);
  341. fgetc(inp); // ti pezi m t allagi grAMIS?
  342.  
  343. current->xronos=*(read_time(inp));
  344.  
  345. fgetc(inp);
  346. c=fgetc(inp);
  347. for (i=0;c!='>';i++,c=fgetc(inp))
  348. current->eventPlace[i]=c;
  349. current->eventPlace[i]='\0';
  350. fgetc(inp);
  351.  
  352. fgetc(inp);
  353. scanf("<%d>",&current->official);
  354. fgetc(inp);
  355.  
  356. current->sport=(read_sport(inp));
  357.  
  358. return current;
  359.  
  360. }
  361. void printsport(TVNodeType lh)
  362. {
  363. TVNodeType i;
  364. struct Event *j;
  365. i=lh;
  366. j=lh->anEmmission->subjects;
  367. while (i!=NULL)
  368. printf("%s %s",lh->anEmmission->title,lh->anEmmission->tvProducer);
  369. 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);
  370. while (j!=NULL)
  371. {
  372. printf("%s\n",j->eventTitle);
  373. j=j->next;
  374. }
  375. i=i->next;
  376. }
  377.  
  378.  
  379.  
  380. //}
  381.  
  382. int main (void)
  383. {
  384.  
  385. FILE *inp;
  386. inp=fopen("input.txt","r");
  387.  
  388. TVNodeType list_head;
  389. TVNodeType list_tail;
  390. TVNodeType new_list_head;
  391. TVNodeType new_list_tail;
  392. list_head=list_tail=new_list_head=new_list_tail=NULL;
  393. int i,j,gegonota,ekpompes,h;
  394.  
  395.  
  396.  
  397. fscanf(inp,"<%d>",&gegonota);
  398. fgetc(inp);
  399.  
  400. for(i=1;i<=gegonota;i++)
  401. {
  402. insertEmmission(&list_head,&list_tail,*read_emmission(inp));
  403. fscanf(inp,"<%d>",&ekpompes);
  404. for(j=1;j<=ekpompes;j++)
  405. if(!insertEvent(&list_head, *read_event(inp)))
  406. return 0; // edo sosta stello m pointer to read_event ? i mpori na to lambani lathos i sinartisi?
  407. }
  408. searchsport(&new_list_head,&new_list_tail,list_head,"football");
  409. // printsport(new_list_head);
  410.  
  411. return 0;
  412.  
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement