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
|
/*
Alfred J. Denigris
CSC 211 Lab 4
March 19, 2009
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
const int DATAMAX = 20;
// Data Structyre Types
//---------------------
struct nameType
{
string first;
string last;
};//end nameType
struct resType
{
int limit;
double used;
double percentUsed;
};//end resType
struct userType
{
nameType name;
int userID;
resType resource;
char flag;
};//end userType
//Prototype Functions
//-------------------
void chooseInputFile(ifstream& inFile, char fileName[]);
void chooseOutputFile(ofstream& outFile);
void getTheDataIntoTheStruct(userType user[],ifstream& inFile, int& arraysize);
double calcPercentUsed(int limit,double used);
void decendingOrderSort(userType list[], int arraysize);
void output(ofstream& outFile, userType user[], int arraysize, char filename[]);
// Main Program
//-------------
void main()
{
//Local variables
//---------------
userType user[DATAMAX];// declare structured data array
ifstream inFile;
ofstream outFile;
int arraysize=0;
char fileName[50];
bool again=true;
char ch1;
while (again)
{ char fileName[50];
//Start
chooseInputFile(inFile, fileName);// choose input file
chooseOutputFile(outFile);// choose output file
getTheDataIntoTheStruct(user,inFile,arraysize);//Get Data
decendingOrderSort(user, arraysize); // Sort data
output(outFile,user,arraysize,fileName); // Output data
inFile.close();
outFile.close();
cout <<"Do you want to add anther file? y or n ";
cin >> ch1;
if (ch1 == 'y' || ch1 =='Y')
again = true;
else
again= false;
}
}// End of Main Program
//---------------------
// Functions
//--------------
void chooseInputFile(ifstream& inFile, char fileName[]) //Function to choose a file to open
{
cout <<"Which file would you like to read data from? \n:";
cin >> fileName;
inFile.open(fileName);
cout <<fileName<< " has been opened for intput\n";
}//end chooseInputFile
void chooseOutputFile(ofstream& outFile) //Function to choose a file to open
{
char fileName[50];
cout <<"Which file would you like to write output data to?? \n:";
cin >> fileName;
outFile.open(fileName, ios::app);
cout <<fileName<< " has been opened for output.\n";
}//end chooseInputFile
void getTheDataIntoTheStruct(userType user[],ifstream& inFile, int& arraysize) // Fills Up a Struct
{
for (int i=0; !inFile.eof(); i++)
{
inFile >> user[i].name.last; // get name.last
inFile >> user[i].name.first; // get name.first
inFile >> user[i].userID; // get userID
inFile >> user[i].resource.limit; // get resource.limit
inFile >> user[i].resource.used; // get resourece.used
user[i].resource.percentUsed = calcPercentUsed(user[i].resource.limit, user[i].resource.used);
// calculate resource.percentUsed
(user[i].resource.percentUsed >= 90) ?user[i].flag='*' :user[i].flag=' ';
// If %used is > 90, put * in flag
arraysize++;// Keep track of how many users have been input
}
}//end getTheDataIntoTheStruct
double calcPercentUsed(int limit,double used) //calculate Percent Used, Return Double percentUsed
{
return static_cast<double>(used/limit)*100;
}
// selection sort function
void decendingOrderSort(userType list[], int arraysize)
{
int length = arraysize;
int i, icopy, i2;
userType temp;
//cout << "length = "<<length<<endl;
for (i = 0; i <length -1; i++)
{
icopy = i;
for (i2= i+1; i2 <length; i2++)
{
if (list[icopy].resource.percentUsed < list[i2].resource.percentUsed)
icopy = i2; // make sure that icopy index points to the largest value
}
temp = list[icopy];
list[icopy] = list[i];
list[i] = temp;// switch index with largest
}
}
void output(ofstream& outFile, userType user[], int arraysize, char fileName[])
{
outFile <<"\nHere is the Data Report for the file\n"<<fileName;
outFile<<endl;
outFile <<setw(25)<<left<<"Name (First, Last)"<<setw(15)<<left<<"Student ID#"<<setw(15)<<left<<"Res. Limit"<<setw(10)<<left<<"Res. Used"<<setw(10)<<left<<"% Used" <<setw(10)<<left<<"Over 90%?"<<endl;
for (int i=0; i<85; i++)
outFile <<'_';
outFile<<endl;
for ( i=0; i<arraysize; i++)
{
outFile <<setw(13)<<left<< user[i].name.last; // get name.last
outFile <<setw(12)<<left<< user[i].name.first; // get name.first
outFile <<setw(15)<<left<< user[i].userID; // get userID
outFile <<setw(15)<<left<< user[i].resource.limit; // get resource.limit
outFile <<setw(10)<<left<< user[i].resource.used; // get resourece.used
outFile <<setw(10)<<left<<setprecision(2)<<fixed<<showpoint<< user[i].resource.percentUsed;
outFile <<setw(10)<<left<< user[i].flag;
outFile <<endl;
}//for(int i=0, i<DATAMAX; i++)
cout <<endl;
}
|