Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. #ifndef _CRT_SECURE_NO_WARNINGS
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #endif
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7. #include<ctype.h>
  8.  
  9. typedef struct{
  10. int ID;
  11. char title[51];
  12. char director[51];
  13. double length;
  14. int year;
  15. }movie;
  16.  
  17. typedef struct Node{
  18. movie data;
  19. struct Node *next;
  20. } node;
  21.  
  22. //----------------------INIT MOVIE------------------//
  23. int isUnique(int ID, node* head);
  24. movie initMovie(node* head);
  25. void printMovie(movie data);
  26. void swapNode(node** A, node** B);
  27. void bubbleSortByYear(node** head);
  28. void printLongestMovie(node* head);
  29. void deleteByDirector(node** head);
  30. //-----------------------NODES---------------------//
  31. int isEmpty(node* head);
  32. int nodesCounter(node* head);
  33. void printNode(node** head);
  34. void addNode(node** head, movie data, int mode);
  35. void deleteNode(node** head);
  36. void deleteNodeAtPoss(node** head, int poss);
  37. //----------------------FILES-----------------------//
  38. char* getFileName();
  39. void loadFile(node** head);
  40. void saveFile(node* head);
  41. // --- GENERAL ---
  42. void getstr(char* str, int maxLen);
  43. int strCmpIgnoreCase(char* str1, char* str2);
  44.  
  45. int menu(){
  46. int i;
  47.  
  48. printf(" __/MAIN MENU\\________________________________\n");
  49. printf("| |\n");
  50. printf("| 1. LOAD DATA |\n");
  51. printf("| 2. SAVE DATA |\n");
  52. printf("| 3. ADD NEW MOVIE |\n");
  53. printf("| 4. PRINT ALL MOVIES |\n");
  54. printf("| 5. DELETE MOVIES BY DIRECTOR |\n");
  55. printf("| 6. GET LONGEST MOVIE |\n");
  56. printf("| 0. EXIT |\n");
  57. printf("|____________________________________________|\n");
  58.  
  59. do{
  60. printf("SELECT OPTION: ");
  61. fflush(stdin);
  62. scanf("%d", &i);
  63. } while (i < 0 || i > 6);
  64.  
  65. return i;
  66. }
  67.  
  68. int main(){
  69. node* head = NULL;
  70.  
  71. while (1){
  72. system("cls");
  73. switch (menu()){
  74. case 1: loadFile(&head);
  75. break;
  76. case 2: saveFile(head);
  77. break;
  78. case 3: addNode(&head, initMovie(head), 1);
  79. break;
  80. case 4: printNode(&head);
  81. break;
  82. case 5: deleteByDirector(&head);
  83. break;
  84. case 6: printLongestMovie(head);
  85. break;
  86. default:
  87. return 0;
  88. }
  89. system("pause");
  90. }
  91. }
  92.  
  93. //------------------DELETE BY DIRECTOR----------------//
  94. void deleteByDirector(node** head){
  95. char director[51];
  96. int poss = 1, count = 0;
  97. node* temp = (*head);
  98. char c;
  99.  
  100. if (!(*head)){
  101. printf("List is Empty!\n");
  102. return;
  103. }
  104.  
  105. printf("Enter Director: ");
  106. getstr(director, 51);
  107.  
  108. printf("Movies By %s\n", director);
  109. while (temp){
  110. if (!strCmpIgnoreCase(director, temp->data.director)){
  111. printf("\nMovie %d\n", ++count);
  112. printMovie(temp->data);
  113. }
  114. temp = temp->next;
  115. }
  116. if (!count){
  117. printf("No movies Found!");
  118. return;
  119. }
  120.  
  121. printf("Do you want do delete data<Y/N>");
  122. do{
  123. fflush(stdin);
  124. c = toupper(getchar());
  125. if (c != 'Y' && c != 'N'){
  126. printf("Select <Y/N>: ");
  127. }
  128. } while (c != 'Y' && c != 'N');
  129.  
  130. if (c == 'N') {
  131. return;
  132. }
  133.  
  134. temp = (*head);
  135. while (temp){
  136. if (!strCmpIgnoreCase(director, temp->data.director)){
  137. temp = temp->next;
  138. deleteNodeAtPoss(head, poss);
  139. }
  140. else{
  141. temp = temp->next;
  142. ++poss;
  143. }
  144. }
  145.  
  146. printf("All Movies by %s Deleted!\n", director);
  147. }
  148.  
  149. //-------------------LONGEST MOVIE-------------------//
  150. void printLongestMovie(node* head){
  151. node* temp = head;
  152. double maxLen = 0;
  153.  
  154. if (!head){
  155. printf("No movies!\n");
  156. return;
  157. }
  158.  
  159. while (temp){
  160. if (temp->data.length > maxLen){
  161. maxLen = temp->data.length;
  162. }
  163. temp = temp->next;
  164. }
  165.  
  166. while (head){
  167. if (head->data.length == maxLen){
  168. printf("\n");
  169. printMovie(head->data);
  170. }
  171. head = head->next;
  172. }
  173. }
  174.  
  175. //----------------------INIT MOVIE------------------//
  176. int isUnique(int ID, node* head){
  177. while (head){
  178. if (head->data.ID == ID){
  179. return 0;
  180. }
  181. head = head->next;
  182. }
  183. return 1;
  184. }
  185.  
  186. movie initMovie(node* head){
  187. movie newMovie;
  188.  
  189. while(1){
  190. printf("Enter ID: ");
  191. fflush(stdin);
  192. scanf("%d", &newMovie.ID);
  193. if (newMovie.ID < 1 || newMovie.ID > 999999999999){
  194. fprintf(stderr, "Incorrect ID!\n");
  195. continue;
  196. }
  197. if (!isUnique(newMovie.ID, head)){
  198. fprintf(stderr, "ID Already Exist!\n");
  199. continue;
  200. }
  201. break;
  202. }
  203.  
  204. printf("Enter Title: ");
  205. getstr(newMovie.title, 51);
  206. printf("Enter Director: ");
  207. getstr(newMovie.director, 51);
  208. do{
  209. printf("Enter Length: ");
  210. fflush(stdin);
  211. scanf("%lf", &newMovie.length);
  212. } while (newMovie.length <= 0);
  213. do{
  214. printf("Enter Year: ");
  215. fflush(stdin);
  216. scanf("%d", &newMovie.year);
  217.  
  218. } while (newMovie.year < 1900 || newMovie.year > 2020);
  219.  
  220. return newMovie;
  221. }
  222.  
  223. void printMovie(movie data){
  224. printf("ID: %.12d\n", data.ID);
  225. printf("Title: %s\n", data.title);
  226. printf("Director: %s\n", data.director);
  227. printf("Length: %.2lf\n", data.length);
  228. printf("Year: %d\n", data.year);
  229. }
  230.  
  231. //----------------------SORT-------------------------//
  232. void swapNode(node** A, node** B){
  233. movie temp = (*(*A)).data;
  234.  
  235. (*A)->data = (*B)->data;
  236. (*(*B)).data = temp;
  237. }
  238.  
  239. void bubbleSortByYear(node** head){
  240. node* temp = *head;
  241. int i, j;
  242. int n = nodesCounter(*head);
  243.  
  244. for (i = 0; i < n - 1; i++, temp = (*head)){
  245. for (j = 0; j < n - i - 1; j++){
  246. if (temp->data.year < temp->next->data.year){
  247. swapNode(&temp, &temp->next);
  248. }
  249. temp = temp->next;
  250. }
  251. }
  252.  
  253. }
  254.  
  255. //-----------------------NODES---------------------//
  256. int isEmpty(node* head){
  257. if (head)
  258. return 0;
  259. printf("List is empty!\n");
  260. return 1;
  261. }
  262.  
  263. int nodesCounter(node* head){
  264. int n = 0;
  265. while (head){
  266. ++n;
  267. head = head->next;
  268. }
  269.  
  270. return n;
  271. }
  272.  
  273. void printNode(node** head) {
  274. int i = 0;
  275. node* temp = (*head);
  276.  
  277. if (isEmpty(*head))
  278. return;
  279. bubbleSortByYear(head);
  280.  
  281. while (temp){
  282. printf("\nMovie %d\n", ++i);
  283. printMovie(temp->data);
  284.  
  285. temp = temp->next;
  286. }
  287. }
  288.  
  289. void addNode(node** head, movie data, int mode){
  290. node* temp = (node*)malloc(sizeof(node));
  291. node* temp2 = *head;
  292.  
  293. temp->data = data;
  294. temp->next = NULL;
  295.  
  296. if (!(*head)){
  297. *head = temp;
  298. if (mode == 1)
  299. printf("First movie added successfully!\n");
  300. return;
  301. }
  302.  
  303. while (temp2->next){
  304. temp2 = temp2->next;
  305. }
  306.  
  307. temp2->next = temp;
  308. if (mode == 1)
  309. printf("New movie added successfully!\n");
  310. }
  311.  
  312. void deleteNode(node** head){
  313. node* temp;
  314.  
  315. while (*head){
  316. temp = *head;
  317. *head = (*head)->next;
  318. free(temp);
  319. }
  320. }
  321.  
  322. void deleteNodeAtPoss(node** head, int poss){
  323. int i;
  324. node *temp1 = (*head), *temp2;
  325.  
  326. if (poss == 1){
  327. (*head) = temp1->next;
  328. free(temp1);
  329. return;
  330. }
  331.  
  332. for (i = 0; i < poss - 2; i++){
  333. temp1 = temp1->next;
  334. //temp1 pointst to (n-1)th possition;
  335. }
  336. temp2 = temp1->next;//temp2 points to n-th poss Node;
  337. temp1->next = temp2->next;
  338. free(temp2);
  339. }
  340.  
  341. //----------------------FILES-----------------------//
  342. char* getFileName(){
  343. char *fileName = (char*)malloc(51 * sizeof(char));
  344.  
  345. printf("\nEnter file name: ");
  346. getstr(fileName, 51);
  347.  
  348. if (!strchr(fileName, '.')){
  349. strcat(fileName, ".dat");
  350. }
  351.  
  352. return fileName;
  353. }
  354.  
  355. void loadFile(node** head){
  356. FILE* fp;
  357. movie data;
  358. char *fileName;
  359.  
  360. fileName = getFileName();
  361.  
  362. if (!(fp = fopen(fileName, "rb"))){
  363. fprintf(stderr, "Error loading file!\a\n");
  364. free(fileName);
  365. return;
  366. }
  367.  
  368. if (*head)
  369. deleteNode(head);
  370.  
  371. while (1){
  372. if (!fread(&data, sizeof(data), 1, fp))
  373. break;
  374.  
  375. addNode(head, data, 2);
  376. }
  377.  
  378. fclose(fp);
  379.  
  380. if (!(*head))
  381. printf("No data loaded from \"%s\"\n", fileName);
  382. else
  383. printf("Info loaded successfully from \"%s\"\n", fileName);
  384.  
  385. free(fileName);
  386. }
  387.  
  388. void saveFile(node* head){
  389. FILE* fp;
  390. char *fileName;
  391.  
  392. if (isEmpty(head)){
  393. printf("Add some movies first!\n");
  394. return;
  395. }
  396.  
  397. fileName = getFileName();
  398.  
  399. if (!(fp = fopen(fileName, "wb"))){
  400. exit(1);
  401. }
  402.  
  403. while (head){
  404. if (fwrite(&head->data, sizeof(movie), 1, fp) != 1){
  405. printf("Error occured while writing on file\n");
  406. exit(1);
  407. }
  408. head = head->next;
  409. }
  410.  
  411. fclose(fp);
  412.  
  413. printf("Info saved successfully to \"%s\"\n", fileName);
  414. free(fileName);
  415. }
  416.  
  417. // --- GENERAL ---
  418. void getstr(char* str, int maxLen){
  419. fflush(stdin);
  420. fgets(str, maxLen * sizeof(char), stdin);
  421. str[strlen(str) - 1] = '\0';
  422. }
  423.  
  424. int strCmpIgnoreCase(char* str1, char* str2){
  425. char str1upper[51];
  426. char str2upper[51];
  427. unsigned i;
  428.  
  429. strcpy(str1upper, str1);
  430. strcpy(str2upper, str2);
  431.  
  432. for (i = 0; i < strlen(str1); i++){
  433. str1upper[i] = toupper(str1upper[i]);
  434. }
  435. for (i = 0; i < strlen(str2); i++){
  436. str2upper[i] = toupper(str2upper[i]);
  437. }
  438.  
  439. if (!strcmp(str1upper, str2upper)){
  440. return 0;
  441. }
  442. return -1;
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement