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
|
#include <fstream>
#include <iostream>
using namespace std;
const int MaxSize=500;
struct Frame //structure consisting of a Name (a string), a Width (an integer), a Length (an integer), and a Cost (a float)
{
string Name;
int Width;
int Length;
float Cost;
};
void ReadFrame(ifstream& Input, Frame Box[], int& Howmany); // pull relevant details from datafile.
void PrintPrices(Frame Box[], int Howmany); //Display contents of the "box" and calculate finished price.
void PrintArea(Frame Box[], int Howmany); //Display contents of the "box" and calculate the area of the frame.
void PrintPerimeter(Frame Box[], int Howmany); //Display contents of the "box" and calculate the perimeter.
int FindHighestCost(Frame Box[], int Howmany); // analyze the array and display the highest Cost value.
float FindAverageCost(Frame Box[], int Howmany); // Find average of all Cost values.
int FindFirst(Frame Box[], int Howmany); // Determine the box with the lowest value, alphabetically.
int FindLast(Frame Box[], int Howmany); // Determine the box with the highest value, alphabetically.
int main(int argc, char *argv[])
{
Frame Box[MaxSize];
int Largest;
int Howmany;
float Price;
int Area;
int Perimeter;
float Average = 0;
int First;
int Last;
if (argc != 2) // Verify that a datafile was provided when command was entered.
{
cerr << "Incorrect number of command line arguments given." << endl;
return -1;
}
ifstream Input;
Input.open(argv[1]);
if (!Input)
{
cerr << "Could not open file: " << argv[1] << endl;
return -1;
}
ReadFrame(Input, Box, Howmany);
cout << "The Prices:" << endl;
PrintPrices(Box , Howmany);
cout << endl << endl;
cout <<"The Areas:" << endl;
PrintArea(Box , Howmany);
cout << endl << endl;
cout<< "The Perimeters:" << endl;
PrintPerimeter(Box , Howmany);
cout << endl << endl;
Largest = FindHighestCost(Box, Howmany);
First = FindFirst(Box, Howmany);
Last = FindLast(Box, Howmany);
cout << "The greatest cost item is: " << Box[Largest].Cost << " found at position " << Largest << " and with name " << Box[Largest].Name<< endl;
Average = FindAverageCost(Box, Howmany);
cout << "The average of the cost is: " << Average << endl;
cout << "The first item on the list is: " << endl << Box[First].Name << ' ' << Box[First].Width << ' ' << Box[First].Length << ' ' << Box[First].Cost << endl;
cout << "The last item on the list is: " << endl << Box[Last].Name << ' ' << Box[Last].Width << ' ' << Box[Last].Length << ' ' << Box[Last].Cost << endl;
return 0;
}
void ReadFrame(ifstream& Input, Frame Box[], int& Howmany) //Read values from file, and input them into proper array for processing.
{
int Counter = 0;
Input >> Box[Counter].Name;
Input >> Box[Counter].Width;
Input >> Box[Counter].Length;
Input >> Box[Counter].Cost;
while (Input && Counter > -1 && Counter<MaxSize)
{
++Counter;
Input >> Box[Counter].Name;
Input >> Box[Counter].Width;
Input >> Box[Counter].Length;
Input >> Box[Counter].Cost;
}
Howmany = Counter;
}
//This function prints the Frame information (name, width, length, cost) plus price. The price for each frame is the area of the frame times the cost.
void PrintPrices( Frame Box[], int Howmany)
{
for (int i=0; i < Howmany ; i++)
cout << Box[i].Name << ' ' << Box[i].Width << ' ' << Box[i].Length << ' ' << Box[i].Cost << ' ' << Box[i].Length * Box[i].Width * Box[i].Cost << endl;
}
void PrintArea(Frame Box[], int Howmany)
{
for (int i=0; i < Howmany ; i++)
cout << Box[i].Name << ' ' << Box[i].Width << ' ' << Box[i].Length << ' ' << Box[i].Cost << ' ' << Box[i].Length * Box[i].Width << endl;
}
void PrintPerimeter(Frame Box[], int Howmany)
{
for (int i=0; i < Howmany ; i++)
cout << Box[i].Name << ' ' << Box[i].Width << ' ' << Box[i].Length << ' ' << Box[i].Cost << ' ' << 2 * (Box[i].Length + Box[i].Width) << endl;
}
int FindHighestCost(Frame Box[], int Howmany)
{
int Largest = 0;
for(int i = 0; i < Howmany; ++i)
while (Box[i].Cost > Box[Largest].Cost)
Largest = i;
return Largest;
}
float AverageCost(Frame Box[], int Howmany)
{
int Sum = 0;
float avg = 0;
for(int i=0; i< Howmany; ++i)
Sum = Sum + Box[i].Cost;
avg = float (Sum)/Howmany;
return avg;
}
int FindFirst(Frame Box[], int Howmany)
{
int least = -1;
for(int i=0; i< Howmany; ++i)
while (Box[least].Name < Box[i].Name)
least = i;
return least;
}
int FindLast(Frame Box[], int Howmany)
{
int greatest = 0;
for(int i=0; i< Howmany; ++i)
while (Box[greatest].Name > Box[i].Name)
greatest = i;
return greatest;
}
|