Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.22 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-Read the data from a binary file",
  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("\tId:\tNameNeighborhood:\t\tNameSuper:\t\tNumberFloors\tNumberApartments\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.     ps->nameN[strlen(ps->nameN)-1] = '\0';
  263.     fflush(stdin);
  264.     printf("\nName of super:  ");
  265.     if (fgets(ps->nameSuper, sizeof(ps->nameSuper), stdin) == NULL)
  266.         return  0;
  267.     if (ps->nameSuper[0] == '\0')
  268.         strcpy(ps->nameSuper, "?");
  269.     if (strlen(ps->nameSuper) > MAX_LENGTH)
  270.         ps->nameSuper[MAX_LENGTH] = '\0';
  271.     ps->nameSuper[strlen(ps->nameSuper)-1] = '\0';
  272.     do
  273.     {
  274.         fflush(stdin);
  275.         printf("\nNumber of floors: ");
  276.         res = scanf("%d", &(ps->numFloors));
  277.         if (res == EOF)
  278.             return  0;
  279.     } while (res == 0 || ps->numFloors < 0);
  280.     do
  281.     {
  282.         fflush(stdin);
  283.         printf("\nNumber of apartments: ");
  284.         res = scanf("%d", &(ps->numApp));
  285.         if (res == EOF)
  286.             return  0;
  287.     } while (res == 0 || ps->numApp < 0);
  288.     return  1;
  289. }
  290. int printBody(BODY s)
  291. {
  292.     int res;
  293.     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);
  294.     if (res < 0)    return 0;
  295.     else
  296.         return 1;
  297. }
  298.  
  299. void printFloorBody(LIST *pFirst)
  300. {
  301.  
  302.     LIST *p;
  303.     p = pFirst;
  304.     int number;
  305.     printf("Enter numbers of floors:\n");
  306.     scanf("%d", &number);
  307.     while (p != NULL)
  308.     {
  309.         if (p->body.numFloors > number)
  310.         {
  311.             printBody(p->body);
  312.         }
  313.         p = p->pNext;
  314.     }
  315.  
  316.  
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement