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
|
#include <iostream>
#include <fstream>
using namespace std;
#define MAX 50
//declare functions
int input(ifstream&, int[], double[], int [], int []);
void output(ofstream&, int[], double[], int[], int[], int howMany);
int search(int[], int, int);
int getExpensive(double[], int);
void sortSelect(double arr[], int num, int id[], int qty[], int rop[]);
int main(void)
{
//Declare variables
ifstream inFile; ofstream fpOut;
int id[MAX];
double price[MAX];
int qty[MAX];
int rop[MAX];
int number; int target; int found; int index;
//Open file
inFile.open("products.txt");
if (inFile.fail())
{
cout << "No such file";
system("pause");
exit(100);
}
fpOut.open("inventOut.txt");
//Input the entire contents of the product file
number = input(inFile, id, price, qty, rop);
//Looking for a particular product
cout << "Input product id for search: ";
cin >> target;
found = search(id, number, target);
if (found == -1)
cout << "No Such Product!" << endl;
else
cout << id[found] << "\t" << price[found] << "\t" << qty[found] << "\t"
<< rop[found] << endl;
//Find most expensive
index = getExpensive(price, number);
cout << "Most expensive:" << id[index] << "\t" << price[index] << endl << endl;
//Sort by the product id
sortSelect(price, number, id, qty, rop);
//Output each product's id, quantity on hand, re-order point, and price
output(fpOut, id, price, qty, rop, number);
system("pause");
return 0;
}
int input(ifstream& inFile, int id[], double price[], int qty[], int rop[])
{
/*Pre: inFile - reference data file
id[] - array of product ids
price[] - array of unit cost
qty[] - array of quantity on hand
rop[] - array of reorder point for each product
Post: Number of products
Purpoe: input entire inventory into parallel arrays*/
int num = 0;
while (!inFile.eof())
{
inFile >> id[num];
inFile >> price[num] >> qty[num] >> rop[num];
num++;
}
return num;
}//input
void output(ofstream& fpOut, int id[], double cost[], int qty[], int rop[], int num)
{
fpOut << "Mary Pape" << endl << endl;
for (int i = 0; i < num; i++)
fpOut << id[i] << "\t" << cost[i] << "\t" << qty[i] << "\t" << rop[i] << endl;
}
int search(int id[], int num, int target)
{
/*Pre: id[] - array of product codes
num - number of items in inventory
target - product being searched for
Post: the index of the target
Purpose: Search inventory for specific item*/
int found = -1;
for (int i = 0; i < num; i++)
{
if (target == id[i])
found = i;
}
return found;
}
int getExpensive(double price[], int num)
{
int index = 0;
double great = price[index];
for (int i = 0; i < num; i++)
{
if (price[i] > great)
{
index = i;
great = price[index];
}
}
return index;
}
void sortSelect(double arr[], int num, int id[], int qty[], int rop[])
{
int current; int walker;
int smallestIndex;
double temp; int tempInt;
for (current = 0; current < num - 1; current++)
{
smallestIndex = current;
for (walker = current; walker < num; walker++)
{
if (arr[walker] < arr[smallestIndex])
smallestIndex = walker;
}//for walker
//Swap to position smallest at what is the current position
temp = arr[current];
arr[current] = arr[smallestIndex];
arr[smallestIndex] = temp;
tempInt = id[current];
id[current] = id[smallestIndex];
id[smallestIndex] = tempInt;
tempInt = qty[current];
qty[current] = qty[smallestIndex];
qty[smallestIndex] = tempInt;
tempInt = rop[current];
rop[current] = rop[smallestIndex];
rop[smallestIndex] = tempInt;
}//for current
return;
}
| |