Advertisement
VolodiaKost

VIPZ3

Mar 26th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.11 KB | None | 0 0
  1. #include "header.h"
  2.  
  3. int main()
  4. {
  5.     FILE* fpInput = NULL, * fpOutput = NULL;
  6.     sBook* pHead = NULL;
  7.     char cUserMenuChoice;
  8.  
  9.     if ((fpInput = fopen("D:\\Projects\\ListOfBooks(v1.1)\\ListOfBooks\\booklist.txt",
  10.         "r")) == NULL)
  11.     {
  12.         printf("Can't open the input file!");
  13.         exit(1);
  14.     }
  15.     if ((fpOutput = fopen("D:\\Projects\\ListOfBooks(v1.1)\\ListOfBooks\\booklistres.txt",
  16.         "w")) == NULL)
  17.     {
  18.         printf("Can't create the result file!");
  19.         exit(1);
  20.     }
  21.  
  22.     pHead = CreateLinkedListFromFile(fpInput);
  23.     int numberOfBooks = CountBooksInList(pHead);
  24.     ShowUserMenu();
  25.  
  26.     while ((cUserMenuChoice = GetChoiceForMenu()) != 'i')
  27.     {
  28.         cout << endl;
  29.         switch (cUserMenuChoice) {
  30.         case 'a':   PrintTable(pHead);                          break;
  31.         case 'b':   DeleteCheapBooks(&pHead);                   break;
  32.         case 'c':   PrintTwoThickestBooks(pHead);               break;
  33.         case 'd':   AddUserBookToList(&pHead);                  break;
  34.         case 'e':   DeleteBookFromList(&pHead);                 break;
  35.         case 'f':   numberOfBooks = CountBooksInList(pHead);
  36.             for (int i = 0; i < (numberOfBooks * numberOfBooks); ++i)
  37.                 SortListByAuthorsLastName(&pHead);
  38.             cout << endl << "\tList was sorted by author's last name" << endl; break;
  39.         case 'g':   numberOfBooks = CountBooksInList(pHead);
  40.             for (int i = 0; i < (numberOfBooks * numberOfBooks); ++i)
  41.                 SortByPrice(&pHead);
  42.             cout << endl << "\tList was sorted by price" << endl; break;
  43.         case 'h':   CalculatePriceOfAllBooks(pHead);            break;
  44.         }
  45.         cout << endl << "Choose another operation or press 'i' to exit : ";
  46.     }
  47.  
  48.     PrintTableIntoFile(pHead, fpOutput);
  49.     FreeMemory(&pHead);
  50.     fclose(fpInput);
  51.     fclose(fpOutput);
  52.     cout << endl << endl << "\t\tExit..." << endl << endl;
  53.     return 0;
  54. }
  55.  
  56.  
  57.  
  58. #include "header.h"
  59.  
  60. //-----------------------------------------------------------------------------
  61.  
  62. void ShowUserMenu(void)
  63. {
  64.     PrintLotOfCharacters('+');
  65.     cout << "\t\t\t\t\t\tChoose an operation \n"
  66.         "\t\t\t\t\t a) Print book list \n"
  67.         "\t\t\t\t\t b) Delete cheap books(under 100$) \n"
  68.         "\t\t\t\t\t c) Print two thickest books \n"
  69.         "\t\t\t\t\t d) Add book to list \n"
  70.         "\t\t\t\t\t e) Delete book from list \n"
  71.         "\t\t\t\t\t f) Sort by author's last name \n"
  72.         "\t\t\t\t\t g) Sort by price \n"
  73.         "\t\t\t\t\t h) Calculate total price \n";
  74.     PrintLotOfCharacters('+');
  75.     printf("\t\t\t\t\tOperation: ");
  76. }
  77.  
  78. //-----------------------------------------------------------------------------
  79.  
  80. void CalculatePriceOfAllBooks(sBook* pHead)
  81. {
  82.     double sum = 0;
  83.     sBook* ptr = pHead;
  84.     while (ptr != NULL)
  85.     {
  86.         sum += ptr->dPrice;
  87.         ptr = ptr->next;
  88.     }
  89.     cout << endl << "\tTotal price = " << sum << endl;
  90. }
  91.  
  92. //-----------------------------------------------------------------------------
  93.  
  94. int YesOrNo(void)
  95. {
  96.     int iReturnValue;
  97.     cout << "Your choice (1 - YES | 0 - NO) : ";
  98.     while (!(cin >> iReturnValue) || iReturnValue < 0 || iReturnValue > 1)
  99.     {
  100.         cout << "Re-enter the number (1 - YES | 0 - NO) : ";
  101.         cin.clear();
  102.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  103.     }
  104.     return iReturnValue;
  105. }
  106.  
  107. //-----------------------------------------------------------------------------
  108.  
  109. int GetPosition(sBook* pHead)
  110. {
  111.     int iReturnValue;
  112.     int iNumOfBooks = CountBooksInList(pHead);
  113.     while (!(cin >> iReturnValue) || iReturnValue < 1 || iReturnValue > NUMBER_OF_BOOKS || iReturnValue > iNumOfBooks + 1)
  114.     {
  115.         cout << "Re-enter the number (min 1 - max " << iNumOfBooks + 1 << ") : ";
  116.         cin.clear();
  117.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  118.     }
  119.     return iReturnValue;
  120. }
  121.  
  122. //-----------------------------------------------------------------------------
  123.  
  124. int GetPosition(void)
  125. {
  126.     int iReturnValue;
  127.     while (!(cin >> iReturnValue) || iReturnValue < 1 || iReturnValue > NUMBER_OF_BOOKS)
  128.     {
  129.         cout << "Re-enter the number (min 1 - max 30) : ";
  130.         cin.clear();
  131.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  132.     }
  133.     return iReturnValue;
  134. }
  135.  
  136. //-----------------------------------------------------------------------------
  137.  
  138. int GetYear(void)
  139. {
  140.     int iReturnValue;
  141.     while (!(cin >> iReturnValue) || iReturnValue < 1 || iReturnValue > 2020)
  142.     {
  143.         cout << "Re-enter the number (min 1 - max 2020) : ";
  144.         cin.clear();
  145.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  146.     }
  147.     return iReturnValue;
  148. }
  149.  
  150. //-----------------------------------------------------------------------------
  151.  
  152. double GetPrice(void)
  153. {
  154.     double dReturnValue;
  155.     while (!(cin >> dReturnValue) || dReturnValue < 0.00 || dReturnValue > 9999.99)
  156.     {
  157.         cout << "Re-enter the number (min 0.00 - max 9999.99) : ";
  158.         cin.clear();
  159.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  160.     }
  161.     return dReturnValue;
  162. }
  163.  
  164. //-----------------------------------------------------------------------------
  165.  
  166. int GetPages(void)
  167. {
  168.     int iReturnValue;
  169.     while (!(cin >> iReturnValue) || iReturnValue < 1 || iReturnValue > 9999)
  170.     {
  171.         cout << "Re-enter the number (min 1 - max 9999) : ";
  172.         cin.clear();
  173.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  174.     }
  175.     return iReturnValue;
  176. }
  177.  
  178. //-----------------------------------------------------------------------------
  179.  
  180. char GetChoiceForMenu(void)
  181. {
  182.     char cUserChoice = getche();
  183.     while (cUserChoice > 'i' || cUserChoice < 'a')
  184.     {
  185.         cout << endl;
  186.         printf("Please, re-enter the character (a-i) : ");
  187.         cUserChoice = getche();
  188.     }
  189.     return cUserChoice;
  190. }
  191.  
  192. //-----------------------------------------------------------------------------
  193.  
  194. int CountBooksInList(sBook* pHead)
  195. {
  196.     int iCount = 0;
  197.     sBook* ptr = pHead;
  198.     while (ptr != NULL)
  199.     {
  200.         ++iCount;
  201.         ptr = ptr->next;
  202.     }
  203.     return iCount;
  204. }
  205.  
  206. //-----------------------------------------------------------------------------
  207.  
  208. void PrintLotOfCharacters(char cSymbol)
  209. {
  210.     putchar('|');
  211.     for (int i = 0; i < TABLE_SIZE; ++i)
  212.         putchar(cSymbol);
  213.     putchar('|');
  214.     putchar('\n');
  215. }
  216.  
  217. //-----------------------------------------------------------------------------
  218.  
  219. void PrintTable(sBook* pHead) {
  220.     sBook* ptr = pHead;
  221.     if (ptr == NULL)
  222.     {
  223.         cout << endl << "\tNo books in list" << endl;
  224.         return;
  225.     }
  226.     int iCount = 1;
  227.     cout << endl;
  228.     PrintLotOfCharacters('*');
  229.     printf("%70s\n", "BOOK LIST");
  230.     PrintLotOfCharacters('*');
  231.     PrintLotOfCharacters('-');
  232.     printf("|  N |  %16s    | %17s     | %35s       %20s %s %s \n", "First name",
  233.         "Last name", "Book title", "| Year |", " pp. |", "   $    |");
  234.     PrintLotOfCharacters('-');
  235.  
  236.     while (ptr != NULL)
  237.     {
  238.         printf("|%3d)|%21s | %21s | %53s | %4d | %4d "
  239.             "| %7.2lf |\n", iCount, ptr->czAuthorFirstName,
  240.             ptr->czAuthorLastName, ptr->czBookTitle,
  241.             ptr->iReleaseDate, ptr->iPages, ptr->dPrice);
  242.         PrintLotOfCharacters('-');
  243.         ptr = ptr->next;
  244.         ++iCount;
  245.     }
  246. }
  247.  
  248. //-----------------------------------------------------------------------------
  249.  
  250. void FreeMemory(sBook** pHead)
  251. {
  252.     sBook* pCurrent = *pHead;
  253.     sBook* ptr;
  254.     while (pCurrent != NULL) {
  255.         ptr = pCurrent->next;
  256.         free(pCurrent);
  257.         pCurrent = ptr;
  258.     }
  259.     *pHead = NULL;
  260. }
  261.  
  262. //-----------------------------------------------------------------------------
  263.  
  264. void PrintTableIntoFile(sBook* pHead, FILE* fp)
  265. {
  266.     sBook* ptr = pHead;
  267.     if (pHead == NULL)
  268.         fprintf(fp, "\t! No books in list !");
  269.  
  270.     else
  271.     {
  272.         int iCount = 1;
  273.         fprintf(fp, "%70s\n", "BOOK LIST");
  274.         fprintf(fp, "|  N |  %16s    | %17s     | %35s       %20s %s %s \n", "First name",
  275.             "Last name", "Book title", "| Year |", " pp. |", "   $    |");
  276.  
  277.         while (ptr != NULL)
  278.         {
  279.             fprintf(fp, "|%3d)|%21s | %21s | %53s | %4d | %4d "
  280.                 "| %7.2lf |\n", iCount, ptr->czAuthorFirstName,
  281.                 ptr->czAuthorLastName, ptr->czBookTitle,
  282.                 ptr->iReleaseDate, ptr->iPages, ptr->dPrice);
  283.             ptr = ptr->next;
  284.             ++iCount;
  285.         }
  286.     }
  287.     cout << endl << endl << "\tTable was printed into a file";
  288. }
  289.  
  290. //-----------------------------------------------------------------------------
  291.  
  292. void SortByPrice(sBook** pHead)
  293. {
  294.     int number_of_books = CountBooksInList(*pHead);
  295.     sBook* pSeek = NULL, * pTop = (*pHead);
  296.     sBook temp;
  297.  
  298.     for (int i = 1; i < number_of_books; ++i)
  299.     {
  300.         if (pTop->next != NULL)
  301.             pSeek = pTop->next;
  302.  
  303.         for (int j = i + 1; j <= number_of_books; ++j)
  304.         {
  305.             if (pTop->dPrice < pSeek->dPrice)
  306.                 SwapTwoNodes(pHead, i, j);
  307.  
  308.             else if (pTop->dPrice == pSeek->dPrice)
  309.             {
  310.                 if (strncmp(pTop->czAuthorLastName, pSeek->czAuthorLastName, 20) > 0)
  311.                     SwapTwoNodes(pHead, i, j);
  312.  
  313.                 else if (strncmp(pTop->czAuthorLastName, pSeek->czAuthorLastName, 20) == 0)
  314.                 {
  315.                     if (strncmp(pTop->czAuthorFirstName, pSeek->czAuthorFirstName, 20) > 0)
  316.                         SwapTwoNodes(pHead, i, j);
  317.  
  318.                     else if (strncmp(pTop->czAuthorFirstName, pSeek->czAuthorFirstName, 20) == 0)
  319.                     {
  320.                         if (strncmp(pTop->czBookTitle, pSeek->czBookTitle, 50) > 0)
  321.                             SwapTwoNodes(pHead, i, j);
  322.                     }
  323.                 }
  324.             }
  325.  
  326.             if (pSeek->next != NULL)
  327.                 pSeek = pSeek->next;
  328.         }
  329.  
  330.         if (pTop->next != NULL)
  331.             pTop = pTop->next;
  332.     }
  333. }
  334.  
  335. //-----------------------------------------------------------------------------
  336.  
  337. void SortListByAuthorsLastName(sBook** pHead)
  338. {
  339.     int number_of_books = CountBooksInList(*pHead);
  340.     sBook* pSeek = NULL, * pTop = (*pHead);
  341.     sBook temp;
  342.  
  343.     for (int i = 1; i < number_of_books; ++i)
  344.     {
  345.         if (pTop->next != NULL)
  346.             pSeek = pTop->next;
  347.  
  348.         for (int j = i + 1; j <= number_of_books; ++j)
  349.         {
  350.             if (strncmp(pTop->czAuthorLastName, pSeek->czAuthorLastName, 20) > 0)
  351.                 SwapTwoNodes(pHead, i, j);
  352.  
  353.             else if (strncmp(pTop->czAuthorLastName, pSeek->czAuthorLastName, 20) == 0)
  354.             {
  355.                 if (strncmp(pTop->czAuthorFirstName, pSeek->czAuthorFirstName, 20) > 0)
  356.                     SwapTwoNodes(pHead, i, j);
  357.  
  358.                 else if (strncmp(pTop->czAuthorFirstName, pSeek->czAuthorFirstName, 20) == 0)
  359.                 {
  360.                     if (strncmp(pTop->czBookTitle, pSeek->czBookTitle, 50) > 0)
  361.                         SwapTwoNodes(pHead, i, j);
  362.                 }
  363.             }
  364.  
  365.  
  366.  
  367.             if (pSeek->next != NULL)
  368.                 pSeek = pSeek->next;
  369.         }
  370.  
  371.         if (pTop->next != NULL)
  372.             pTop = pTop->next;
  373.     }
  374. }
  375.  
  376. //-----------------------------------------------------------------------------
  377.  
  378. void PrintTwoThickestBooks(sBook* pHead)
  379. {
  380.     int iNumberOfBooks = CountBooksInList(pHead);
  381.     sBook* temp1, * temp2, * ptr = NULL;
  382.     if (iNumberOfBooks <= 1)
  383.     {
  384.         cout << endl << "\tToo few books in list" << endl;
  385.         return;
  386.     }
  387.  
  388.     if (pHead->next->next != NULL)
  389.         ptr = pHead->next->next;
  390.  
  391.     if (pHead->iPages >= pHead->next->iPages)
  392.     {
  393.         temp1 = pHead;
  394.         temp2 = pHead->next;
  395.     }
  396.  
  397.     else
  398.     {
  399.         temp1 = pHead->next;
  400.         temp2 = pHead;
  401.     }
  402.     sBook* temp3 = NULL;
  403.  
  404.     while (ptr != NULL)
  405.     {
  406.         if (ptr->iPages > temp2->iPages)
  407.         {
  408.             if (ptr->iPages > temp1->iPages&& temp1->iPages > temp2->iPages)
  409.             {
  410.                 temp2 = temp1;
  411.                 temp1 = ptr;
  412.             }
  413.  
  414.             else if (ptr->iPages > temp1->iPages)
  415.                 temp1 = ptr;
  416.  
  417.             else
  418.                 temp2 = ptr;
  419.  
  420.         }
  421.         ptr = ptr->next;
  422.     }
  423.  
  424.     PrintLotOfCharacters('=');
  425.     printf("\t\t\tTwo thickest books are:\n\t\t"
  426.         "1) %s %s %s - %d\n\t\t2) %s %s %s - %d\n",
  427.         temp1->czAuthorFirstName, temp1->czAuthorLastName,
  428.         temp1->czBookTitle, temp1->iPages, temp2->czAuthorFirstName,
  429.         temp2->czAuthorLastName, temp2->czBookTitle, temp2->iPages);
  430.     PrintLotOfCharacters('=');
  431. }
  432.  
  433. //-----------------------------------------------------------------------------
  434.  
  435. void DeleteCheapBooks(sBook** pHead)
  436. {
  437.     if (*pHead == NULL)
  438.     {
  439.         cout << endl << "\tNo books in list!" << endl;
  440.         return;
  441.     }
  442.  
  443.     sBook* ptr = *pHead;
  444.     sBook* pTemp = *pHead;
  445.     sBook* tmp = NULL;
  446.     int iNumOfBooks = CountBooksInList(*pHead);
  447.     int iCount = 0;
  448.     int iIterator = 1;
  449.  
  450.     while (ptr != NULL)
  451.     {
  452.         if (ptr->dPrice < PRICE)
  453.         {
  454.             if (ptr == (*pHead))
  455.             {
  456.                 do
  457.                 {
  458.                     if ((*pHead) != NULL)
  459.                         pTemp = (*pHead)->next;
  460.  
  461.                     if (ptr != NULL)
  462.                         free(ptr);
  463.  
  464.                     (*pHead) = pTemp;
  465.                     ++iCount;
  466.                     ptr = (*pHead);
  467.  
  468.                 } while (ptr != NULL && ptr->dPrice < PRICE);
  469.             }
  470.  
  471.             else if (ptr->next != NULL)
  472.             {
  473.                 pTemp->next = ptr->next;
  474.                 free(ptr);
  475.                 ptr = pTemp;
  476.                 ++iCount;
  477.             }
  478.  
  479.             else
  480.             {
  481.                 pTemp->next = NULL;
  482.                 ++iCount;
  483.                 free(ptr);
  484.                 ptr = NULL;
  485.             }
  486.         }
  487.  
  488.         if (ptr != NULL)
  489.         {
  490.             pTemp = ptr;
  491.             ptr = ptr->next;
  492.         }
  493.     }
  494.  
  495.     if (iCount == iNumOfBooks)
  496.         cout << "\n\tAll books was deleted\n";
  497.  
  498.     else if (iCount == 0)
  499.         cout << "\n\tNo books was deleted\n";
  500.  
  501.     else
  502.         cout << "\n\t" << iCount << " books was deleted\n";
  503.  
  504. }
  505.  
  506. //-----------------------------------------------------------------------------
  507.  
  508. void SwapTwoNodes(sBook** pHead, int iFirstNode, int iSecondNode)
  509. {
  510.     sBook* ptr = *pHead, * pPreviousNode = NULL;
  511.     sBook* pNodeX = NULL, * pPreviousNodeX = NULL;
  512.     sBook* pNodeY = NULL, * pPreviousNodeY = NULL;
  513.     sBook* pTemp;
  514.     int iFirstNodeIterator = 1, iSecondNodeIterator = 1;
  515.     int iNumberOfBooks = CountBooksInList(*pHead);
  516.  
  517.     if (iFirstNode > iSecondNode)
  518.     {
  519.         int temp = iFirstNode;
  520.         iFirstNode = iSecondNode;
  521.         iSecondNode = temp;
  522.     }
  523.  
  524.     if (iFirstNode == 1)
  525.     {                                            //first node == pHead
  526.         if (iSecondNode == 2)
  527.         {                                       //second node == pHead->next
  528.             pNodeX = *pHead;
  529.             pNodeY = pNodeX->next;
  530.             pTemp = pNodeY;
  531.             pNodeX->next = pNodeY->next;
  532.             pNodeY->next = pNodeX;
  533.             *pHead = pTemp;
  534.         }
  535.  
  536.         else
  537.         {
  538.             pNodeX = ptr = *pHead;
  539.             pPreviousNodeX = pPreviousNode = NULL;
  540.             while (ptr != NULL && iSecondNodeIterator != iSecondNode)
  541.             {
  542.                 pPreviousNode = ptr;
  543.                 ptr = ptr->next;
  544.                 ++iSecondNodeIterator;
  545.             }
  546.  
  547.             pNodeY = ptr;
  548.             pPreviousNodeY = pPreviousNode;
  549.             pTemp = pNodeY->next;
  550.             pNodeY->next = pNodeX->next;
  551.             pNodeX->next = pTemp;
  552.             *pHead = pNodeY;
  553.             pPreviousNodeY->next = pNodeX;
  554.         }
  555.     }
  556.     else if (iSecondNode == iNumberOfBooks)
  557.     {                                                                          //Second node == last node
  558.         if (iFirstNode == iSecondNode - 1)
  559.         {                                                                    //Second node == FirstNode->next  
  560.             while (iFirstNodeIterator != iFirstNode)
  561.             {
  562.                 pPreviousNode = ptr;
  563.                 ptr = ptr->next;
  564.                 ++iFirstNodeIterator;
  565.             }
  566.  
  567.             pPreviousNodeX = pPreviousNode;
  568.             pNodeX = ptr;
  569.             pNodeY = ptr->next;
  570.             pNodeY->next = NULL;
  571.  
  572.             pNodeY->next = pNodeX;
  573.             pPreviousNodeX->next = pNodeY;
  574.             pNodeX->next = NULL;
  575.         }
  576.         else
  577.         {
  578.             while (iFirstNodeIterator != iFirstNode)
  579.             {
  580.                 pPreviousNode = ptr;
  581.                 ptr = ptr->next;
  582.                 ++iFirstNodeIterator;
  583.             }
  584.  
  585.             pPreviousNodeX = pPreviousNode;
  586.             pNodeX = ptr;
  587.  
  588.             while (ptr->next != NULL &&
  589.                 iSecondNodeIterator != iSecondNode)
  590.             {
  591.                 pPreviousNode = ptr;
  592.                 ptr = ptr->next;
  593.                 ++iSecondNodeIterator;
  594.             }
  595.  
  596.             pPreviousNodeY = pPreviousNode;
  597.             pNodeY = ptr;
  598.  
  599.             pNodeY->next = pNodeX->next;
  600.             pTemp = pNodeX;
  601.             pPreviousNodeX->next = pNodeY;
  602.             pPreviousNodeY->next = pTemp;
  603.             pNodeX->next = NULL;
  604.         }
  605.     }
  606.     else
  607.     {
  608.         if (iSecondNode == iFirstNode + 1)
  609.         {                                               //nodes are neighbours
  610.             while (iFirstNodeIterator != iFirstNode)
  611.             {
  612.                 pPreviousNode = ptr;
  613.                 ptr = ptr->next;
  614.                 ++iFirstNodeIterator;
  615.             }
  616.  
  617.             pPreviousNodeX = pPreviousNode;
  618.             pNodeX = ptr;
  619.             pNodeY = ptr->next;
  620.  
  621.             pTemp = pNodeY->next;
  622.             pNodeY->next = pNodeX;
  623.             pPreviousNodeX->next = pNodeY;
  624.             pNodeX->next = pTemp;
  625.         }
  626.         else
  627.         {
  628.             while (iFirstNodeIterator != iFirstNode)
  629.             {
  630.                 pPreviousNode = ptr;
  631.                 ptr = ptr->next;
  632.                 ++iFirstNodeIterator;
  633.             }
  634.  
  635.             pPreviousNodeX = pPreviousNode;
  636.             pNodeX = ptr;
  637.  
  638.             iSecondNodeIterator = iFirstNodeIterator;
  639.  
  640.             while (iSecondNodeIterator != iSecondNode)
  641.             {
  642.                 pPreviousNode = ptr;
  643.                 ptr = ptr->next;
  644.                 ++iSecondNodeIterator;
  645.             }
  646.  
  647.             pPreviousNodeY = pPreviousNode;
  648.             pNodeY = ptr;
  649.  
  650.             pTemp = pNodeY->next;
  651.             pNodeY->next = pNodeX->next;
  652.             pPreviousNodeY->next = pNodeX;
  653.             pPreviousNodeX->next = pNodeY;
  654.             pNodeX->next = pTemp;
  655.         }
  656.     }
  657. }
  658.  
  659. //-----------------------------------------------------------------------------
  660.  
  661. void DeleteBookFromList(sBook** pHead)
  662. {
  663.     if (*pHead == NULL)
  664.     {
  665.         cout << endl << "\tNo books in list" << endl;
  666.         return;
  667.     }
  668.     cout << endl << "\tEnter the position of book you want to delete : ";
  669.     int iIterator = 1;
  670.     int iBookToDelete = GetPosition();
  671.     int iNumberOfBooks = CountBooksInList(*pHead);
  672.  
  673.     if (iBookToDelete > iNumberOfBooks)
  674.     {
  675.         printf("\n\tThere are only %d books\n", iNumberOfBooks);
  676.         return;
  677.     }
  678.  
  679.     sBook* ptr = *pHead, * pTemp = NULL;
  680.  
  681.     if (iBookToDelete == 1)
  682.     {
  683.         pTemp = *pHead;
  684.         *pHead = (*pHead)->next;
  685.         cout << endl << "\tFirst book in list was successfully deleted" << endl;
  686.         free(pTemp);
  687.     }
  688.     else if (iBookToDelete == iNumberOfBooks)
  689.     {
  690.         while (ptr->next != NULL)
  691.         {
  692.             pTemp = ptr;
  693.             ptr = ptr->next;
  694.         }
  695.         pTemp->next = NULL;
  696.         cout << endl << "\tLast book in list was successfully deleted" << endl;
  697.         free(ptr);
  698.     }
  699.     else
  700.     {
  701.         while (iIterator != iBookToDelete)
  702.         {
  703.             pTemp = ptr;
  704.             ptr = ptr->next;
  705.             ++iIterator;
  706.         }
  707.         pTemp->next = ptr->next;
  708.         cout << endl << "\tBook #" << iBookToDelete << " was successfully deleted" << endl;
  709.         free(ptr);
  710.     }
  711.  
  712. }
  713.  
  714. //-----------------------------------------------------------------------------
  715.  
  716. void AddUserBookToList(sBook** pHead)
  717. {
  718.     int iNumberOfBooks = CountBooksInList(*pHead);
  719.     if (iNumberOfBooks >= NUMBER_OF_BOOKS)
  720.     {
  721.         cout << endl << "\tThere is already max number of books" << endl;
  722.         return;
  723.     }
  724.  
  725.  
  726.     fflush(stdin);
  727.  
  728.     sBook* pUserNode = (sBook*)malloc(sizeof(sBook));
  729.  
  730.     char FirstName[LENGTH];
  731.     int i = 0;
  732.     bool ret = true;
  733.     do
  734.     {
  735.         cout << endl << "Enter author's first name. After name press SPACE button : ";
  736.         i = 0;
  737.         while (1)
  738.         {
  739.             FirstName[i] = getche();
  740.             if (FirstName[0] == ' ')
  741.             {
  742.                 FirstName[0] = '-';
  743.                 ++i;
  744.                 ret = false;
  745.                 break;
  746.             }
  747.             else if (FirstName[i] == ' ')
  748.             {
  749.                 ret = false;
  750.                 break;
  751.             }
  752.             if (!isalpha(FirstName[i]) || (isupper(FirstName[i]) && i != 0) || islower(FirstName[0]))
  753.                 break;
  754.             ++i;
  755.             if (i > LENGTH - 1)
  756.                 break;
  757.         }
  758.         if (ret)
  759.         {
  760.             cout << endl << "Something went wrong. Remember - only alphabet symbols (max - 20). Capital letter only at the beginning";
  761.             cout << endl << "Press space after name to end input or in the beginning if there is no author" << endl;
  762.         }
  763.     } while (ret);
  764.     FirstName[i] = '\0';
  765.     strncpy(pUserNode->czAuthorFirstName, FirstName, LENGTH);
  766.  
  767.  
  768.     char LastName[LENGTH];
  769.     i = 0;
  770.     ret = true;
  771.     do
  772.     {
  773.         cout << endl << "Enter author's last name. After name press SPACE button : ";
  774.         i = 0;
  775.         while (1)
  776.         {
  777.             LastName[i] = getche();
  778.             if (LastName[0] == ' ')
  779.             {
  780.                 LastName[0] = '-';
  781.                 ++i;
  782.                 ret = false;
  783.                 break;
  784.             }
  785.             else if (LastName[i] == ' ')
  786.             {
  787.                 ret = false;
  788.                 break;
  789.             }
  790.             if (!isalpha(LastName[i]) || (isupper(LastName[i]) && i != 0) || islower(LastName[0]))
  791.                 break;
  792.             ++i;
  793.             if (i > LENGTH - 1)
  794.                 break;
  795.         }
  796.         if (ret)
  797.         {
  798.             cout << endl << "Something went wrong. Remember - only alphabet symbols (max - 20). Capital letter only at the beginning";
  799.             cout << endl << "Press space after name to end input or in the beginning if there is no author" << endl;
  800.         }
  801.     } while (ret);
  802.     LastName[i] = '\0';
  803.     strncpy(pUserNode->czAuthorLastName, LastName, LENGTH);
  804.  
  805.  
  806.     char BookName[BOOK_LENGTH];
  807.     i = 1;
  808.     ret = true;
  809.     do
  810.     {
  811.         cout << endl << "Enter book title. It must be in quotation marks ("") : ";
  812.         i = 0;
  813.         while (1)
  814.         {
  815.             BookName[i] = getche();
  816.  
  817.             if (BookName[0] != '"' || BookName[1] == '"' || BookName[i] == '\n')
  818.                 break;
  819.  
  820.             else if (BookName[i] == '"' && i != 0)
  821.             {
  822.                 ret = false;
  823.                 break;
  824.             }
  825.  
  826.             ++i;
  827.             if (i > BOOK_LENGTH - 2)
  828.                 break;
  829.         }
  830.         if (ret)
  831.             cout << endl << "Something went wrong. Remember - max 50 symbols." << endl;
  832.     } while (ret);
  833.     BookName[i + 1] = '\0';
  834.     strncpy(pUserNode->czBookTitle, BookName, BOOK_LENGTH);
  835.  
  836.     cout << endl << "Enter release date : ";
  837.     pUserNode->iReleaseDate = GetYear();
  838.  
  839.     cout << "Enter the price : ";
  840.     pUserNode->dPrice = GetPrice();
  841.  
  842.     cout << "Enter the number of pages: ";
  843.     pUserNode->iPages = GetPages();
  844.  
  845.     cout << "Now enter the position of this book in list: ";
  846.     int iPosition = GetPosition(*pHead);
  847.  
  848.     cout << "Do you really want to add this book to list? ";
  849.     if (YesOrNo())
  850.     {
  851.         sBook* ptr = (*pHead);
  852.         int iIterator = 1;
  853.  
  854.         if (iPosition == 1)
  855.         {                                                            //insert as first
  856.             pUserNode->next = (*pHead);
  857.             *pHead = pUserNode;
  858.         }
  859.         else if (iPosition == iNumberOfBooks + 1)
  860.         {                                                             //insert as last
  861.             while (ptr->next != NULL && iIterator != iNumberOfBooks)
  862.             {
  863.                 ptr = ptr->next;
  864.                 ++iIterator;
  865.             }
  866.  
  867.             ptr->next = pUserNode;
  868.             pUserNode->next = NULL;
  869.         }
  870.         else
  871.         {
  872.             while (iIterator != iPosition - 1)
  873.             {
  874.                 ptr = ptr->next;
  875.                 ++iIterator;
  876.             }
  877.             pUserNode->next = ptr->next;
  878.             ptr->next = pUserNode;
  879.         }
  880.     }
  881. }
  882.  
  883. //-----------------------------------------------------------------------------
  884.  
  885. sBook* CreateLinkedListFromFile(FILE* fp)
  886. {
  887.     sBook* pHead = NULL, * pTemp = NULL, * ptr = NULL;
  888.     int iCountBooks = 0;
  889.     char ch;    bool add = true;     int i = 0;     int cond = -1;       int size = 0;
  890.     int troubles[NUMBER_OF_BOOKS] = { 0 };
  891.  
  892.     char Name[LENGTH * 10], Surname[LENGTH * 10], BookName[BOOK_LENGTH * 10];
  893.     int year, pages;
  894.     double price;
  895.  
  896.     while (!feof(fp))
  897.     {
  898.  
  899.         cond++;
  900.  
  901.         add = true;
  902.         pTemp = (sBook*)malloc(sizeof(sBook));
  903.         if (pTemp == NULL)
  904.         {
  905.             printf("OS refused to give so much memory");
  906.             exit(2);
  907.         }
  908.  
  909.         if (fscanf(fp, "%s %s \t %[^\t] %d %d %lf", Name, Surname, BookName, &year, &pages, &price) != 6 || iCountBooks >= NUMBER_OF_BOOKS)
  910.             continue;
  911.  
  912.         i = 0;
  913.         size = strlen(Name);
  914.         Name[size] = '\0';
  915.         ch = Name[0];
  916.         while (ch != '\0')
  917.         {
  918.             if (i > LENGTH - 1 || !isupper(Name[0]))
  919.             {
  920.                 add = false;
  921.                 break;
  922.             }
  923.             if (!islower(Name[i]) && i != 0 && Name[i] != '\0')
  924.             {
  925.                 add = false;
  926.                 break;
  927.             }
  928.             ch = Name[i++];
  929.         }
  930.  
  931.         i = 0;
  932.         size = strlen(Surname);
  933.         Surname[size] = '\0';
  934.         ch = Surname[0];
  935.         while (ch != '\0')
  936.         {
  937.             if (i > LENGTH - 1 || !isupper(Surname[0]))
  938.             {
  939.                 add = false;
  940.                 break;
  941.             }
  942.             if (!islower(Surname[i]) && i != 0 && Surname[i] != '\0')
  943.             {
  944.                 add = false;
  945.                 break;
  946.             }
  947.             ch = Surname[i++];
  948.         }
  949.  
  950.         i = 0;
  951.         size = strlen(BookName);
  952.         BookName[size] = '\0';
  953.         ch = BookName[0];
  954.         while (ch != '\0')
  955.         {
  956.             if (i > BOOK_LENGTH - 1 || BookName[0] != '"' || BookName[strlen(BookName) - 1] != '"')
  957.             {
  958.                 add = false;
  959.                 break;
  960.             }
  961.             ch = BookName[i++];
  962.         }
  963.  
  964.         if (year < 1 || year > 2020)
  965.             add = false;
  966.         if (pages < 1 || pages > 9999)
  967.             add = false;
  968.         if (price < 0 || price > 9999.99)
  969.             add = false;
  970.  
  971.  
  972.         if (add)
  973.         {
  974.             strncpy(pTemp->czAuthorFirstName, Name, LENGTH);
  975.             strncpy(pTemp->czAuthorLastName, Surname, LENGTH);
  976.             strncpy(pTemp->czBookTitle, BookName, BOOK_LENGTH);
  977.             pTemp->dPrice = price;
  978.             pTemp->iPages = pages;
  979.             pTemp->iReleaseDate = year;
  980.             troubles[cond] = 1;
  981.  
  982.             pTemp->next = NULL;
  983.             ++iCountBooks;
  984.  
  985.             if (pHead == NULL)
  986.                 pHead = pTemp;
  987.             else
  988.             {
  989.                 ptr = pHead;
  990.                 while (ptr->next != NULL)
  991.                     ptr = ptr->next;
  992.                 ptr->next = pTemp;
  993.             }
  994.         }
  995.     }
  996.  
  997.     for (int i = 0; i < cond; ++i)
  998.         if (troubles[i] == 0)
  999.             cout << endl << "There was some troubles reading " << i + 1 << " element in file";
  1000.  
  1001.     if (iCountBooks == 0)
  1002.         cout << "\n\t\tList is empty";
  1003.     else
  1004.         cout << endl << "\t" << iCountBooks << " elements was added to list.";
  1005.  
  1006.     cout << endl << endl;
  1007.  
  1008.     return pHead;
  1009. }
  1010.  
  1011.  
  1012.  
  1013. #pragma once
  1014. //-----------------------------------------------------------------------------
  1015. #include <iostream>
  1016. #include <cstring>
  1017. #include <cstdlib>
  1018. #include <limits>
  1019. #include <conio.h>
  1020. #include <cctype>
  1021. //-----------------------------------------------------------------------------
  1022. #define _CRT_SECURE_NO_WARNINGS
  1023. #define _CRT_SECURE_NO_DEPRECATE
  1024. #define _CRT_NONSTDC_NO_DEPRECATE
  1025. #pragma warning(disable : 4996)
  1026. //-----------------------------------------------------------------------------
  1027. using namespace std;
  1028. //-----------------------------------------------------------------------------
  1029. const int LENGTH = 21;
  1030. const int TABLE_SIZE = 131;
  1031. const double PRICE = 100.00;
  1032. const int BOOK_LENGTH = 53;
  1033. const int NUMBER_OF_BOOKS = 20;
  1034. //-----------------------------------------------------------------------------
  1035. typedef struct sBook
  1036. {
  1037.     char czAuthorFirstName[LENGTH];
  1038.     char czAuthorLastName[LENGTH];
  1039.     char czBookTitle[BOOK_LENGTH];
  1040.     int iReleaseDate;
  1041.     int iPages;
  1042.     double dPrice;
  1043.     struct sBook* next;
  1044. }sBook;
  1045. //-----------------------------------------------------------------------------
  1046. int GetPosition(void);
  1047. int GetPosition(sBook* pHead);
  1048. int GetYear(void);
  1049. char GetChoiceForMenu(void);
  1050. double GetPrice(void);
  1051. int GetPages(void);
  1052. int YesOrNo(void);
  1053. void CalculatePriceOfAllBooks(sBook* pHead);
  1054. //-----------------------------------------------------------------------------
  1055. void ShowUserMenu(void);
  1056. void PrintLotOfCharacters(char symbol);
  1057. void PrintTable(sBook* pHead);
  1058. void PrintTableIntoFile(sBook* pHead, FILE* fp);
  1059. //-----------------------------------------------------------------------------
  1060. int CountBooksInList(sBook* pHead);
  1061. //-----------------------------------------------------------------------------
  1062. sBook* CreateLinkedListFromFile(FILE* fp);
  1063. //-----------------------------------------------------------------------------
  1064. void SwapTwoNodes(sBook** pHead, int first, int second);
  1065. //-----------------------------------------------------------------------------
  1066. void AddUserBookToList(sBook** pHead);
  1067. void DeleteBookFromList(sBook** pHead);
  1068. //-----------------------------------------------------------------------------
  1069. void SortListByAuthorsLastName(sBook** pHead);
  1070. //-----------------------------------------------------------------------------
  1071. void DeleteCheapBooks(sBook** pHead);
  1072. void PrintTwoThickestBooks(sBook* pHead);
  1073. //-----------------------------------------------------------------------------
  1074. void FreeMemory(sBook** pHead);
  1075. //-----------------------------------------------------------------------------
  1076. void SortByPrice(sBook** pHead);
  1077. //-----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement