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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class book
{
public:
book();
book(string catalog, string lname, string fname, string title, string genre, string available);
//Accesor Functions
string getCatalogNumber();
string getLastName();
string getFirstName();
string getBookTitle();
string getBookGenre();
string getAvailability();
//Mutator Functions
void setCatalogNumber(string catalog);
void setLastName(string lname);
void setFirstName(string fname);
void setBookTitle(string title);
void setBookGenre(string genre);
void setAvailability(string available);
//other member functions
void sortLastName(book array[], int length);
void sortGenre(book array[], int length);
private:
string catalogNumber,
lastName,
firstName,
booktitle,
bookgenre,
availability;
};
int main(int argc, char* argv[])
{
ifstream inputFile1, inputFile2;
ofstream outputFile1, outputFile2, outputFile3;
inputFile1.open("input1.txt");
string catalog, lname, fname, title, genre, available; // placeholder for contents in inputFile1.
int size = 0;
book arr[100]; // creating a book array so as to store the objects we extract form the inputFile1.
while(inputFile1 >> catalog >> lname >> fname >> title >> genre >> available)
{
arr[size].setCatalogNumber(catalog);
arr[size].setLastName(lname);
arr[size].setFirstName(fname);
arr[size].setBookTitle(title);
arr[size].setBookGenre(genre);
arr[size].setAvailability(available);
size++;
}
inputFile2.close();
// the following code computes computational tasks 1 and 2.
sortLastName(arr, size);
outputFile1.open("output1.txt");
for(int i = 0; i < size; i++)
{
outputFile1 << arr[i].getCatalogNumber() << " " << arr[i].getLastName()
<< " " << arr[i].getFirstName() << " " << arr[i].getBookTitle()
<< " " << arr[i].getBookGenre() << " " << arr[i].getAvailability() << endl;
}
outputFile1.close();
sortGenre(arr,size);
outputFile2.open("output2.txt");
for(int i = 0; i < size; i++)
{
outputFile2 << arr[i].getCatalogNumber() << " " << arr[i].getLastName()
<< " " << arr[i].getFirstName() << " " << arr[i].getBookTitle()
<< " " << arr[i].getBookGenre() << " " << arr[i].getAvailability() << endl;
}
outputFile2.close();
// the following code computes the computational task 3.
inputFile2.open("input2.txt");
outputFile3.open("output3.txt");
string requestCatalog;
bool found;
while(inputFile2 >> requestCatalog)
{
for(int p = 0; p < size; p++)
{
if((arr[p].getCatalogNumber() == requestCatalog) && (arr[p].getAvailability() == "available"))
{
outputFile3 << requestCatalog << " " << arr[p].getBookTitle() << " available" << endl;
found = true;
break;
}
else if((arr[p].getCatalogNumber() == requestCatalog) && (arr[p].getAvailability() == "unavailable"))
{
outputFile3 << requestCatalog << " " << arr[p].getBookTitle() << " unavailable" << endl;
found = true;
break;
}
else
found = false;
}
if(!found)
outputFile3 << requestCatalog << " unavailable" << endl;
}
inputFile2.close();
outputFile3.close();
return 0;
}
book::book()
{
string catalogNumber = "",
lastName = "",
firstName = "",
booktitle = "",
bookgenre = "";
availability= "";
}
book :: book(string catalog, string fname, string lname, string title, string genre, string available)
{
catalogNumber = catalog;
lastName = lname;
firstName = fname;
booktitle = title;
bookgenre = genre;
availability = available;
}
string book :: getCatalogNumber()
{
return catalogNumber;
}
string book :: getLastName()
{
return lastName;
}
string book :: getFirstName()
{
return firstName;
}
string book :: getBookTitle()
{
return booktitle;
}
string book :: getBookGenre()
{
return bookgenre;
}
string book :: getAvailability()
{
return availability;
}
void book :: setCatalogNumber(string catalog)
{
catalogNumber = catalog;
}
void book :: setLastName(string lname)
{
lastName = lname;
}
void book :: setFirstName(string fname)
{
firstName = fname;
}
void book :: setBookTitle(string title)
{
booktitle = title;
}
void book :: setBookGenre(string genre)
{
bookgenre = genre;
}
void book :: setAvailability(string available)
{
availability = available;
}
void book :: sortLastName(book array[], int length)
{
for(int i = 0; i < length-1; i++)
{
for(int j = i+1; j < length; j++)
{
if(array[j].lastName < array[i].lastName)
{
string temp = array[i].lastName;
array[i].lastName = array[j].lastName;
array[j].lastName = temp;
}
}
}
}
void book :: sortGenre(book array[], int length)
{
for(int i = 0; i < length-1; i++)
{
for(int j = i+1; j < length; j++)
{
if(array[j].bookgenre < array[i].bookgenre)
{
string temp = array[i].bookgenre;
array[i].bookgenre = array[j].bookgenre;
array[j].bookgenre = temp;
}
}
}
}
|