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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
ofstream fout("NewPartsList.txt");
struct programParts
{
string partNumber;
char partClass;
int partOHB;
double partCost;
}P2;
// Function Prototypes
// Fills Vectors
bool getData(vector <programParts> & V);
// Does a binary search
int binSearch(const vector <programParts> &V, string key, int min, int max);
// Asks user for a part number to search for
string getTarget(vector <programParts> &V);
// Gets remaining info to add a part number
void getMoreData(vector <programParts> &V);
// Inserts part number data into vectors into the proper location
// See book for information on how to write this
void insertData(vector <programParts>& V, programParts toAdd);
// Displays info on part number
void display(const vector <programParts> &V);
// sorts vectors (Calls swapper)
void sort(vector <programParts> & V);
// Prints search stats
void printStats(int searches, int good, int bad);
// Writes out file when program ends, so additions are saved to file
void putData(const vector <programParts> & V);
// Templated swap function – Swaps two items in a vector of any type
template <class CType>
void swapper(CType& a, CType & b)
{
CType temp;
temp = a;
a = b;
b = temp;
}
int main()
{
vector <programParts> V;
char choice;
char choiceAgain;
bool read;
read = getData(V);
int good = 0;
int bad = 0;
int searches = 0;
if (read == true)
{
display(V);
cout << "Would you like to do another search? (Y or N) " << endl;
cin >> choice;
if (choice == 'Y')
{
getMoreData(V);
cout << "Would you like to do another search?" << endl;
cin >> choiceAgain;
if (choiceAgain == 'Y')
{
printStats(searches, good, bad);
}
}
else
cout << "Goodbye!" << endl;
}
else
cout << endl;
}
bool getData(vector <programParts> & V)
{
bool no = false;
bool ok = true;
programParts programIn;
ifstream inputFile;
inputFile.open("parts.txt");
if (inputFile.fail())
{
return no;
}
else
{
while (inputFile >> programIn.partNumber >> programIn.partClass >> programIn.partOHB >> programIn.partCost)
{
V.push_back(programIn);
}
inputFile.close();
}
return ok;
}
int binSearch(const vector <programParts> &V, string key, int min, int max)
{
bool found = false;
int middle, position = -1;
while (!found && min <= max)
{
middle = (min + max) / 2;
if (V[middle].partNumber == key)
{
found = true;
position = middle;
}
else if (V[middle].partNumber > key)
{
max = middle - 1;
}
else
{
min = middle + 1;
}
}
return position;
}
string getTarget(vector <programParts> &V)
{
string partNumber;
cout << "What part number would you like to look for?" << endl;
cin >> partNumber;
return partNumber;
}
void insertData(vector <programParts>& V, programParts toAdd)
{
int max = V.size();
int min = 0;
int middle, position = 1;
bool found = false;
while (!found && min <= max)
{
middle = (min + max) / 2;
if (V[middle].partNumber == P2.partNumber)
{
found = true;
position = middle;
}
else if (V[middle].partNumber > P2.partNumber)
{
max = middle - 1;
}
else
{
min = middle + 1;
}
if (position == -1)
{
if (V[middle].partNumber > P2.partNumber)
{
position = middle;
}
else
{
position = middle + 1;
}
}
else
{
position = middle;
}
V.insert(V.begin() + position, P2);
}
}
void sort(vector <programParts> & V)
{
bool flag = true;
int i, letter = V.size();
int x = letter;
while (flag || (x > 1))
{
flag = false;
x = (x + 1) / 2;
for (int i = 0; i < (letter - x); i++)
{
if (V[i + x].partNumber < V[i].partNumber)
{
swapper(V[i], V[i + x]);
flag = true;
}
}
}
}
void putData(const vector <programParts> & V)
{
for (int i = 0; i < V.size(); i++)
{
fout << V[i].partNumber << " " << V[i].partClass << " " << V[i].partOHB << " " << V[i].partCost;
}
fout.close();
}
void getMoreData(vector <programParts> &V)
{
string partNumber;
char choice;
char anotherChoice;
char choiceAgain;
char choiceClass;
int choiceOHB;
double choiceCost;
vector <programParts> A;
int good = 0;
int bad = 0;
int searches;
cout << "What part number would you like to look for?" << endl;
cin >> partNumber;
cout << "Add this part?" << endl;
cin >> choice;
if (choice == 'Y')
{
cout << "What class is this part number in?" << endl;
cin >> choiceClass;
cout << "What is the on hand balance of this part?" << endl;
cin >> choiceOHB;
cout << "What is the cost of this part?" << endl;
cin >> choiceCost;
fout << partNumber << " " << choiceClass << " " << choiceOHB << " " << choiceCost << endl;
cout << "Would you like to do another search (Y or N)" << endl;
cin >> anotherChoice;
if (anotherChoice == 'Y')
{
cout << "What part number would you like to look for?" << endl;
cin >> partNumber;
cout << "There are" << " " << choiceOHB << " " << "of part number" << " " << partNumber << " " << "in the inventory." << " " << "It is a class" << " " << choiceClass << " " << "part." << endl;
cout << "The cost is" << " " << "$" << choiceCost << "." << "The value of that inventory is" << endl;
}
else
cout << endl;
}
}
void display(const vector <programParts> &V)
{
int max = V.size();
int min = 0;
vector <programParts> A;
string key = getTarget(A);
int i = binSearch(V, key, min, max);
int specificOHB;
char specificClass;
string specificNumber;
double specificCost;
double specificCostInventory = 0;
specificClass = V[i].partClass;
specificCost = V[i].partCost;
specificOHB = V[i].partOHB;
specificNumber = V[i].partNumber;
for (int i = 0; i < V.size(); i++)
{
specificCostInventory = V[i].partClass + V[i].partCost + specificCostInventory;
}
cout << "There are" << " " << specificOHB << " " << "of part number" << " " << key << " " << "in the inventory." << " " << "It is a class" << " " << specificClass << " " << "part." << endl;
cout << "The cost is" << " " << "$" << specificCost << "." << "The value of that inventory is" << " " << "$" << specificCostInventory << endl;
}
void printStats(int searches, int good, int bad)
{
cout << "There were 3 searches performed today." << endl;
cout << "however many were succesfull" << " " << "of them were succesfull" << endl;
cout << "however werent in the system" << " " << "of them were not in the system" << endl;
cout << "The file is now updated" << endl;
}
|