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
|
#include "Resolver.h"
#include <cstdlib>
#include <fstream>
using namespace std;
Resolver::Resolver()
{
//ctor
}
Resolver::Resolver(string t)
{
//ctor
setFileNames(t);
}
Resolver::~Resolver()
{
//dtor
}
void Resolver::setFileNames(string t){
book_Data=t;
}
void Resolver::doIt(){
int bookCnt;
bookList X[MaxBooks];
readDataFromFiles("testdata.txt", bookCnt, X);
solve1(bookCnt, X);
solve2(bookCnt, X);
solve3(bookCnt, X);
solve4(bookCnt, X);
solve5(bookCnt, X);
}
void Resolver::parseTextLine(string tmp, int & bookCnt, bookList X[]){
std::size_t found;
found=tmp.find(";");
if (found!=string::npos) {
X[bookCnt].author=tmp.substr(0,found);
tmp=tmp.substr(found+1);
}
found=tmp.find(";");
if (found!=string::npos) {
X[bookCnt].title=tmp.substr(0,found);
tmp=tmp.substr(found+1);
}
found=tmp.find(";");
if (found!=string::npos) {
X[bookCnt].pageNumber=atoi(tmp.substr(0,found).c_str());
tmp=tmp.substr(found+1);
}
bookCnt++;
}
void Resolver::readDataFromFiles(string bookFile, int & bookCnt, bookList X[]){
bookCnt=0;
ifstream finS(bookFile.c_str(),ios::in);
bool first=true;
while (!finS.eof()) {
string tmp="";
getline(finS,tmp);
if (tmp!="") {
if (first) {
first=!first;
} else {
parseTextLine(tmp,bookCnt,X);
}
}
}
finS.close();
}
void Resolver::solve1(int bookCnt, const bookList X[]){
//maximum pick
int j=0;
for (int i=1;i<bookCnt;i++) {
if (X[i].pageNumber>X[j].pageNumber) {
j=i;
}
}
cout << X[j].title << endl;
}
void Resolver::solve2(int bookCnt, const bookList X[]){
bool pNGT = false;
int j;
for (int i=1;i<bookCnt;i++) {
if (X[i].pageNumber<15) {
pNGT = true;
j=i;
}
}
if(pNGT == true){
cout << X[j].title << " Has less than 15 pages" << endl;
}
else{
cout << "No books have less than 15 pages" << endl;
}
}
void Resolver::solve3(int bookCnt, const bookList X[]){
bool exist = false;
int j;
for (int i=1;i<bookCnt;i++) {
if (X[i].title=="The Old Man and the Sea") {
exist = true;
j=i;
}
}
if(exist == true){
cout << X[j].author << endl;
}
else{
cout << "Does not exist" << endl;
}
}
void Resolver::solve4(int bookCnt, const bookList X[]){
int sum = 0;
for (int i=1;i<bookCnt;i++) {
sum+=X[i].pageNumber;
}
cout << sum << endl;
}
void Resolver::solve5(int bookCnt, const bookList X[]){
int jules[MaxN];
int j = 0;
for (int i=1;i<bookCnt;i++) {
if (X[i].author == "Jules Verne") {
jules[j] = i;
j++;
}
}
for (int p=0;p<j;p++) {
cout << jules[p] << endl;
}
}
|