Advertisement
elenaalex

Untitled

Jan 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define N 20
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. struct date
  7. {
  8. int day, month, year;
  9. };
  10. struct lecturer
  11. {
  12. char *name;
  13. struct date birthday;
  14. int num_of_courses;
  15. char **courses;
  16. };
  17. void Course_Lectures(struct lecturer *arr1, char *name, int size)
  18. {
  19. int i, j;
  20. int i_course, j_course;
  21. for (i = 0; i < size; i++)
  22. {
  23. for (j = 0; j < arr1[i].num_of_courses; j++)
  24. {
  25. if (strcmp(name, arr1[i].courses[j]) == 0)
  26. {
  27. puts(arr1[i].name);
  28. }
  29. }
  30. }
  31.  
  32. }
  33. void common(struct lecturer *arr1, char *name1, char *name2, int size)
  34. {
  35. int i, j;
  36. int i_name, j_name;
  37. for (i = 0; i < size; i++)
  38. {
  39. if (strcmp(name1, arr1[i].name) == 0)
  40. {
  41. i_name = i;
  42. }
  43. if (strcmp(name2, arr1[i].name) == 0)
  44. {
  45. j_name = i;
  46. }
  47. }
  48. for (i = 0; i < arr1[i_name].num_of_courses; i++)
  49. {
  50. for (j = 0; j < arr1[j_name].num_of_courses; j++)
  51. if ((strcmp(arr1[i_name].courses[i], arr1[j_name].courses[j])) == 0)
  52. printf(arr1[i_name].courses[i]);
  53. }
  54. }
  55. void old(int day,int month,int year,struct lecturer *arr1, int size)
  56. {
  57. int i, j;
  58. for (i = 0; i < size; i++)
  59. {
  60. if (arr1[i].birthday.year<year)
  61. {
  62. puts(arr1[i].name);
  63. }
  64. else if (arr1[i].birthday.year =year)
  65. {
  66. if (arr1[i].birthday.month = month)
  67. {
  68. if (arr1[i].birthday.day <= day)
  69. {
  70. puts(arr1[i].name);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. int main()
  77. {
  78. struct lecturer *data = NULL;
  79. int day,month,year;
  80. int size, i, j;
  81. char name[N], name1[N], name2[N], NameCourse[N];
  82. printf("enter please the number of the lectrures :\n ");
  83. scanf("%d", &size);
  84. data = (struct lecturer*)malloc(size * sizeof(struct lecturer));
  85. if (data == NULL)
  86. {
  87. printf("error \n");
  88. return 0;
  89. }
  90. for (i = 0; i < size; i++)
  91. {
  92. printf("enter the name of the prof:\n");
  93. scanf("%s", name);
  94. data[i].name = (char*)malloc((strlen(name) + 1) * sizeof(char));
  95. if (data[i].name == NULL)
  96. {
  97. printf("memory error \n");
  98. // Oh look memory leak. Free your things!
  99. return 0;
  100. }
  101. for (j = 0; j < strlen(name); j++)
  102. {
  103. data[i].name[j] = name[j];
  104. }
  105. data[i].name[j] = '\0';
  106.  
  107. printf("please enter the birthday of the prof :\n");
  108. scanf("%d%d%d", &data[i].birthday.day, &data[i].birthday.month, &data[i].birthday.year);
  109. printf("enter the number of courses of the prof :\n");
  110. scanf("%d", &data[i].num_of_courses);
  111. data[i].courses = (char**)malloc(data[i].num_of_courses * sizeof(char*));
  112. for (j = 0; j<data[i].num_of_courses; j++)
  113. {
  114. data[i].courses[j] = (char*)malloc(N * sizeof(char));
  115. if (data[i].courses[j] == NULL)
  116. {
  117. printf("memory error \n");
  118. // Oh look memory leak. Free your things!
  119. return 0;
  120. }
  121. printf("enter the name of the course :\n");
  122. scanf("%s", data[i].courses[j]);
  123. }
  124. }
  125.  
  126. printf("Done inputting lecturers, now inputting courses:\n");
  127. printf("enter the name of the course :\n");
  128. scanf("%s", NameCourse);
  129. Course_Lectures(data,NameCourse, size);
  130. printf("\n");
  131. printf("enter names of two profs\n");
  132. scanf("%s", name1);
  133. scanf("%s", name2);
  134. common(data, name1, name2, size);
  135. printf("\n");
  136. printf("enter the date after which the prof was born");
  137. scanf("%d%d%d",&day,&month, &year);
  138. old(day,month,year, data, size);
  139. for (i = 0; i < size; i++)
  140. {
  141. for (j = 0; j<data[i].num_of_courses; j++)
  142. free(data[i].courses[j]);
  143. free(data[i].courses);
  144. }
  145. free(data);
  146. return 0;
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement