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
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
// Sorts vectors (Calls swapper)
void sort(vector<string>& part_number, vector<char>& part_class, vector<int>&ohb, vector<double>& cost);
// Fills vectors
bool get_data(vector <string>& part_number, vector <char>& part_class, vector<int>& part_ohb, vector <double>& part_cost);
// Does a binary search
int bin_search(string key, const vector<string>& part_number);
// Asks user for a part number to search for
string get_target();
// Gets remaining info to add a part number
void get_more_data(char& class_in, int& part_ohb_in, double& part_cost_in);
// Inserts part number data into vectors into the proper location (code for this given below)
void insert_data(vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost, string part_in,
char class_in, int part_ohb_in, double part_cost_in);
// Displays info on part number
void display(const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost, int finder);
// Prints search stats
void print_stats(int searches, int good, int bad);
// Writes out file
void put_data(const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost);
// Templated swap function. Swaps two items in a vector of any type
template <class CType> void swapper(CType& a, CType & b);
template<class CType>
void swapper(CType& a, CType& b)
{
CType temp;
temp = a;
a = b;
b = temp;
}
int main() {
vector <string> part_number;
vector <char> part_class;
vector <int> part_ohb;
vector <double> part_cost;
string part_in;
int part_ohb_in, searches = 0, good = 0, bad = 0, finder;
char class_in, response;
double part_cost_in;
do {
part_in = get_target();
searches++;
get_data(part_number, part_class, part_ohb, part_cost);
sort(part_number, part_class, part_ohb, part_cost);
finder = bin_search(part_in, part_number);
if (finder == -1) {
cout << part_in << "not found!\n";
get_more_data(class_in, part_ohb_in, part_cost_in);
cout << "Adding part into library...\n";
bad++;
insert_data(part_number, part_class, part_ohb, part_cost, part_in, class_in, part_ohb_in, part_cost_in);
}
else {
display(part_number, part_class, part_ohb, part_cost, finder);
good++;
}
cout << "Would you like to search again?" << endl;
cin >> response;
} while (response == 'y' || response == 'Y');
print_stats(searches, good, bad);
put_data(part_number, part_class, part_ohb, part_cost);
return 0;
}
void sort(vector<string>& part_number, vector<char>& part_class, vector<int>& part_ohb, vector<double>& part_cost) {
unsigned int i, j, increment;
string temp_pn;
char temp_pc;
int temp_ohb;
double temp_cost;
increment = 3;
while (increment > 0) {
for (i = 0; i < part_number.size(); i++) {
j = i;
swapper(temp_pn, part_number[i]);
swapper(temp_pc, part_class[i]);
swapper(temp_ohb, part_ohb[i]);
swapper(temp_cost, part_cost[i]);
while ((j >= increment) && (part_number[j - increment] > temp_pn)) {
swapper(part_number[j], part_number[j - increment]);
swapper(part_class[j], part_class[j - increment]);
swapper(part_ohb[j], part_ohb[j - increment]);
swapper(part_cost[j], part_cost[j - increment]);
j = j - increment;
}
swapper(part_number[j], temp_pn);
swapper(part_class[j], temp_pc);
swapper(part_ohb[j], temp_ohb);
swapper(part_cost[j], temp_cost);
}
if (increment / 2 != 0) {
increment = increment / 2;
}
else if (increment == 1) {
increment = 0;
}
else {
increment = 1;
}
}
}
bool get_data(vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost) {
string partIn;
int ohbIn;
char classIn;
double costIn;
bool status;
ifstream file;
//reading the file here
file.open("parts.txt");
while (file >> partIn >> classIn >> ohbIn >> costIn)
{
part_number.push_back(partIn);
part_class.push_back(classIn);
part_ohb.push_back(ohbIn);
part_cost.push_back(costIn);
}
status = true;
file.close();
return status;
}
int bin_search(string key, const vector<string>& part_number) {
bool found = false;
int first, mid, last, return_val;
first = 0;
last = part_number.size() - 1;
while ((first <= last) && (!found)) {
mid = (first + last) / 2;
if (key == part_number[mid]) {
found = true;
}
else {
if (key < part_number[mid]) {
last = mid - 1;
}
else {
first = mid + 1;
}
}
}
if (found) {
return_val = mid;
}
else {
return_val = -1;
}
return return_val;
}
string get_target() {
string part_in;
cout << "What part number should I search for?" << endl;
cin >> part_in;
return part_in;
}
void get_more_data(char& class_in, int& part_ohb_in, double& part_cost_in) {
cout << "Please add in the part class, how many of them there are, and the cost per part: ";
cin >> class_in >> part_ohb_in >> part_cost_in;
}
void insert_data(vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost,
string part_in, char class_in, int part_ohb_in, double part_cost_in) {
part_number.push_back(part_in);
part_class.push_back(class_in);
part_ohb.push_back(part_ohb_in);
part_cost.push_back(part_cost_in);
sort(part_number, part_class, part_ohb, part_cost);
}
void display(const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost, int finder) {
double total = part_ohb[finder] * part_cost[finder];
cout << "There are " << part_ohb[finder] << " of Part Number " << part_number[finder] << " in inventory. It is a class " << part_class[finder]
<< " part. The cost is " << part_cost[finder] << ". The value of that inventory is " << total << ".\n";
}
void print_stats(int searches, int good, int bad) {
cout << "You made " << searches << " searches with " << good << " successful searches and " << bad << " bad searches.\n";
}
void put_data(const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost) {
// for loop for displaying
ofstream myfile;
myfile.open("parts2.txt");
for (unsigned int i = 0; i < part_number.size(); i++) {
myfile << part_number[i] << " " << part_class[i] << " " << part_ohb[i] << " " << part_cost[i] << endl;
}
myfile.close();
}
|