Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "header.h"
- int main()
- {
- FILE* fpInput = NULL, * fpOutput = NULL;
- sBook* pHead = NULL;
- char cUserMenuChoice;
- if ((fpInput = fopen("D:\\Projects\\ListOfBooks(v1.1)\\ListOfBooks\\booklist.txt",
- "r")) == NULL)
- {
- printf("Can't open the input file!");
- exit(1);
- }
- if ((fpOutput = fopen("D:\\Projects\\ListOfBooks(v1.1)\\ListOfBooks\\booklistres.txt",
- "w")) == NULL)
- {
- printf("Can't create the result file!");
- exit(1);
- }
- pHead = CreateLinkedListFromFile(fpInput);
- int numberOfBooks = CountBooksInList(pHead);
- ShowUserMenu();
- while ((cUserMenuChoice = GetChoiceForMenu()) != 'i')
- {
- cout << endl;
- switch (cUserMenuChoice) {
- case 'a': PrintTable(pHead); break;
- case 'b': DeleteCheapBooks(&pHead); break;
- case 'c': PrintTwoThickestBooks(pHead); break;
- case 'd': AddUserBookToList(&pHead); break;
- case 'e': DeleteBookFromList(&pHead); break;
- case 'f': numberOfBooks = CountBooksInList(pHead);
- for (int i = 0; i < (numberOfBooks * numberOfBooks); ++i)
- SortListByAuthorsLastName(&pHead);
- cout << endl << "\tList was sorted by author's last name" << endl; break;
- case 'g': numberOfBooks = CountBooksInList(pHead);
- for (int i = 0; i < (numberOfBooks * numberOfBooks); ++i)
- SortByPrice(&pHead);
- cout << endl << "\tList was sorted by price" << endl; break;
- case 'h': CalculatePriceOfAllBooks(pHead); break;
- }
- cout << endl << "Choose another operation or press 'i' to exit : ";
- }
- PrintTableIntoFile(pHead, fpOutput);
- FreeMemory(&pHead);
- fclose(fpInput);
- fclose(fpOutput);
- cout << endl << endl << "\t\tExit..." << endl << endl;
- return 0;
- }
- #include "header.h"
- //-----------------------------------------------------------------------------
- void ShowUserMenu(void)
- {
- PrintLotOfCharacters('+');
- cout << "\t\t\t\t\t\tChoose an operation \n"
- "\t\t\t\t\t a) Print book list \n"
- "\t\t\t\t\t b) Delete cheap books(under 100$) \n"
- "\t\t\t\t\t c) Print two thickest books \n"
- "\t\t\t\t\t d) Add book to list \n"
- "\t\t\t\t\t e) Delete book from list \n"
- "\t\t\t\t\t f) Sort by author's last name \n"
- "\t\t\t\t\t g) Sort by price \n"
- "\t\t\t\t\t h) Calculate total price \n";
- PrintLotOfCharacters('+');
- printf("\t\t\t\t\tOperation: ");
- }
- //-----------------------------------------------------------------------------
- void CalculatePriceOfAllBooks(sBook* pHead)
- {
- double sum = 0;
- sBook* ptr = pHead;
- while (ptr != NULL)
- {
- sum += ptr->dPrice;
- ptr = ptr->next;
- }
- cout << endl << "\tTotal price = " << sum << endl;
- }
- //-----------------------------------------------------------------------------
- int YesOrNo(void)
- {
- int iReturnValue;
- cout << "Your choice (1 - YES | 0 - NO) : ";
- while (!(cin >> iReturnValue) || iReturnValue < 0 || iReturnValue > 1)
- {
- cout << "Re-enter the number (1 - YES | 0 - NO) : ";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- return iReturnValue;
- }
- //-----------------------------------------------------------------------------
- int GetPosition(sBook* pHead)
- {
- int iReturnValue;
- int iNumOfBooks = CountBooksInList(pHead);
- while (!(cin >> iReturnValue) || iReturnValue < 1 || iReturnValue > NUMBER_OF_BOOKS || iReturnValue > iNumOfBooks + 1)
- {
- cout << "Re-enter the number (min 1 - max " << iNumOfBooks + 1 << ") : ";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- return iReturnValue;
- }
- //-----------------------------------------------------------------------------
- int GetPosition(void)
- {
- int iReturnValue;
- while (!(cin >> iReturnValue) || iReturnValue < 1 || iReturnValue > NUMBER_OF_BOOKS)
- {
- cout << "Re-enter the number (min 1 - max 30) : ";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- return iReturnValue;
- }
- //-----------------------------------------------------------------------------
- int GetYear(void)
- {
- int iReturnValue;
- while (!(cin >> iReturnValue) || iReturnValue < 1 || iReturnValue > 2020)
- {
- cout << "Re-enter the number (min 1 - max 2020) : ";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- return iReturnValue;
- }
- //-----------------------------------------------------------------------------
- double GetPrice(void)
- {
- double dReturnValue;
- while (!(cin >> dReturnValue) || dReturnValue < 0.00 || dReturnValue > 9999.99)
- {
- cout << "Re-enter the number (min 0.00 - max 9999.99) : ";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- return dReturnValue;
- }
- //-----------------------------------------------------------------------------
- int GetPages(void)
- {
- int iReturnValue;
- while (!(cin >> iReturnValue) || iReturnValue < 1 || iReturnValue > 9999)
- {
- cout << "Re-enter the number (min 1 - max 9999) : ";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- return iReturnValue;
- }
- //-----------------------------------------------------------------------------
- char GetChoiceForMenu(void)
- {
- char cUserChoice = getche();
- while (cUserChoice > 'i' || cUserChoice < 'a')
- {
- cout << endl;
- printf("Please, re-enter the character (a-i) : ");
- cUserChoice = getche();
- }
- return cUserChoice;
- }
- //-----------------------------------------------------------------------------
- int CountBooksInList(sBook* pHead)
- {
- int iCount = 0;
- sBook* ptr = pHead;
- while (ptr != NULL)
- {
- ++iCount;
- ptr = ptr->next;
- }
- return iCount;
- }
- //-----------------------------------------------------------------------------
- void PrintLotOfCharacters(char cSymbol)
- {
- putchar('|');
- for (int i = 0; i < TABLE_SIZE; ++i)
- putchar(cSymbol);
- putchar('|');
- putchar('\n');
- }
- //-----------------------------------------------------------------------------
- void PrintTable(sBook* pHead) {
- sBook* ptr = pHead;
- if (ptr == NULL)
- {
- cout << endl << "\tNo books in list" << endl;
- return;
- }
- int iCount = 1;
- cout << endl;
- PrintLotOfCharacters('*');
- printf("%70s\n", "BOOK LIST");
- PrintLotOfCharacters('*');
- PrintLotOfCharacters('-');
- printf("| N | %16s | %17s | %35s %20s %s %s \n", "First name",
- "Last name", "Book title", "| Year |", " pp. |", " $ |");
- PrintLotOfCharacters('-');
- while (ptr != NULL)
- {
- printf("|%3d)|%21s | %21s | %53s | %4d | %4d "
- "| %7.2lf |\n", iCount, ptr->czAuthorFirstName,
- ptr->czAuthorLastName, ptr->czBookTitle,
- ptr->iReleaseDate, ptr->iPages, ptr->dPrice);
- PrintLotOfCharacters('-');
- ptr = ptr->next;
- ++iCount;
- }
- }
- //-----------------------------------------------------------------------------
- void FreeMemory(sBook** pHead)
- {
- sBook* pCurrent = *pHead;
- sBook* ptr;
- while (pCurrent != NULL) {
- ptr = pCurrent->next;
- free(pCurrent);
- pCurrent = ptr;
- }
- *pHead = NULL;
- }
- //-----------------------------------------------------------------------------
- void PrintTableIntoFile(sBook* pHead, FILE* fp)
- {
- sBook* ptr = pHead;
- if (pHead == NULL)
- fprintf(fp, "\t! No books in list !");
- else
- {
- int iCount = 1;
- fprintf(fp, "%70s\n", "BOOK LIST");
- fprintf(fp, "| N | %16s | %17s | %35s %20s %s %s \n", "First name",
- "Last name", "Book title", "| Year |", " pp. |", " $ |");
- while (ptr != NULL)
- {
- fprintf(fp, "|%3d)|%21s | %21s | %53s | %4d | %4d "
- "| %7.2lf |\n", iCount, ptr->czAuthorFirstName,
- ptr->czAuthorLastName, ptr->czBookTitle,
- ptr->iReleaseDate, ptr->iPages, ptr->dPrice);
- ptr = ptr->next;
- ++iCount;
- }
- }
- cout << endl << endl << "\tTable was printed into a file";
- }
- //-----------------------------------------------------------------------------
- void SortByPrice(sBook** pHead)
- {
- int number_of_books = CountBooksInList(*pHead);
- sBook* pSeek = NULL, * pTop = (*pHead);
- sBook temp;
- for (int i = 1; i < number_of_books; ++i)
- {
- if (pTop->next != NULL)
- pSeek = pTop->next;
- for (int j = i + 1; j <= number_of_books; ++j)
- {
- if (pTop->dPrice < pSeek->dPrice)
- SwapTwoNodes(pHead, i, j);
- else if (pTop->dPrice == pSeek->dPrice)
- {
- if (strncmp(pTop->czAuthorLastName, pSeek->czAuthorLastName, 20) > 0)
- SwapTwoNodes(pHead, i, j);
- else if (strncmp(pTop->czAuthorLastName, pSeek->czAuthorLastName, 20) == 0)
- {
- if (strncmp(pTop->czAuthorFirstName, pSeek->czAuthorFirstName, 20) > 0)
- SwapTwoNodes(pHead, i, j);
- else if (strncmp(pTop->czAuthorFirstName, pSeek->czAuthorFirstName, 20) == 0)
- {
- if (strncmp(pTop->czBookTitle, pSeek->czBookTitle, 50) > 0)
- SwapTwoNodes(pHead, i, j);
- }
- }
- }
- if (pSeek->next != NULL)
- pSeek = pSeek->next;
- }
- if (pTop->next != NULL)
- pTop = pTop->next;
- }
- }
- //-----------------------------------------------------------------------------
- void SortListByAuthorsLastName(sBook** pHead)
- {
- int number_of_books = CountBooksInList(*pHead);
- sBook* pSeek = NULL, * pTop = (*pHead);
- sBook temp;
- for (int i = 1; i < number_of_books; ++i)
- {
- if (pTop->next != NULL)
- pSeek = pTop->next;
- for (int j = i + 1; j <= number_of_books; ++j)
- {
- if (strncmp(pTop->czAuthorLastName, pSeek->czAuthorLastName, 20) > 0)
- SwapTwoNodes(pHead, i, j);
- else if (strncmp(pTop->czAuthorLastName, pSeek->czAuthorLastName, 20) == 0)
- {
- if (strncmp(pTop->czAuthorFirstName, pSeek->czAuthorFirstName, 20) > 0)
- SwapTwoNodes(pHead, i, j);
- else if (strncmp(pTop->czAuthorFirstName, pSeek->czAuthorFirstName, 20) == 0)
- {
- if (strncmp(pTop->czBookTitle, pSeek->czBookTitle, 50) > 0)
- SwapTwoNodes(pHead, i, j);
- }
- }
- if (pSeek->next != NULL)
- pSeek = pSeek->next;
- }
- if (pTop->next != NULL)
- pTop = pTop->next;
- }
- }
- //-----------------------------------------------------------------------------
- void PrintTwoThickestBooks(sBook* pHead)
- {
- int iNumberOfBooks = CountBooksInList(pHead);
- sBook* temp1, * temp2, * ptr = NULL;
- if (iNumberOfBooks <= 1)
- {
- cout << endl << "\tToo few books in list" << endl;
- return;
- }
- if (pHead->next->next != NULL)
- ptr = pHead->next->next;
- if (pHead->iPages >= pHead->next->iPages)
- {
- temp1 = pHead;
- temp2 = pHead->next;
- }
- else
- {
- temp1 = pHead->next;
- temp2 = pHead;
- }
- sBook* temp3 = NULL;
- while (ptr != NULL)
- {
- if (ptr->iPages > temp2->iPages)
- {
- if (ptr->iPages > temp1->iPages&& temp1->iPages > temp2->iPages)
- {
- temp2 = temp1;
- temp1 = ptr;
- }
- else if (ptr->iPages > temp1->iPages)
- temp1 = ptr;
- else
- temp2 = ptr;
- }
- ptr = ptr->next;
- }
- PrintLotOfCharacters('=');
- printf("\t\t\tTwo thickest books are:\n\t\t"
- "1) %s %s %s - %d\n\t\t2) %s %s %s - %d\n",
- temp1->czAuthorFirstName, temp1->czAuthorLastName,
- temp1->czBookTitle, temp1->iPages, temp2->czAuthorFirstName,
- temp2->czAuthorLastName, temp2->czBookTitle, temp2->iPages);
- PrintLotOfCharacters('=');
- }
- //-----------------------------------------------------------------------------
- void DeleteCheapBooks(sBook** pHead)
- {
- if (*pHead == NULL)
- {
- cout << endl << "\tNo books in list!" << endl;
- return;
- }
- sBook* ptr = *pHead;
- sBook* pTemp = *pHead;
- sBook* tmp = NULL;
- int iNumOfBooks = CountBooksInList(*pHead);
- int iCount = 0;
- int iIterator = 1;
- while (ptr != NULL)
- {
- if (ptr->dPrice < PRICE)
- {
- if (ptr == (*pHead))
- {
- do
- {
- if ((*pHead) != NULL)
- pTemp = (*pHead)->next;
- if (ptr != NULL)
- free(ptr);
- (*pHead) = pTemp;
- ++iCount;
- ptr = (*pHead);
- } while (ptr != NULL && ptr->dPrice < PRICE);
- }
- else if (ptr->next != NULL)
- {
- pTemp->next = ptr->next;
- free(ptr);
- ptr = pTemp;
- ++iCount;
- }
- else
- {
- pTemp->next = NULL;
- ++iCount;
- free(ptr);
- ptr = NULL;
- }
- }
- if (ptr != NULL)
- {
- pTemp = ptr;
- ptr = ptr->next;
- }
- }
- if (iCount == iNumOfBooks)
- cout << "\n\tAll books was deleted\n";
- else if (iCount == 0)
- cout << "\n\tNo books was deleted\n";
- else
- cout << "\n\t" << iCount << " books was deleted\n";
- }
- //-----------------------------------------------------------------------------
- void SwapTwoNodes(sBook** pHead, int iFirstNode, int iSecondNode)
- {
- sBook* ptr = *pHead, * pPreviousNode = NULL;
- sBook* pNodeX = NULL, * pPreviousNodeX = NULL;
- sBook* pNodeY = NULL, * pPreviousNodeY = NULL;
- sBook* pTemp;
- int iFirstNodeIterator = 1, iSecondNodeIterator = 1;
- int iNumberOfBooks = CountBooksInList(*pHead);
- if (iFirstNode > iSecondNode)
- {
- int temp = iFirstNode;
- iFirstNode = iSecondNode;
- iSecondNode = temp;
- }
- if (iFirstNode == 1)
- { //first node == pHead
- if (iSecondNode == 2)
- { //second node == pHead->next
- pNodeX = *pHead;
- pNodeY = pNodeX->next;
- pTemp = pNodeY;
- pNodeX->next = pNodeY->next;
- pNodeY->next = pNodeX;
- *pHead = pTemp;
- }
- else
- {
- pNodeX = ptr = *pHead;
- pPreviousNodeX = pPreviousNode = NULL;
- while (ptr != NULL && iSecondNodeIterator != iSecondNode)
- {
- pPreviousNode = ptr;
- ptr = ptr->next;
- ++iSecondNodeIterator;
- }
- pNodeY = ptr;
- pPreviousNodeY = pPreviousNode;
- pTemp = pNodeY->next;
- pNodeY->next = pNodeX->next;
- pNodeX->next = pTemp;
- *pHead = pNodeY;
- pPreviousNodeY->next = pNodeX;
- }
- }
- else if (iSecondNode == iNumberOfBooks)
- { //Second node == last node
- if (iFirstNode == iSecondNode - 1)
- { //Second node == FirstNode->next
- while (iFirstNodeIterator != iFirstNode)
- {
- pPreviousNode = ptr;
- ptr = ptr->next;
- ++iFirstNodeIterator;
- }
- pPreviousNodeX = pPreviousNode;
- pNodeX = ptr;
- pNodeY = ptr->next;
- pNodeY->next = NULL;
- pNodeY->next = pNodeX;
- pPreviousNodeX->next = pNodeY;
- pNodeX->next = NULL;
- }
- else
- {
- while (iFirstNodeIterator != iFirstNode)
- {
- pPreviousNode = ptr;
- ptr = ptr->next;
- ++iFirstNodeIterator;
- }
- pPreviousNodeX = pPreviousNode;
- pNodeX = ptr;
- while (ptr->next != NULL &&
- iSecondNodeIterator != iSecondNode)
- {
- pPreviousNode = ptr;
- ptr = ptr->next;
- ++iSecondNodeIterator;
- }
- pPreviousNodeY = pPreviousNode;
- pNodeY = ptr;
- pNodeY->next = pNodeX->next;
- pTemp = pNodeX;
- pPreviousNodeX->next = pNodeY;
- pPreviousNodeY->next = pTemp;
- pNodeX->next = NULL;
- }
- }
- else
- {
- if (iSecondNode == iFirstNode + 1)
- { //nodes are neighbours
- while (iFirstNodeIterator != iFirstNode)
- {
- pPreviousNode = ptr;
- ptr = ptr->next;
- ++iFirstNodeIterator;
- }
- pPreviousNodeX = pPreviousNode;
- pNodeX = ptr;
- pNodeY = ptr->next;
- pTemp = pNodeY->next;
- pNodeY->next = pNodeX;
- pPreviousNodeX->next = pNodeY;
- pNodeX->next = pTemp;
- }
- else
- {
- while (iFirstNodeIterator != iFirstNode)
- {
- pPreviousNode = ptr;
- ptr = ptr->next;
- ++iFirstNodeIterator;
- }
- pPreviousNodeX = pPreviousNode;
- pNodeX = ptr;
- iSecondNodeIterator = iFirstNodeIterator;
- while (iSecondNodeIterator != iSecondNode)
- {
- pPreviousNode = ptr;
- ptr = ptr->next;
- ++iSecondNodeIterator;
- }
- pPreviousNodeY = pPreviousNode;
- pNodeY = ptr;
- pTemp = pNodeY->next;
- pNodeY->next = pNodeX->next;
- pPreviousNodeY->next = pNodeX;
- pPreviousNodeX->next = pNodeY;
- pNodeX->next = pTemp;
- }
- }
- }
- //-----------------------------------------------------------------------------
- void DeleteBookFromList(sBook** pHead)
- {
- if (*pHead == NULL)
- {
- cout << endl << "\tNo books in list" << endl;
- return;
- }
- cout << endl << "\tEnter the position of book you want to delete : ";
- int iIterator = 1;
- int iBookToDelete = GetPosition();
- int iNumberOfBooks = CountBooksInList(*pHead);
- if (iBookToDelete > iNumberOfBooks)
- {
- printf("\n\tThere are only %d books\n", iNumberOfBooks);
- return;
- }
- sBook* ptr = *pHead, * pTemp = NULL;
- if (iBookToDelete == 1)
- {
- pTemp = *pHead;
- *pHead = (*pHead)->next;
- cout << endl << "\tFirst book in list was successfully deleted" << endl;
- free(pTemp);
- }
- else if (iBookToDelete == iNumberOfBooks)
- {
- while (ptr->next != NULL)
- {
- pTemp = ptr;
- ptr = ptr->next;
- }
- pTemp->next = NULL;
- cout << endl << "\tLast book in list was successfully deleted" << endl;
- free(ptr);
- }
- else
- {
- while (iIterator != iBookToDelete)
- {
- pTemp = ptr;
- ptr = ptr->next;
- ++iIterator;
- }
- pTemp->next = ptr->next;
- cout << endl << "\tBook #" << iBookToDelete << " was successfully deleted" << endl;
- free(ptr);
- }
- }
- //-----------------------------------------------------------------------------
- void AddUserBookToList(sBook** pHead)
- {
- int iNumberOfBooks = CountBooksInList(*pHead);
- if (iNumberOfBooks >= NUMBER_OF_BOOKS)
- {
- cout << endl << "\tThere is already max number of books" << endl;
- return;
- }
- fflush(stdin);
- sBook* pUserNode = (sBook*)malloc(sizeof(sBook));
- char FirstName[LENGTH];
- int i = 0;
- bool ret = true;
- do
- {
- cout << endl << "Enter author's first name. After name press SPACE button : ";
- i = 0;
- while (1)
- {
- FirstName[i] = getche();
- if (FirstName[0] == ' ')
- {
- FirstName[0] = '-';
- ++i;
- ret = false;
- break;
- }
- else if (FirstName[i] == ' ')
- {
- ret = false;
- break;
- }
- if (!isalpha(FirstName[i]) || (isupper(FirstName[i]) && i != 0) || islower(FirstName[0]))
- break;
- ++i;
- if (i > LENGTH - 1)
- break;
- }
- if (ret)
- {
- cout << endl << "Something went wrong. Remember - only alphabet symbols (max - 20). Capital letter only at the beginning";
- cout << endl << "Press space after name to end input or in the beginning if there is no author" << endl;
- }
- } while (ret);
- FirstName[i] = '\0';
- strncpy(pUserNode->czAuthorFirstName, FirstName, LENGTH);
- char LastName[LENGTH];
- i = 0;
- ret = true;
- do
- {
- cout << endl << "Enter author's last name. After name press SPACE button : ";
- i = 0;
- while (1)
- {
- LastName[i] = getche();
- if (LastName[0] == ' ')
- {
- LastName[0] = '-';
- ++i;
- ret = false;
- break;
- }
- else if (LastName[i] == ' ')
- {
- ret = false;
- break;
- }
- if (!isalpha(LastName[i]) || (isupper(LastName[i]) && i != 0) || islower(LastName[0]))
- break;
- ++i;
- if (i > LENGTH - 1)
- break;
- }
- if (ret)
- {
- cout << endl << "Something went wrong. Remember - only alphabet symbols (max - 20). Capital letter only at the beginning";
- cout << endl << "Press space after name to end input or in the beginning if there is no author" << endl;
- }
- } while (ret);
- LastName[i] = '\0';
- strncpy(pUserNode->czAuthorLastName, LastName, LENGTH);
- char BookName[BOOK_LENGTH];
- i = 1;
- ret = true;
- do
- {
- cout << endl << "Enter book title. It must be in quotation marks ("") : ";
- i = 0;
- while (1)
- {
- BookName[i] = getche();
- if (BookName[0] != '"' || BookName[1] == '"' || BookName[i] == '\n')
- break;
- else if (BookName[i] == '"' && i != 0)
- {
- ret = false;
- break;
- }
- ++i;
- if (i > BOOK_LENGTH - 2)
- break;
- }
- if (ret)
- cout << endl << "Something went wrong. Remember - max 50 symbols." << endl;
- } while (ret);
- BookName[i + 1] = '\0';
- strncpy(pUserNode->czBookTitle, BookName, BOOK_LENGTH);
- cout << endl << "Enter release date : ";
- pUserNode->iReleaseDate = GetYear();
- cout << "Enter the price : ";
- pUserNode->dPrice = GetPrice();
- cout << "Enter the number of pages: ";
- pUserNode->iPages = GetPages();
- cout << "Now enter the position of this book in list: ";
- int iPosition = GetPosition(*pHead);
- cout << "Do you really want to add this book to list? ";
- if (YesOrNo())
- {
- sBook* ptr = (*pHead);
- int iIterator = 1;
- if (iPosition == 1)
- { //insert as first
- pUserNode->next = (*pHead);
- *pHead = pUserNode;
- }
- else if (iPosition == iNumberOfBooks + 1)
- { //insert as last
- while (ptr->next != NULL && iIterator != iNumberOfBooks)
- {
- ptr = ptr->next;
- ++iIterator;
- }
- ptr->next = pUserNode;
- pUserNode->next = NULL;
- }
- else
- {
- while (iIterator != iPosition - 1)
- {
- ptr = ptr->next;
- ++iIterator;
- }
- pUserNode->next = ptr->next;
- ptr->next = pUserNode;
- }
- }
- }
- //-----------------------------------------------------------------------------
- sBook* CreateLinkedListFromFile(FILE* fp)
- {
- sBook* pHead = NULL, * pTemp = NULL, * ptr = NULL;
- int iCountBooks = 0;
- char ch; bool add = true; int i = 0; int cond = -1; int size = 0;
- int troubles[NUMBER_OF_BOOKS] = { 0 };
- char Name[LENGTH * 10], Surname[LENGTH * 10], BookName[BOOK_LENGTH * 10];
- int year, pages;
- double price;
- while (!feof(fp))
- {
- cond++;
- add = true;
- pTemp = (sBook*)malloc(sizeof(sBook));
- if (pTemp == NULL)
- {
- printf("OS refused to give so much memory");
- exit(2);
- }
- if (fscanf(fp, "%s %s \t %[^\t] %d %d %lf", Name, Surname, BookName, &year, &pages, &price) != 6 || iCountBooks >= NUMBER_OF_BOOKS)
- continue;
- i = 0;
- size = strlen(Name);
- Name[size] = '\0';
- ch = Name[0];
- while (ch != '\0')
- {
- if (i > LENGTH - 1 || !isupper(Name[0]))
- {
- add = false;
- break;
- }
- if (!islower(Name[i]) && i != 0 && Name[i] != '\0')
- {
- add = false;
- break;
- }
- ch = Name[i++];
- }
- i = 0;
- size = strlen(Surname);
- Surname[size] = '\0';
- ch = Surname[0];
- while (ch != '\0')
- {
- if (i > LENGTH - 1 || !isupper(Surname[0]))
- {
- add = false;
- break;
- }
- if (!islower(Surname[i]) && i != 0 && Surname[i] != '\0')
- {
- add = false;
- break;
- }
- ch = Surname[i++];
- }
- i = 0;
- size = strlen(BookName);
- BookName[size] = '\0';
- ch = BookName[0];
- while (ch != '\0')
- {
- if (i > BOOK_LENGTH - 1 || BookName[0] != '"' || BookName[strlen(BookName) - 1] != '"')
- {
- add = false;
- break;
- }
- ch = BookName[i++];
- }
- if (year < 1 || year > 2020)
- add = false;
- if (pages < 1 || pages > 9999)
- add = false;
- if (price < 0 || price > 9999.99)
- add = false;
- if (add)
- {
- strncpy(pTemp->czAuthorFirstName, Name, LENGTH);
- strncpy(pTemp->czAuthorLastName, Surname, LENGTH);
- strncpy(pTemp->czBookTitle, BookName, BOOK_LENGTH);
- pTemp->dPrice = price;
- pTemp->iPages = pages;
- pTemp->iReleaseDate = year;
- troubles[cond] = 1;
- pTemp->next = NULL;
- ++iCountBooks;
- if (pHead == NULL)
- pHead = pTemp;
- else
- {
- ptr = pHead;
- while (ptr->next != NULL)
- ptr = ptr->next;
- ptr->next = pTemp;
- }
- }
- }
- for (int i = 0; i < cond; ++i)
- if (troubles[i] == 0)
- cout << endl << "There was some troubles reading " << i + 1 << " element in file";
- if (iCountBooks == 0)
- cout << "\n\t\tList is empty";
- else
- cout << endl << "\t" << iCountBooks << " elements was added to list.";
- cout << endl << endl;
- return pHead;
- }
- #pragma once
- //-----------------------------------------------------------------------------
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <limits>
- #include <conio.h>
- #include <cctype>
- //-----------------------------------------------------------------------------
- #define _CRT_SECURE_NO_WARNINGS
- #define _CRT_SECURE_NO_DEPRECATE
- #define _CRT_NONSTDC_NO_DEPRECATE
- #pragma warning(disable : 4996)
- //-----------------------------------------------------------------------------
- using namespace std;
- //-----------------------------------------------------------------------------
- const int LENGTH = 21;
- const int TABLE_SIZE = 131;
- const double PRICE = 100.00;
- const int BOOK_LENGTH = 53;
- const int NUMBER_OF_BOOKS = 20;
- //-----------------------------------------------------------------------------
- typedef struct sBook
- {
- char czAuthorFirstName[LENGTH];
- char czAuthorLastName[LENGTH];
- char czBookTitle[BOOK_LENGTH];
- int iReleaseDate;
- int iPages;
- double dPrice;
- struct sBook* next;
- }sBook;
- //-----------------------------------------------------------------------------
- int GetPosition(void);
- int GetPosition(sBook* pHead);
- int GetYear(void);
- char GetChoiceForMenu(void);
- double GetPrice(void);
- int GetPages(void);
- int YesOrNo(void);
- void CalculatePriceOfAllBooks(sBook* pHead);
- //-----------------------------------------------------------------------------
- void ShowUserMenu(void);
- void PrintLotOfCharacters(char symbol);
- void PrintTable(sBook* pHead);
- void PrintTableIntoFile(sBook* pHead, FILE* fp);
- //-----------------------------------------------------------------------------
- int CountBooksInList(sBook* pHead);
- //-----------------------------------------------------------------------------
- sBook* CreateLinkedListFromFile(FILE* fp);
- //-----------------------------------------------------------------------------
- void SwapTwoNodes(sBook** pHead, int first, int second);
- //-----------------------------------------------------------------------------
- void AddUserBookToList(sBook** pHead);
- void DeleteBookFromList(sBook** pHead);
- //-----------------------------------------------------------------------------
- void SortListByAuthorsLastName(sBook** pHead);
- //-----------------------------------------------------------------------------
- void DeleteCheapBooks(sBook** pHead);
- void PrintTwoThickestBooks(sBook* pHead);
- //-----------------------------------------------------------------------------
- void FreeMemory(sBook** pHead);
- //-----------------------------------------------------------------------------
- void SortByPrice(sBook** pHead);
- //-----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement