Advertisement
Guest User

Untitled

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