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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
// function to sort and output data to a new file
// - overloaded << operator
#include <iostream>
#include <fstream>
#include <string>
#include <streambuf>
const int MAX_CHARS_PER_LINE = 1000;
const int MAX_TOKENS_PER_LINE = 5;
const char* const DELIMITER = ",";
using namespace std;
std::fstream t("Book_List.txt");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
template<typename T>
class Counter {
public:
Counter() { ++count; }
Counter(const Counter&) { ++count; }
~Counter() { --count; }
static size_t howMany()
{ return count; }
private:
static size_t count;
};
template<typename T>
size_t
Counter<T>::count = 0; // this now can go in header
template<class T>
void sort(T* v,int n)
{
for (int i = 1; i < n; i++)
for (int j = 0; j=0, j< n-i; j++)
if (v[j] > v[j+1])
swap (v[j],v[j+1]);
}
template<class T>
int findLines(string)
{
int numLines = 0;
ifstream in("file.txt");
//while ( ! in.eof() )
while ( in.good() )
{
std::string line;
std::getline(in, line);
++numLines;
}
return numLines;
}
class Book
{
public:
Book();
Book(int);
Book(string);
Book(long, long);
Book(const Book&);
Book(string, string, string, double, double, double);
void setBookData(string, string, string, double, double, double);
void setAuthor(string);
void setTitle(string);
void setISBN(string);
void setQS(double);
void setPrice(double);
void setQOH(double);
void getBookData(string, string, string, double, double, double); // friend ostream& operator=(ifstream &, string);
Book& operator = (const Book&);
Book operator = (string); //book will cout string
Book& operator = (ifstream &);
friend istream& operator >> (istream &, const Book&);
friend ostream& operator << (ostream &, const Book&);
int a;
string num;
long end, begin;
private:
string Title, Author, ISBN;
double Price, QOH, QS;
};
Book :: Book()
{ }
Book :: Book(int i)
{
a = i;
}
Book :: Book(string z) : num(z)
{ }
Book :: Book(long b, long e)
{
b = 0;
e = 0;
}
Book :: Book(const Book& v) : Title(v.Title), Author(v.Author), Price(v.Price), QOH(v.QOH), QS(v.QS), ISBN(v.ISBN)
{ } // copy constructor
Book :: Book(string t, string a, string isbn, double p, double qoh, double qs )
{
Title = t;
Author = a;
ISBN = isbn;
Price = p;
QOH = qoh;
QS = qs;
}
void Book :: setBookData(string t, string a, string isbn, double p, double qoh, double qs)
{
Title = t;
Author = a;
Price = p;
QOH = qoh;
QS = qs;
ISBN = isbn;
}
void Book :: getBookData(string t, string a,string isbn, double p, double qoh, double qs)
{
t = Title;
a = Author;
isbn = ISBN;
p = Price;
qoh = QOH;
qs = QS;
}
void Book :: setAuthor(string t)
{
Author = t;
}
void Book :: setISBN(string i)
{
ISBN = i;
}
void Book :: setTitle(string a)
{
Title = a;
}
void Book :: setPrice(double p)
{
Price = p;
}
void Book :: setQS (double q)
{
QS = q;
}
void Book :: setQOH( double qo)
{
QOH = qo;
}
Book& Book::operator=(const Book& v) //passed in from copy constructor
{
if (this != &v)
{//if they are the same object, we can skip copying of x
//if different copy into this instance
Title = v.Title;
Author = v.Author;
Price = v.Price;
QOH = v.QOH;
QS = v.QS;
ISBN = v.ISBN;
}
return *this; //return dereferenced instance of Fractionv to enable cascading assignment.
}
/*
void Book operator=(ifstream& ifs, string s)
{
Book temp;
temp.num = s;
return temp;
} */
istream& operator >> (istream &is, const Book &v)
{
cout << "enter any number: " << endl;
is >> v.num;
return is;
}
ostream &operator << (ostream &os, const Book&v)
{
os<< v.Title << v.Author << v.ISBN << v.Price << v.QOH << v.QS;
return os;
}
int main()
{
int num;
long begin,end;
begin = 0;
end = 0;
string bookstring;
Book BookS[5];
// create a file-reading object
ifstream indata;
indata.open("Book_List.csv"); // open a file
if (!indata.good())
return 1; // exit if file not found
while (!indata.eof())
{
char buf[MAX_CHARS_PER_LINE];
indata.getline(buf, MAX_CHARS_PER_LINE);
int n = 0;
const char* token[MAX_TOKENS_PER_LINE] = {0}; // initialize to 0
// parse the line
token[0] = strtok(buf, DELIMITER); // first token
if (token[0]) // zero if line is blank
{
for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
{
token[n] = strtok(0, DELIMITER); // subsequent tokens
if (!token[n] || token[n]==0) break; // no more tokens
}
}
// process (print) the tokens
for (int n = 0; n < MAX_TOKENS_PER_LINE; n++) // n = #of tokens
{
cout << "Token[" << n << "] = " << token[n] << endl;
cout << endl;
}
indata.close();
}
cin.get();
return 0;
}
// function to find out how many objects to create (function find)
// create an array (of tokens) with that many objects (function create)
// - use seekg to get to the beginning of file before starting (sort through file)
// function to read information from file into array of objects (read content into array of objects)
// - use substr in the parsing process
// - use atoi and atof in the parsing process
// loop to call sorting function based on 3 different criteria (3 sort criteria)
// - sorting function will call a function template to compare values
// - print results for each sort
// function to sort and output data to a new file
// - overloaded << operator
|