Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <limits.h>
  7.  
  8. struct carinfo_t
  9. {
  10. char *brand;
  11. char *model;
  12. int year;
  13. float value;
  14.  
  15. };
  16.  
  17. struct carinfo_t *createCarinfo(char *brand, char *model, int year, float value)
  18. {
  19.  
  20.  
  21.  
  22. struct carinfo_t* temp = (struct carinfo_t*) malloc(sizeof(struct carinfo_t));
  23.  
  24. temp->brand = (char*)malloc(sizeof(char)*(strlen(brand) +1));
  25. strcpy(temp->brand, brand);
  26.  
  27. temp->model = (char*)malloc(sizeof(char)*(strlen(model) + 1));
  28. strcpy(temp->model, model);
  29.  
  30. temp->year = year;
  31.  
  32. temp->value = value;
  33.  
  34. return temp;
  35. }
  36.  
  37. void addCarinfo(struct carinfo_t **carbase, struct carinfo_t *carinfo)
  38. {
  39. int i = 0;
  40. int n = 0;
  41. for (i = 0; i < 100; i++)
  42. {
  43. if (carbase[i] != NULL)
  44. {
  45. n++;
  46. }
  47. }
  48. if (n >= 100)
  49. {
  50.  
  51. printf("Failed adding car; carbase is already full!");
  52. }
  53. if (n < 100)
  54. {
  55. carbase[n] = carinfo;
  56.  
  57.  
  58. }
  59. }
  60. void freeCarinfo(struct carinfo_t *carinfo)
  61. {
  62. free(carinfo->brand);
  63. free(carinfo->model);
  64. free(carinfo);
  65.  
  66.  
  67. }
  68.  
  69. void printCarbase(struct carinfo_t **carbase)
  70. {
  71. int i,j = 0;
  72. int n = 0;
  73. for (j = 0; j < 100; j++)
  74. {
  75. if (carbase[j] == NULL)
  76. {
  77. n++;
  78. }
  79. }
  80. for (i = 0; i < 100; i++)
  81. {
  82. if (n == 100)
  83. {
  84. printf("The database is empty.\n", carbase[i]);
  85. i = 105;
  86. }
  87. if (carbase[i] != NULL && i < 100)
  88. {
  89. printf("Car:\n");
  90. printf("- brand: %s\n", carbase[i]->brand);
  91. printf("- model: %s\n", carbase[i]->model);
  92. printf("- year: %d\n", carbase[i]->year);
  93. printf("- value: %.2f\n", carbase[i]->value);
  94. }
  95. }
  96. }
  97.  
  98. void removeCarinfo(struct carinfo_t **carbase, struct carinfo_t *carinfo)
  99. {
  100. int i,j,n = 0;
  101. for (i = 0; i < 100; i++)
  102. {
  103. if (carbase[i] != NULL)
  104. {
  105. if (strcmp(carbase[i]->brand, carinfo->brand) == 0 && strcmp(carbase[i]->model, carinfo->model) == 0 && carbase[i]->year == carinfo->year)
  106. {
  107.  
  108. free(carbase[i]->brand);
  109. free(carbase[i]->model);
  110. free(carbase[i]);
  111. carbase[i] = NULL;
  112. }
  113. }
  114.  
  115. }
  116. }
  117. int main()
  118. {
  119. int i, j = 0;
  120. struct carinfo_t *carbase[100] = {};
  121. struct carinfo_t *carinfo;
  122. char brand[20] = {};
  123. char model[20] = {};
  124. int year = 0;
  125. float value = 0;
  126. char remove[20] = {};
  127. char f;
  128. int e = 1;
  129.  
  130. while (e)
  131. {
  132. printf("Command (a/r/p/q): ");
  133. scanf(" %c", &f);
  134. switch (f)
  135. {
  136. case 'a':
  137. printf("Add a car:\n");
  138. printf(" brand: ");
  139. scanf(" %s", &brand);
  140. printf(" model: ");
  141. scanf(" %s", &model);
  142. printf(" year: ");
  143. scanf(" %d", &year);
  144. printf(" value: ");
  145. scanf(" %f", &value);
  146.  
  147. carinfo = createCarinfo(brand, model, year, value);
  148. addCarinfo(carbase, carinfo);
  149.  
  150.  
  151. break;
  152. case 'p': printCarbase(carbase);
  153. break;
  154. case 'r':
  155. printf("Remove a car:\n");
  156. printf(" brand: ");
  157. scanf(" %[^\n]s\n", &brand);
  158. printf(" model: ");
  159. scanf(" %[^\n]s", &model);
  160. printf(" year: ");
  161. scanf(" %d", &year);
  162. carinfo = createCarinfo(brand, model, year, '\0');
  163. removeCarinfo(carbase, carinfo);
  164. break;
  165. case 'q': e = 0;
  166. break;
  167.  
  168. }
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement