Advertisement
COSCI539

CS136 Lab1 [9]

Feb 27th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.80 KB | None | 0 0
  1. //Lab 1 Wilson, Drayden T Th
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <algorithm>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. const int INV_ARR_MAX = 15;
  11.  
  12. struct productInfo
  13. {
  14.     string ID, name;
  15.     int quantity;
  16.     float price;
  17. };
  18.  
  19. typedef productInfo product;
  20.  
  21. int FillInventory(product *list[]);
  22. void DuplicateArray(product *list1[], product *list2[], int listSize);
  23. void PrintList(product *list[], int listSize);
  24. int CompareItems(const void *item1, const void *item2);
  25. void TestSearchItem(product *list[], string testItem, string searchItem, int index);
  26. void SearchList(product *list[], int listSize, string searchItem);
  27. void PrintListInfo(product *list[], int listSize);
  28. void DeallocateMemory(product *list[], int listSize);
  29. void RuntimeError(const string errorMessage);
  30.  
  31. int main()
  32. {
  33.     product *pInventoryUnsorted[INV_ARR_MAX] = { nullptr },
  34.         *pInventorySorted[INV_ARR_MAX] = { nullptr };
  35.     int productCount, userMenuSelection = 0;
  36.  
  37.     productCount = FillInventory(pInventoryUnsorted);
  38.  
  39.     cout << pInventoryUnsorted[0]->ID << ' ' << pInventoryUnsorted[0]->name << ' '
  40.         << pInventoryUnsorted[0]->quantity << ' ' << pInventoryUnsorted[0]->price << endl;
  41.     //////////////////// why does this work in FillInventory() but not here in main?
  42.  
  43.     DuplicateArray(pInventoryUnsorted, pInventorySorted, productCount);
  44.  
  45.     do
  46.     {
  47.         cout << "\n\n[1] Display the inventory unsorted.\n"
  48.             << "[2] Display the inventory in ascending order by a field.\n"
  49.             << "[3] Search for an item by ID or name.\n"
  50.             << "[4] Display the unique item count, total worth,"
  51.             << "and total item count of the inventory.\n"
  52.             << "[5] Exit.";
  53.  
  54.         if (userMenuSelection)
  55.         {
  56.             cout << "\n\nSelect another option.: ";
  57.         }
  58.         else
  59.         {
  60.             cout << "\n\nSelect an option by entering its corresponding number: ";
  61.         }
  62.  
  63.         cin >> userMenuSelection;
  64.  
  65.         switch (userMenuSelection)
  66.         {
  67.         case 1:
  68.             PrintList(pInventoryUnsorted, productCount);
  69.             break;
  70.         case 2:
  71.             qsort(pInventorySorted, productCount, sizeof(product*), CompareItems);// use bubble sort~~~~~~~~~~~~~~~
  72.             PrintList(pInventorySorted, productCount);
  73.             break;
  74.         case 3:
  75.             PrintSearchResult(pInventoryUnsorted, productCount);
  76.             break;
  77.         case 4:
  78.             PrintListInfo(pInventoryUnsorted, productCount);
  79.             break;
  80.         case 5:
  81.             cout << "Exiting program.\n";
  82.             break;
  83.         default:
  84.             cout << "Invalid menu option selected.\n";
  85.  
  86.             userMenuSelection = 1; //avoids repeating initial instructions
  87.             break;
  88.         }
  89.     } while (userMenuSelection != 5);
  90.  
  91.     DeallocateMemory(pInventoryUnsorted, productCount);
  92.  
  93.     system("pause");
  94.     return 0;
  95. }
  96.  
  97. void PrintSearchResult(product *pInventoryUnsorted[], int productCount)
  98. {
  99.     string searchItem;
  100.  
  101.     cout << "Input an ID or name to search by: ";
  102.     cin >> searchItem; //assuming input is all numbers or all letters
  103.  
  104.     transform(searchItem.begin(), searchItem.end(), searchItem.begin(), ::toupper);
  105.  
  106.     SearchList(pInventoryUnsorted, productCount, searchItem);
  107. }
  108.  
  109. //Populates an array of pointers to structs with data from file
  110. //pre: list and inFile exist
  111. //post: inFile is closed, list contains items from inFile, number of items is returned
  112. int FillInventory(product *list[])
  113. {
  114.     ifstream inFile;
  115.     product temp;
  116.     int i; //counter
  117.  
  118.     inFile.open("input.txt");
  119.  
  120.     if (!inFile)
  121.     {
  122.         RuntimeError("Failed to open input file.");
  123.     }
  124.     else
  125.     {
  126.         cout << "Input file read successfully.";
  127.     }
  128.  
  129.  
  130.     for (i = 0; inFile >> temp.ID && i < INV_ARR_MAX; ++i)
  131.     {
  132.         inFile >> temp.name >> temp.quantity >> temp.price;
  133.  
  134.         transform((temp.name).begin(), (temp.name).end(), (temp.name).begin(), ::toupper);
  135.  
  136.         if (temp.quantity < 0 || temp.price < 0.01)
  137.         {
  138.             printf("Invalid entry detected. Discarding \"%s\" entry.\n", temp.name);
  139.         }
  140.         else
  141.         {
  142.             try
  143.             {
  144.                 list[i] = (product *)malloc(sizeof(product));
  145.             }
  146.             catch (bad_alloc exception)
  147.             {
  148.                 RuntimeError("Insufficient memory available.");
  149.             }
  150.  
  151.             list[i] = &temp;
  152.  
  153.             cout << list[i]->ID << ' ' << list[i]->name << ' ' << list[i]->quantity << ' ' << list[i]->price << endl;//////////////
  154.         }
  155.     }
  156.  
  157.     inFile.close();
  158.  
  159.     if (i == 0)
  160.     {
  161.         RuntimeError("Input file is empty.");
  162.     }
  163.     else if (!inFile.eof())
  164.     {
  165.         RuntimeError("Excess entries detected.");
  166.     }
  167.  
  168.     return i;
  169. }
  170.  
  171. //Assigns one array of pointers to structs to another
  172. //pre: list1 is populated and listSize is defined
  173. //post: list2 is identical to list1
  174. void DuplicateArray(product *list1[], product *list2[], int listSize)
  175. {
  176.     for (int i = 0; i < listSize; ++i)
  177.     {
  178.         list2[i] = list1[i];
  179.     }
  180. }
  181.  
  182. //Prints all members held within an array
  183. //pre: list is populated and listSize is defined
  184. //post: all members are printed
  185. void PrintList(product *list[], int listSize)
  186. {
  187.     for (int i = 0; i < listSize; ++i)
  188.     {
  189.         cout << list[i]->ID << ' '
  190.             << list[i]->name << ' '
  191.             << list[i]->quantity << ' '
  192.             << list[i]->price << endl;
  193.     }
  194. }
  195.  
  196. //Compares two members from different structs from an array of pointers to structs
  197. //pre: the source array and all structs within are defined
  198. //post: the difference between the two struct members is returned
  199. int CompareItems(const void *item1, const void *item2)
  200. {
  201.     product *pItem1 = *(product**)item1;
  202.     product *pItem2 = *(product**)item2;
  203.     int sortChoice;
  204.  
  205.     cout << "Select an option to sort the list by "    //sortChoice prompt and input must be here
  206.         << "[1]ID, [2]name, [3]quantity, [4]price: ";  //because of nature of qsort
  207.  
  208.     do
  209.     {
  210.         cin >> sortChoice;
  211.  
  212.         switch (sortChoice)
  213.         {
  214.         case 1:
  215.             return pItem1->ID.compare(pItem2->ID);
  216.         case 2:
  217.             return pItem1->name.compare(pItem2->name);
  218.         case 3:
  219.             return (pItem1->quantity - pItem2->quantity) * 100;
  220.         case 4:
  221.             return pItem1->price - pItem2->price;
  222.         default:
  223.             cout << "Invalid option selected. Try Again: ";
  224.             break;
  225.         }
  226.     } while (sortChoice < 1 || sortChoice > 4);
  227. }
  228.  
  229. //Tests if a given item is equivalent to a given search term
  230. //pre: all variables are defined
  231. //post: item info is printed if found, fail message printed otherwise
  232. void TestSearchItem(product *list[], string testItem, string searchItem, int index)
  233. {
  234.     if (testItem == searchItem)
  235.     {
  236.         cout << list[index]->ID << ' '
  237.             << list[index]->name << ' '
  238.             << list[index]->quantity << ' '
  239.             << list[index]->price << endl;
  240.     }
  241.     else
  242.     {
  243.         cout << "Item not found.\n";
  244.     }
  245. }
  246.  
  247. //Seaches a list for a given item by ID or name then prints that item's info is found
  248. //pre: list is populated, listSize is defined and searchItem is defined
  249. //post: the sought after item's info is printed if found
  250. void SearchList(product *list[], int listSize, string searchItem)
  251. {
  252.     for (int i = 0; i < listSize; ++i)
  253.     {
  254.         if (isdigit(searchItem[0])) //search item is an ID
  255.         {
  256.             TestSearchItem(list, list[i]->ID, searchItem, i);
  257.         }
  258.         else //search item is a name
  259.         {
  260.             TestSearchItem(list, list[i]->name, searchItem, i);
  261.         }
  262.  
  263.     }
  264. }
  265.  
  266. int binarySearch(int arr[], int l, int r, int x)////////////////////
  267. {
  268.     if (r >= l) {
  269.         int mid = l + (r - l) / 2;
  270.  
  271.         // If the element is present at the middle
  272.         // itself
  273.         if (arr[mid] == x)
  274.             return mid;
  275.  
  276.         // If element is smaller than mid, then
  277.         // it can only be present in left subarray
  278.         if (arr[mid] > x)
  279.             return binarySearch(arr, l, mid - 1, x);
  280.  
  281.         // Else the element can only be present
  282.         // in right subarray
  283.         return binarySearch(arr, mid + 1, r, x);
  284.     }
  285.  
  286.     // We reach here when element is not
  287.     // present in array
  288.     return -1;
  289. }
  290.  
  291. //Calculates and prints the number of unique elements, total al value and total size of an inventory
  292. //pre: list is populated and listSize is defined
  293. //post: info has been printed
  294. void PrintListInfo(product *list[], int listSize)
  295. {
  296.     static float inventoryValue = 0;
  297.     static int inventorySize = 0;
  298.     bool infoCalculated = 0;
  299.  
  300.     for (int i = 0; i < listSize && !infoCalculated; ++i)
  301.     {
  302.         inventoryValue += list[i]->price;
  303.         inventorySize += list[i]->quantity;
  304.     }
  305.  
  306.     infoCalculated = 1;
  307.  
  308.     cout << "Unique Items: " << listSize
  309.         << "\nInventory Value: " << inventoryValue
  310.         << "\nInventory Size: " << inventorySize << endl;
  311. }
  312.  
  313. //Frees memory of an array's elements
  314. //pre: list is populated and listSize is defined
  315. //post: list's elements no longer have memory allocated to them
  316. void DeallocateMemory(product *list[], int listSize)
  317. {
  318.     for (int i = 0; i < listSize; ++i)
  319.     {
  320.         free((void *)list[i]);
  321.     }
  322. }
  323.  
  324. //Outputs a unique error message and ends the program
  325. //pre: N/A
  326. //post: error message has been output and program has ended
  327. void RuntimeError(const string errorMessage)
  328. {
  329.     cout << "Error: " << errorMessage << " Exiting program.\n\n";
  330.     system("pause");
  331.     exit(1);
  332. }
  333.  
  334. //issue with product count after fill
  335. //switch to bubble sort
  336. //program crashes when trying to exit from menu
  337. //store price as float?
  338.  
  339. /*
  340. 997196478 StroLLer 25 134.78
  341. 168484964 umBRElLA 39 14.74
  342. 168746844 Coat 17 36.54
  343. 498749896 baSkEt 29 7.23
  344. 836749194 tOwEl 52 5.89
  345. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement