The VP of Manufacturing, Silas Grump, calls you down to his large office with a
ID: 3906949 • Letter: T
Question
The VP of Manufacturing, Silas Grump, calls you down to his large office with a desk on a slightly raised platform. He looks down at you and says, "I heard you can program a little, kid. They sure do pay you computer guru types a lotta bucks!" Then he says, "But, what can you do for me? I need a program that when I type in a part number, it will come back with a report on the console like,
There are 11 of Part Number P-33195 in inventory. It is a class C part. The cost is $24.00. The value of that inventory is $264.00.
Or, if I type in a part that ain’t on file, it should tell me that and I’d like to be able to insert it into the list. I want it to put it exactly into the right spot, so it stays in order.
Then, it should keep on asking for more part numbers, until I say I don't want any more. You know all that info is on the parts.txt file.
After I’m all done and say I don’t want to search for any more part numbers, I’d like a little report telling how many searches, how many were successful and how many new part numbers I added. And, it should write the file out again, so the additions are saved. Simple request for a hotshot programmer, ain’t it?”
Back in your office you look up the file and find out it contains part number, part class, part on hand balance and part cost.
You also note that it is not in order. For sorting, you decide to use a shell sort, because every change to the order of parts needs to be reflected in all the other vectors. You couldn’t do that if you used the sort function from <algorithm> You know you need to copy the file to the directory where your .cpp file will be. You quickly define the problem and decide that you will need parallel vectors or a vector of structs to hold the data and functions to read in the data, sort the vectors, search for the part and then print out the report.
Search for three part numbers that are on the file and then search for and add the following part numbers:
P-10022 C 123 45.00
P-31977 A 12 156.00
P-32344 D 88 12.00
Functions required (Some of the prototypes will be different if you use a vector of structs):
// 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
// See book for information on how to write this
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);
// sorts vectors (Calls swapper)
void sort (vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost);
// prints search stats
void print_stats(int searches, int good, int bad);
// writes out file when program ends, so additions are saved to 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
// Put this BEFORE main() Called from sort function
template <class CType>
void swapper (CType& a, CType & b)
{
CType temp;
temp = a;
a = b;
b= temp;
}
The parts.txt file is the following:
P-39457 A 16 102.14
P-11702 B 21 24.74
P-11754 B 27 15.23
P-12009 D 3 12.80
P-12131 A 48 24.17
P-12385 B 41 27.33
P-12652 E 18 28.33
P-12677 D 15 26.59
P-12746 A 6 111.57
P-12746 C 25 14.83
P-12797 D 18 29.07
P-12856 A 33 120.96
P-13192 C 65 76.98
P-13289 C 45 36.61
P-13682 D 14 25.56
P-13713 A 10 35.12
P-14175 B 41 9.21
P-14226 D 14 28.95
P-14455 D 26 26.39
P-14493 A 29 22.09
P-14518 D 9 5.56
P-14613 A 49 74.66
P-14702 A 38 92.06
P-14748 C 24 24.80
P-38806 C 42 31.39
P-38870 A 29 127.19
P-38880 B 26 110.57
P-39014 A 30 111.33
P-39319 B 26 3.38
P-39499 B 32 37.50
P-39819 B 13 18.71
P-40002 B 9 86.81
P-40021 C 45 27.28
P-40100 B 9 40.04
P-40426 D 32 11.67
P-40784 B 4 106.76
P-41213 C 23 16.53
P-41254 D 5 11.13
P-41316 C 17 33.99
P-41359 A 44 132.94
P-43521 C 25 26.27
P-14976 C 45 22.16
P-15122 B 47 17.96
P-15825 E 27 25.76
P-15902 C 22 26.10
P-15944 D 33 15.94
P-16100 A 40 94.38
P-16283 A 44 108.99
P-16414 C 43 5.12
P-16420 D 12 25.72
P-16467 B 28 72.51
P-16803 D 44 13.61
P-17160 D 23 11.61
P-17243 A 7 69.19
P-17248 D 42 10.50
P-17295 C 2 9.26
P-17344 C 42 36.05
P-17345 B 3 33.14
P-17550 B 23 6.75
P-17644 B 24 2.62
P-17668 D 15 8.86
P-17788 F 34 9.84
P-17833 B 14 25.95
P-17871 C 4 11.70
P-18158 D 25 22.26
P-18553 A 39 5.31
P-18638 C 35 31.64
P-18827 A 26 99.28
P-19041 B 45 63.99
P-19152 A 28 133.91
P-19378 C 44 26.01
P-19505 B 7 54.98
P-19780 A 33 44.66
P-19798 D 17 22.09
P-19842 D 38 5.01
P-19943 D 30 17.46
P-19965 C 34 17.07
P-20097 A 2 26.46
P-20120 B 2 72.45
P-20126 D 6 7.17
P-20143 B 43 38.61
P-20206 B 17 39.00
P-20353 D 8 8.40
P-20486 A 20 186.81
P-20491 B 32 43.20
P-20864 C 27 33.88
P-20884 C 33 27.90
P-21071 A 28 146.96
P-21092 A 43 83.53
P-21282 C 37 10.50
P-21326 F 18 12.09
P-21499 D 30 20.19
P-21599 B 42 41.83
P-22018 B 47 46.58
P-22025 E 25 18.95
P-22083 A 45 3.64
P-22123 F 14 11.51
P-22204 A 46 100.85
P-22254 C 2 7.98
P-22391 B 47 6.69
P-22526 C 37 9.14
P-22547 C 15 32.88
P-22554 A 50 41.31
P-22604 A 10 125.74
P-22757 B 12 47.19
P-22844 D 10 0.50
P-23038 A 29 2.33
P-23102 A 11 56.07
P-23146 D 6 16.89
P-23379 D 43 19.29
P-23506 C 40 14.41
P-23975 D 5 6.84
P-24183 D 13 10.63
P-24253 A 40 120.22
P-24286 B 23 54.52
P-24383 C 44 9.01
P-24613 B 50 57.98
P-24675 E 21 14.22
P-24692 D 33 29.45
P-24724 B 45 6.45
P-24910 A 23 14.27
P-24991 D 34 28.48
P-35349 A 46 80.95
P-35432 A 36 69.73
P-35797 A 4 94.76
P-35809 B 30 30.69
P-35867 D 35 20.86
P-35936 B 22 31.21
P-35961 F 32 0.57
P-36143 D 29 9.53
P-36145 A 5 83.35
P-11196 B 39 59.04
P-11226 C 29 24.46
P-11245 D 45 0.93
P-11273 C 38 21.48
P-11281 B 40 66.47
P-11376 D 50 27.03
P-24992 C 123 45.00
P-13725 A 23 61.46
P-13726 B 12 51.08
P-13754 D 27 4.56
P-13947 C 34 27.71
P-13982 C 17 10.47
P-13987 D 17 27.24
P-13999 B 12 33.01
P-14036 A 41 67.24
P-14124 B 7 69.20
P-25157 A 32 84.01
P-25366 A 37 32.37
P-25405 D 2 8.61
P-25482 A 13 71.09
P-25513 B 15 76.05
P-25549 C 8 4.78
P-25564 D 24 13.30
P-25594 C 16 32.59
P-25602 B 39 15.05
P-25606 C 42 16.95
P-25648 C 3 25.89
P-25746 A 35 90.57
P-25895 D 39 26.89
P-25923 C 2 26.08
P-26188 C 48 32.37
P-26298 A 40 76.50
P-26377 C 21 21.78
P-26382 C 39 15.99
P-26408 F 9 0.37
P-26574 A 38 7.96
P-26576 C 13 23.59
P-26853 A 10 118.43
P-26868 A 2 125.05
P-27191 C 34 20.39
P-27225 C 34 14.28
P-27345 A 28 195.43
P-27417 C 42 1.61
P-27586 B 16 24.21
P-27648 D 10 1.39
P-27785 B 46 26.75
P-27812 C 5 18.37
P-27850 A 46 181.07
P-27873 B 13 102.37
P-27945 D 42 10.44
P-27969 C 2 12.52
P-28009 A 2 60.77
P-27987 C 49 5.37
P-28061 A 35 117.29
P-28309 B 28 34.06
P-28509 D 26 12.95
P-28510 D 13 16.13
P-28518 C 1 22.53
P-28690 A 45 19.34
P-28943 A 21 121.80
P-29044 D 14 6.17
P-29059 D 9 29.82
P-29096 A 49 106.39
P-11191 A 10 34.29
P-29182 A 11 26.83
P-29183 E 46 29.59
P-29185 A 28 73.08
P-29279 C 42 12.55
P-29518 A 17 147.52
P-29549 F 10 7.95
P-29637 B 18 87.18
P-29722 C 25 20.74
P-30037 C 37 22.19
P-30383 A 31 124.74
P-30538 B 1 2.86
P-30591 A 9 40.47
P-30643 A 7 81.94
P-10659 B 17 88.97
P-31007 A 3 11.77
P-31071 B 46 0.36
P-31136 A 29 89.83
P-31429 C 43 5.85
P-31497 A 38 28.12
P-31585 C 8 44.39
P-31701 B 35 51.74
P-31722 A 31 191.32
P-31739 B 24 71.54
P-31744 C 45 15.03
P-31763 A 13 130.56
P-31859 D 24 29.54
P-31969 C 20 17.44
P-32291 A 37 188.37
P-32382 C 44 29.54
P-32417 C 11 27.06
P-32467 C 15 2.73
P-32556 A 44 31.13
P-32599 B 43 66.23
P-32890 A 11 95.99
P-32901 D 32 4.44
P-33017 D 33 5.70
P-33066 C 25 30.27
P-33140 A 32 26.49
P-33143 B 45 44.69
P-33155 D 10 15.25
P-33195 C 11 24.63
P-33224 D 15 8.96
P-33382 C 46 3.44
P-33481 D 20 17.22
P-33491 C 32 24.65
P-33593 C 3 3.52
P-33597 A 2 28.85
P-33643 C 21 5.09
P-34050 B 16 18.99
P-34099 D 49 0.70
P-34106 A 24 63.59
P-34490 F 4 1.65
P-34503 C 7 8.64
P-34512 B 2 69.17
P-34554 A 33 11.27
P-34627 B 22 73.36
P-34670 B 39 112.66
P-34689 B 29 16.06
P-34711 C 1 23.09
P-34743 D 22 25.21
P-34826 B 16 111.01
P-34883 B 49 45.64
P-34991 B 10 41.79
P-34997 A 12 156.00
P-43582 B 3 13.18
P-41383 C 2 2.62
P-41644 D 43 3.40
P-41740 A 25 186.21
P-41754 D 4 20.07
P-41828 D 45 26.95
P-41936 A 11 59.17
P-42059 A 12 78.86
P-42117 C 19 3.07
P-42217 D 45 16.88
P-42259 B 29 73.98
P-42329 A 43 93.94
P-42381 A 37 188.53
P-42634 F 16 7.67
P-42788 C 38 21.88
P-14786 D 22 5.55
P-35146 B 6 62.55
P-11646 D 42 23.58
P-36728 A 15 120.47
P-36826 C 19 18.52
P-36830 B 41 25.29
P-36895 C 11 31.64
P-37009 D 8 23.64
P-37402 C 49 26.02
P-37494 C 1 28.88
P-37504 C 11 13.09
P-37542 B 39 26.43
P-37674 B 17 31.02
P-38033 A 12 70.61
P-38087 B 29 37.76
P-38108 C 44 23.04
P-38247 A 15 128.48
P-38388 C 37 20.84
P-38434 D 49 7.31
P-38460 C 47 28.68
P-38684 B 13 20.30
P-38757 A 30 191.64
P-42792 C 36 17.68
P-42936 A 18 129.79
P-43179 A 47 35.60
P-43264 B 31 103.81
P-43367 B 5 32.39
P-43378 A 46 6.38
P-43622 A 10 155.36
P-43672 A 38 17.97
Explanation / Answer
ScreenShot
-------------------------------------------------------------
Program
//header files for I/O string handling and file manipulation
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
//Prototypes
// 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
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);
// sorts vectors (Calls swapper)
void sort(vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost);
// writes out file when program ends, so additions are saved to 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
// Put this BEFORE main() Called from sort function
template <class CType>
void swapper(CType& a, CType & b)
{
CType temp;
temp = a;
a = b;
b = temp;
}
//Main driver method
int main()
{
//Variable declaration
vector <string> part_number;
vector <char> part_class;
vector <int> part_ohb;
vector <double> part_cost;
char class_in,ch;
int part_ohb_in;
double part_cost_in;
//Call method to read data
get_data(part_number,part_class,part_ohb,part_cost);
//Call method to get parts number
string key=get_target();
/*for (int i = 0; i<part_number.size(); i++)
cout << part_number[i] << " "<< part_class[i]<<" "<< part_ohb[i]<<" "<< part_cost[i]<<endl;*/
//Call search if present or not
int finder = bin_search(key, part_number);
//cout << finder << endl;
//If not ask user to enter other details
while (finder == -1) {
cout << "Do you want to add data(y/n)";
cin >> ch;
if (ch == 'y' || ch == 'Y') {
get_more_data(class_in, part_ohb_in, part_cost_in);
}
else if (ch == 'n' || ch == 'N') {
exit(0);
}
}
//Display details
display(part_number, part_class, part_ohb, part_cost, finder);
//sort
sort(part_number, part_class, part_ohb,part_cost);
put_data(part_number, part_class, part_ohb, part_cost);
/*for (int i = 0; i < part_class.size(); i++) {
cout << "There are " << part_ohb[i] << " of Part Number " << part_number[i] << " in inventory.It is a class " << part_class[i] << " part.The cost is $" << part_cost[i] << ".The value of that inventory is $" << (part_cost[i] * part_ohb[i]) << "." << endl;
}*/
return 0;
}
//Fii text data
bool get_data(vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost) {
ifstream f;
string line;
f.open("C:/Users/deept/Desktop/parts.txt");
if (!f) {
cout << "File not found" << endl;
return false;
}
else {
while (!f.eof()) {
string val1;
char val2;
int val3;
double val4;
f >> val1 >> val2 >> val3 >> val4;
part_number.push_back(val1);
part_class.push_back(val2);
part_ohb.push_back(val3);
part_cost.push_back(val4);
}
return true;
}
}
//Binary search
int bin_search(string key, const vector<string>& part_number) {
int n = part_number.size();
int first = 0;
int last = n - 1;
int middle = (first + last) / 2;
while (first <= last)
{
if (part_number[middle] < key)
{
first = middle + 1;
}
else if (part_number[middle] == key)
{
return middle ;
break;
}
else
{
last = middle - 1;
}
middle = (first + last) / 2;
}
if (first > last)
{
return -1;
}
}
//part number gettinf function
string get_target() {
string key;
cout << "Please enter the part number to search:";
cin >> key;
return key;
}
//Other details need to add
void get_more_data(char& class_in, int& part_ohb_in, double& part_cost_in) {
cout << "Please enter the class of the part:";
cin >> class_in;
cout << "Please enter the count of the part:";
cin >> part_ohb_in;
cout << "Please enter the cost of the part:";
cin >> part_cost_in;
}
//Insert into vector
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);
}
//Display details
void display(const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost, int 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 $" << (part_cost[finder] * part_ohb[finder] )<< "." << endl;
}
//Sort according to parts number
void sort(vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost) {
int i, j, k;
string temp;
int n = part_number.size();
// Gap 'i' between index of the element to be compared, initially n/2.
for (i = n / 2; i > 0; i = i / 2)
{
for (j = i; j < n; j++)
{
for (k = j - i; k >= 0; k = k - i)
{
// If value at higher index is greater, then break the loop.
if (part_number[k + i] >= part_number[k])
break;
// Switch the values otherwise.
else
{
swapper(part_number[k],part_number[k + i]);
swapper(part_class[k], part_class[k + i]);
swapper (part_ohb[k], part_ohb[k + i]);
swapper(part_cost[k], part_cost[k + i]);
}
}
}
}
}
//Save data in to a file
void put_data(const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost){
ofstream of;
string line;
of.open("C:/Users/deept/Desktop/partsOutput.txt");
if (!of) {
cout << "File not found" << endl;
}
else {
for(int i=0;i<part_class.size();i++){
of << part_number[i] << " " << part_class[i] << " " << part_ohb[i] << " " << part_cost[i] << endl;
}
}
}
----------------------------------------------------------------
Output
Please enter the part number to search:P-12385
There are 41 of Part Number P-12385 in inventory.It is a class B part.The cost is $27.33.The value of that inventory is $1120.53.
Press any key to continue . . .
----------------------------------------------------
Note:-
I add all new data at the end of vectors and then sort .So it will store proper place in new file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.