Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //#include <malloc.h>
  4. #include <string.h>
  5. #define LOOPS 2
  6. #define MAX_LENGTH 50
  7. struct building
  8. {
  9. int id, numFloors, numApp;
  10. char nameN[MAX_LENGTH + 1], nameSuper[MAX_LENGTH + 1];
  11. int group;
  12. };
  13. typedef struct building BODY;
  14. struct List
  15. {
  16. BODY body;
  17. struct List *pNext;
  18. };
  19. typedef struct List LIST;
  20. int enterBody(BODY *ps);
  21. int printBody(BODY s);
  22. void print(LIST *pFirst);
  23. LIST *insertBegin(LIST *pFirst, BODY newBody);
  24. LIST *delFirst(LIST *pFirst, BODY *delBody);
  25. void removeList(LIST **pFirst);
  26. int writeElm(LIST *pL, FILE *pF);
  27. int readElm(BODY *pB, FILE *pF);
  28. void printFloorBody(LIST *pFirst);
  29. /************************ MAIN ****************************************/
  30. int main()
  31. {
  32. LIST *pFirst = NULL, *p;
  33. int res, i, mode;
  34. FILE *pOut = NULL, *pIn = NULL;
  35. char Fname[] = "List_bin.dat";
  36. BODY building;
  37. char *menu[] = { "MENU:",
  38. "1-Enter the data for buildings",
  39. "2-Write the data into a binary file",
  40. "3-Edit data for buildings",
  41. "4-Display the list",
  42. "5-Display certain buildings by floor number",
  43. "6-Destroy the list and Exit" };
  44. do
  45. {
  46. //system("cls");
  47. for (i = 0; i < 7; i++)
  48. printf("\n%s\n", menu[i]);
  49. do
  50. {
  51. fflush(stdin);
  52. printf("\n\nChoose mode[1-6]: ");
  53. res = scanf("%d", &mode);
  54. } while (res != 1);
  55. switch (mode)
  56. {
  57. case 1:
  58. for (i = 0; i < LOOPS; i++)
  59. {
  60. res = enterBody(&building);
  61. if (res != 1)
  62. {
  63. printf("Error in initialization %d \n", res);
  64. break;
  65. }
  66. p = insertBegin(pFirst, building);
  67. if (p == NULL)
  68. {
  69. printf("Not enough memory");
  70. break;
  71. }
  72. pFirst = p;
  73. }
  74. //system("pause");
  75. break;
  76. case 2:
  77. {
  78. pOut = fopen(Fname, "wb");
  79. if (pOut == NULL)
  80. {
  81. printf("Can't open file for writing!");
  82. removeList(&pFirst);
  83. break;
  84. }
  85. for (p = pFirst; p != NULL; p = p->pNext)
  86. {
  87. res = writeElm(p, pOut);
  88. if (res != 1)
  89. {
  90. printf("Writing error %d \n", res);
  91. break;
  92. }
  93. }
  94. fclose(pOut);
  95. removeList(&pFirst);
  96. }
  97. //system("pause");
  98. break;
  99. case 3:
  100. {
  101. pIn = fopen(Fname, "rb");
  102. if (pIn == NULL)
  103. {
  104. printf("Can't open file for reading!");
  105. break;
  106. }
  107. do
  108. {
  109. res = readElm(&building, pIn);
  110. if (res != 1 && res != -4)
  111. {
  112. printf("Reading error %d \n", res);
  113. break;
  114. }
  115. if (res != -4)
  116. {
  117. p = insertBegin(pFirst, building);
  118. if (p == NULL)
  119. {
  120. printf("Not enough memory");
  121. break;
  122. }
  123. pFirst = p;
  124. }
  125. } while (res == 1);
  126. fclose(pIn);
  127. }
  128. //system("pause");
  129. break;
  130. case 4:
  131. if (pFirst != NULL) {
  132. printf("Id:\tNameNeighborhood:\t\tNameSuper:\t\tNumberFloors\tNumberApartments\t\n");
  133. print(pFirst);
  134. }
  135. else
  136. printf("\nEmpty list!\n");
  137. //system("pause");
  138. break;
  139. case 5:
  140. if(pFirst != NULL)
  141. printFloorBody(pFirst);
  142. //system("pause");
  143. break;
  144. case 6:
  145. if (pFirst != NULL)
  146. removeList(&pFirst);
  147. printf("\nEmpty list!\n");
  148. break;
  149. default:
  150. printf("\nBad choice!\n");
  151. //system("pause");
  152. }
  153. } while (mode != 6);
  154. return 0;
  155. }
  156. /************************ FUNCTIONS ***********************************/
  157.  
  158. void removeList(LIST **pFirst)
  159. {
  160. BODY first;
  161. while (*pFirst != NULL)
  162. *pFirst = delFirst(*pFirst, &first);
  163. }
  164. int writeElm(LIST *pL, FILE *pF)
  165. {
  166. int res;
  167. if (pL == NULL) return -1;
  168. if (pF == NULL) return -2;
  169. res = fwrite(&(pL->body), sizeof(BODY), 1, pF);
  170. if (res == 1)
  171. return 1;
  172. else
  173. return -3;
  174. }
  175. int readElm(BODY *pB, FILE *pF)
  176. {
  177. int res;
  178. if (pB == NULL) return -1;
  179. if (pF == NULL) return -2;
  180. res = fread(pB, sizeof(BODY), 1, pF);
  181. if (res == 1) return 1;
  182. else {
  183. if (feof(pF)) return -4;
  184. return -3;
  185. }
  186. }
  187. void print(LIST *pFirst)
  188. {
  189. int res;
  190. if (pFirst == NULL)
  191. printf("Empty list.\n");
  192. else
  193. {
  194. LIST *p;
  195. p = pFirst;
  196. while (p != NULL)
  197. {
  198. res = printBody(p->body);
  199. p = p->pNext;
  200. }
  201. printf("\n");
  202. }
  203. }
  204. LIST *insertBegin(LIST *pFirst, BODY newBody)
  205. {
  206. LIST *p;
  207. p = (LIST *)malloc(sizeof(LIST));
  208. if (p == NULL)
  209. {
  210. printf("There is not memory!\n");
  211. return NULL;
  212. }
  213. else
  214. {
  215. p->body = newBody;
  216. p->pNext = pFirst;
  217. pFirst = p;
  218. return p;
  219. }
  220. }
  221. LIST *delFirst(LIST *pFirst, BODY *delBody)
  222. {
  223. if (pFirst == NULL)
  224. {
  225. printf("Empty list!\n");
  226. return NULL;
  227. }
  228. else
  229. {
  230. LIST *p;
  231. *delBody = pFirst->body;
  232. p = pFirst->pNext;
  233. if (p != NULL)
  234. free(pFirst);
  235. pFirst = p;
  236. return pFirst;
  237. }
  238. }
  239. int enterBody(BODY *ps)
  240. {
  241. char buffer[BUFSIZ];
  242. int res;
  243. if (ps == NULL)
  244. return 0;
  245. memset(ps, 0, sizeof(BODY));
  246. do
  247. {
  248. fflush(stdin);
  249. printf("\nID: ");
  250. res = scanf("%d", &(ps->id));
  251. if (res == EOF)
  252. return 0;
  253. } while (res == 0 || ps->id < 0);
  254. fflush(stdin);
  255. printf("\nName of neighborhood: ");
  256. if (fgets(ps->nameN, sizeof(ps->nameN), stdin) == NULL)
  257. return 0;
  258. if (ps->nameN[0] == '\0')
  259. strcpy(ps->nameN, "?");
  260. if (strlen(ps->nameN) > MAX_LENGTH)
  261. ps->nameN[MAX_LENGTH] = '\0';
  262. fflush(stdin);
  263. printf("\nName of super: ");
  264. if (fgets(ps->nameSuper, sizeof(ps->nameSuper), stdin) == NULL)
  265. return 0;
  266. if (ps->nameSuper[0] == '\0')
  267. strcpy(ps->nameSuper, "?");
  268. if (strlen(ps->nameSuper) > MAX_LENGTH)
  269. ps->nameSuper[MAX_LENGTH] = '\0';
  270. do
  271. {
  272. fflush(stdin);
  273. printf("\nNumber of floors: ");
  274. res = scanf("%d", &(ps->numFloors));
  275. if (res == EOF)
  276. return 0;
  277. } while (res == 0 || ps->numFloors < 0);
  278. do
  279. {
  280. fflush(stdin);
  281. printf("\nNumber of apartments: ");
  282. res = scanf("%d", &(ps->numApp));
  283. if (res == EOF)
  284. return 0;
  285. } while (res == 0 || ps->numApp < 0);
  286. return 1;
  287. }
  288. int printBody(BODY s)
  289. {
  290. int res;
  291. res = printf("%9d\t%-20s%20s\t%3d\t%3d\n", s.id, s.nameN, s.nameSuper, s.numFloors, s.numApp);
  292. if (res < 0) return 0;
  293. else
  294. return 1;
  295. }
  296.  
  297. void printFloorBody(LIST *pFirst)
  298. {
  299.  
  300. LIST *p;
  301. p = pFirst;
  302. int number;
  303. printf("Enter numbers of floors:\n");
  304. scanf("%d", &number);
  305. while (p != NULL)
  306. {
  307. if (p->body.numFloors > number)
  308. {
  309. printBody(p->body);
  310. }
  311. p = p->pNext;
  312. }
  313.  
  314.  
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement