1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
#include "stdafx.h" // Visual studio
#include <iostream> // NULL, cin, cout etc.
#include <time.h> // time
#include <stdlib.h> // srand, rand
#include <array> // arrays
using namespace std;
// Package_Node struct to create a list of packages
struct Package_Node
{
int bar_code;
float package_weight;
struct Package_Node *next_package;
};
// Key_Node struct to store key information for dynamic array of keys
struct Key_Node
{
int key = 0;
struct Package_Node *next_package;
};
//Defines Nodeptr to Package_Node for use to make the linked list
typedef Package_Node* Nodeptr;
//All my functions so far, haven't created the comments to describe each of these but I don't think that'd be an issue
// for the feedback
void menu(int N, int Max_Value, int X, Key_Node *Key_Array, Nodeptr package_info);
void NumOfKeys(int &X, int &Max_Value);
void NumOfPackages(int &N);
void ArrayOfKeys(Key_Node *Key_Array, int X);
void OrganizePackages(Nodeptr package_info, int Max_Value, int X, int N, Key_Node *Key_Array);
void InsertList(Nodeptr& head, Nodeptr& tail, Nodeptr package_info);
int main()
{
//Initializing variables
// Total number of packages to create
int N = 0;
// Max value for bar codes generated
int Max_Value = 0;
//Number of keys for array
int X = 0;
//arrayPos to keep track of array index for keys
Key_Node *Key_Array = nullptr;
// code and weight to assign values to bar_code and package_weight for Package_Node
Nodeptr package_info = nullptr;
menu(N, Max_Value, X, Key_Array, package_info);
return 0;
}
void menu(int N, int Max_Value, int X, Key_Node *Key_Array, Nodeptr package_info)
{
// Menu loop to continue loop unless user enters 7
bool menuLoop = true;
do
{
// base menu
cout << "What would you like to do?\n";
cout << "1) Enter number of keys and the Max Value\n";
cout << "2) Enter number of packages (N)\n";
cout << "3) Create Array of Keys\n";
cout << "4) Organize Packages (organizes the N packages)\n";
cout << "5) Table stats\n";
cout << "6) Clean table\n";
cout << "7) Exit\n";
// User input for choice from menu for use in switch statement
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
NumOfKeys(X, Max_Value);
break;
}
case 2:
{
NumOfPackages(N);
break;
}
case 3:
{
ArrayOfKeys(Key_Array, X);
break;
}
case 4:
{
// Checks if no keys or number of packages have been entered yet and goes back to menu if not
if (X == 0 || N == 0)
{
cout << "No keys and/or number of packages were entered.\n";
break;
}
else
{
OrganizePackages(package_info, Max_Value, X, N, Key_Array);
}
break;
}
case 5:
{
// Extra menu for table choices with another switch statement for user choice
int tableChoice;
cout << "What would you like to do with the table?\n";
cout << "0) Find packages given a bar code\n";
cout << "1) Total number of objects given a key\n";
cout << "2) Total weight given a key\n";
cout << "3) Print List given a key\n";
cout << "4) Print Entire Table\n";
cout << "5) Find all package that are greater than a given weight\n";
cin >> tableChoice;
switch (tableChoice)
{
case 0:
{
TableFindPackage();
break;
}
case 1:
{
TableTotalNumOfObjects();
break;
}
case 2:
{
TableTotalWeight();
break;
}
case 3:
{
TablePrintList();
break;
}
case 4:
{
TablePrintTable();
break;
}
case 5:
{
TableFindGreaterWeight();
break;
}
}
}
case 6:
{
CleanTable();
break;
}
case 7:
{
// Stops loop because user chose to exit
menuLoop = false;
break;
}
}
} while (menuLoop == true);
}
void NumOfKeys(int &X, int &Max_Value)
{
// Asks user for number of keys
cout << "Please enter the number of keys that will be used: ";
cin >> X;
// Asks user for Max Value of barcode
cout << "\nPlease Enter the Max Value of the barcode: ";
cin >> Max_Value;
cout << "\n";
}
void NumOfPackages(int &N)
{
// Asks user for number of packages
cout << "Please enter the number of packages (N): ";
cin >> N;
// Goes back to menu after user enters information
}
void ArrayOfKeys(Key_Node *Key_Array, int X)
{
Key_Array = new Key_Node[X];
// Displays the values in the array created and initializes them to 0, 1 , 2 etc. until user entered key is reached
cout << "Your array now has the values: \n";
for (int i = 0; i < X; i++)
{
Key_Array->key = i;
Key_Array->next_package = NULL;
cout << Key_Array->key << " \n";
}
}
//** Part where issue starts
void OrganizePackages(Nodeptr package_info, int Max_Value, int X, int N, Key_Node *Key_Array)
{
// Min value for bar code
int Min_Value = 1;
// float values for weight
//Initializes head and tail to NULL (Not sure if tail is necessary yet)
Nodeptr head = NULL, tail = NULL;
//Initialize random seed
srand(static_cast<unsigned int>(time(NULL)));
if (package_info->next_package == NULL)
{
package_info = new Package_Node;
// Generate random number between Min_Value and Max_Value for barcode
package_info->bar_code = rand() % Max_Value + Min_Value;
// Generate random float weight for package of 1 - 500
package_info->package_weight = static_cast <float> (rand() % 500 + 1);
// Places package with information into key
Key_Array->key = (package_info->bar_code % X);
// Inserts information into list
InsertList(head, tail, package_info);
}
}
//**This is wrong and causes a crash
void InsertList(Nodeptr& head, Nodeptr& tail, Nodeptr package_info)
{
Nodeptr NewPtr = NULL, tempPtr = NULL;
while (NewPtr = package_info)
{
//Add object to the list
if (head == NULL)
{
//beginning of the list
head = NewPtr;
// Current record
tail = NewPtr;
}
else
{
//Previous record reference to new record
{
tail->next_package = NewPtr;
// Current Record
tail = NewPtr;
}
}
// End of the list
tail->next_package = NULL;
for (tempPtr = head; tempPtr != NULL; tempPtr = tempPtr->next_package)
{
cout << "\n" << (tempPtr->bar_code) << "-" << (tempPtr->package_weight) << "\n";
}
}
}
|