Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //#include <malloc.h>
- #include <string.h>
- #define LOOPS 2
- #define MAX_LENGTH 50
- struct building
- {
- int id, numFloors, numApp;
- char nameN[MAX_LENGTH + 1], nameSuper[MAX_LENGTH + 1];
- int group;
- };
- typedef struct building BODY;
- struct List
- {
- BODY body;
- struct List *pNext;
- };
- typedef struct List LIST;
- int enterBody(BODY *ps);
- int printBody(BODY s);
- void print(LIST *pFirst);
- LIST *insertBegin(LIST *pFirst, BODY newBody);
- LIST *delFirst(LIST *pFirst, BODY *delBody);
- void removeList(LIST **pFirst);
- int writeElm(LIST *pL, FILE *pF);
- int readElm(BODY *pB, FILE *pF);
- void printFloorBody(LIST *pFirst);
- /************************ MAIN ****************************************/
- int main()
- {
- LIST *pFirst = NULL, *p;
- int res, i, mode;
- FILE *pOut = NULL, *pIn = NULL;
- char Fname[] = "List_bin.dat";
- BODY building;
- char *menu[] = { "MENU:",
- "1-Enter the data for buildings",
- "2-Write the data into a binary file",
- "3-Read the data from a binary file",
- "4-Display the list",
- "5-Display certain buildings by floor number",
- "6-Destroy the list and Exit" };
- do
- {
- system("cls");
- for (i = 0; i < 7; i++)
- printf("\n%s\n", menu[i]);
- do
- {
- fflush(stdin);
- printf("\n\nChoose mode[1-6]: ");
- res = scanf("%d", &mode);
- } while (res != 1);
- switch (mode)
- {
- case 1:
- for (i = 0; i < LOOPS; i++)
- {
- res = enterBody(&building);
- if (res != 1)
- {
- printf("Error in initialization %d \n", res);
- break;
- }
- p = insertBegin(pFirst, building);
- if (p == NULL)
- {
- printf("Not enough memory");
- break;
- }
- pFirst = p;
- }
- system("pause");
- break;
- case 2:
- {
- pOut = fopen(Fname, "wb");
- if (pOut == NULL)
- {
- printf("Can't open file for writing!");
- removeList(&pFirst);
- break;
- }
- for (p = pFirst; p != NULL; p = p->pNext)
- {
- res = writeElm(p, pOut);
- if (res != 1)
- {
- printf("Writing error %d \n", res);
- break;
- }
- }
- fclose(pOut);
- removeList(&pFirst);
- }
- system("pause");
- break;
- case 3:
- {
- pIn = fopen(Fname, "rb");
- if (pIn == NULL)
- {
- printf("Can't open file for reading!");
- break;
- }
- do
- {
- res = readElm(&building, pIn);
- if (res != 1 && res != -4)
- {
- printf("Reading error %d \n", res);
- break;
- }
- if (res != -4)
- {
- p = insertBegin(pFirst, building);
- if (p == NULL)
- {
- printf("Not enough memory");
- break;
- }
- pFirst = p;
- }
- } while (res == 1);
- fclose(pIn);
- }
- system("pause");
- break;
- case 4:
- if (pFirst != NULL) {
- printf("\tId:\tNameNeighborhood:\t\tNameSuper:\t\tNumberFloors\tNumberApartments\n");
- print(pFirst);
- }
- else
- printf("\nEmpty list!\n");
- system("pause");
- break;
- case 5:
- if(pFirst != NULL)
- printFloorBody(pFirst);
- system("pause");
- break;
- case 6:
- if (pFirst != NULL)
- removeList(&pFirst);
- printf("\nEmpty list!\n");
- break;
- default:
- printf("\nBad choice!\n");
- system("pause");
- }
- } while (mode != 6);
- return 0;
- }
- /************************ FUNCTIONS ***********************************/
- void removeList(LIST **pFirst)
- {
- BODY first;
- while (*pFirst != NULL)
- *pFirst = delFirst(*pFirst, &first);
- }
- int writeElm(LIST *pL, FILE *pF)
- {
- int res;
- if (pL == NULL) return -1;
- if (pF == NULL) return -2;
- res = fwrite(&(pL->body), sizeof(BODY), 1, pF);
- if (res == 1)
- return 1;
- else
- return -3;
- }
- int readElm(BODY *pB, FILE *pF)
- {
- int res;
- if (pB == NULL) return -1;
- if (pF == NULL) return -2;
- res = fread(pB, sizeof(BODY), 1, pF);
- if (res == 1) return 1;
- else {
- if (feof(pF)) return -4;
- return -3;
- }
- }
- void print(LIST *pFirst)
- {
- int res;
- if (pFirst == NULL)
- printf("Empty list.\n");
- else
- {
- LIST *p;
- p = pFirst;
- while (p != NULL)
- {
- res = printBody(p->body);
- p = p->pNext;
- }
- printf("\n");
- }
- }
- LIST *insertBegin(LIST *pFirst, BODY newBody)
- {
- LIST *p;
- p = (LIST *)malloc(sizeof(LIST));
- if (p == NULL)
- {
- printf("There is not memory!\n");
- return NULL;
- }
- else
- {
- p->body = newBody;
- p->pNext = pFirst;
- pFirst = p;
- return p;
- }
- }
- LIST *delFirst(LIST *pFirst, BODY *delBody)
- {
- if (pFirst == NULL)
- {
- printf("Empty list!\n");
- return NULL;
- }
- else
- {
- LIST *p;
- *delBody = pFirst->body;
- p = pFirst->pNext;
- if (p != NULL)
- free(pFirst);
- pFirst = p;
- return pFirst;
- }
- }
- int enterBody(BODY *ps)
- {
- char buffer[BUFSIZ];
- int res;
- if (ps == NULL)
- return 0;
- memset(ps, 0, sizeof(BODY));
- do
- {
- fflush(stdin);
- printf("\nID: ");
- res = scanf("%d", &(ps->id));
- if (res == EOF)
- return 0;
- } while (res == 0 || ps->id < 0);
- fflush(stdin);
- printf("\nName of neighborhood: ");
- if (fgets(ps->nameN, sizeof(ps->nameN), stdin) == NULL)
- return 0;
- if (ps->nameN[0] == '\0')
- strcpy(ps->nameN, "?");
- if (strlen(ps->nameN) > MAX_LENGTH)
- ps->nameN[MAX_LENGTH] = '\0';
- ps->nameN[strlen(ps->nameN)-1] = '\0';
- fflush(stdin);
- printf("\nName of super: ");
- if (fgets(ps->nameSuper, sizeof(ps->nameSuper), stdin) == NULL)
- return 0;
- if (ps->nameSuper[0] == '\0')
- strcpy(ps->nameSuper, "?");
- if (strlen(ps->nameSuper) > MAX_LENGTH)
- ps->nameSuper[MAX_LENGTH] = '\0';
- ps->nameSuper[strlen(ps->nameSuper)-1] = '\0';
- do
- {
- fflush(stdin);
- printf("\nNumber of floors: ");
- res = scanf("%d", &(ps->numFloors));
- if (res == EOF)
- return 0;
- } while (res == 0 || ps->numFloors < 0);
- do
- {
- fflush(stdin);
- printf("\nNumber of apartments: ");
- res = scanf("%d", &(ps->numApp));
- if (res == EOF)
- return 0;
- } while (res == 0 || ps->numApp < 0);
- return 1;
- }
- int printBody(BODY s)
- {
- int res;
- res = printf("\t%d%20s\t\t%20s\t\t\t%d\t\t\t%d\n", s.id, s.nameN, s.nameSuper, s.numFloors, s.numApp);
- if (res < 0) return 0;
- else
- return 1;
- }
- void printFloorBody(LIST *pFirst)
- {
- LIST *p;
- p = pFirst;
- int number;
- printf("Enter numbers of floors:\n");
- scanf("%d", &number);
- while (p != NULL)
- {
- if (p->body.numFloors > number)
- {
- printBody(p->body);
- }
- p = p->pNext;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement