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
|
#include <iostream>
#include <fstream>
#include <iomanip> // Required for setw
using namespace std;
//#define const int SIZE = 20; //global variable
//function prototypes
void readData(int item_num[], float cost[], int quantity[], int SIZE);
void sortData(int item_num[], float cost[], int quantity[], int SIZE);
void printTable(int item_num[], float cost[], int quantity[], int SIZE);
int search(int item_num[], float cost[], int quantity[], int SIZE);
void print(int item_num[], float cost[], int quantity[], int position);
int main()
{
//declare variables
const int SIZE = 20;
int item_num[SIZE];
float cost[SIZE];
int quantity[SIZE];
//read data from file.
readData(item_num, cost, quantity, SIZE);
//sort data into parallel arrays.
sortData(item_num, cost, quantity, SIZE);
//print table
printTable(item_num, cost, quantity, SIZE);
//search for item location using a search algorithm
int position = search(item_num, cost, quantity, SIZE);
//print results of search.
print(item_num, cost, quantity, position);
//end program
return 0;
}
void readData(int item_num[], float cost[], int quantity[], int SIZE )
{
ifstream inFile;
inFile.open ("parts.txt");
for (int count=0; count < SIZE; count++)
{
inFile >> item_num[count]>> cost[count]>> quantity[count];
}
inFile.close();
return;
}
void sortData(int item_num[], float cost[], int quantity[], int SIZE)
{
bool swap;
int temp_num = 0;
float temp_cost = 0.0;
int temp_quantity = 0;
do
{
swap=false;
for (int count = 0; count < (SIZE-1); count++)
{
if (item_num[count] > item_num[count + 1] )
{
temp_num = item_num[count];
temp_cost = cost[count];
temp_quantity = quantity[count];
item_num[count] = item_num[count + 1];
cost[count] = cost[count+1];
quantity[count] = quantity[count+1];
item_num[count + 1] = temp_num;
cost[count + 1] = temp_cost;
quantity [count + 1] = temp_quantity;
swap = true;
}
}
} while (swap);
return;
}
void printTable(int item_num[], float cost[], int quantity[], int SIZE)
{
cout<<"Item #" << setw(8) << "Cost" << setw(8) << "Quantity" << endl;
cout<<"-------------------------------------"<<endl;
for(int i=0; i<SIZE; i++)
{
cout << item_num[i] << setw(8)<< cost[i] <<setw(8) << quantity[i]<< endl;
}
return;
}
int search(int item_num[], float cost[], int quantity[], int SIZE)
{
int searchNum=0;
cout << "Enter valid item number: ";
cin >> searchNum;
cout << endl;
int first = 0,
last = SIZE-1,
middle,
position = 0;
bool found= false;
while (!found && first <= last )
{
middle = (first + last) / 2;
if (item_num[middle] == searchNum)
{
found = true;
position = middle;
}
else if (item_num[middle] > searchNum )
last = middle - 1;
else
first = middle + 1;
}
return position;
}
void print(int item_num[], float cost[], int quantity[], int position)
{
cout << endl << "Item #: " << item_num[position] << setw(15)<< "Cost: " << cost[position] << setw(8) << "Quantity: " << quantity[position] <<endl;
return;
}
|