Skip to content

Commit e2d9e05

Browse files
committed
update chapter14 codes
1 parent e772744 commit e2d9e05

33 files changed

+2191
-1
lines changed

chapter14/.DS_Store

6 KB
Binary file not shown.

chapter14/assignment/.DS_Store

6 KB
Binary file not shown.

chapter14/assignment/14.7.1main.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include "14.7.1winec.h"
3+
4+
5+
int main(void)
6+
{
7+
using std::cout;
8+
using std::cin;
9+
using std::endl;
10+
11+
12+
cout << "Enter name of wine: ";
13+
char lab[50];
14+
cin.getline(lab, 50);
15+
cout << "Enter number of years: ";
16+
int yrs;
17+
cin >> yrs;
18+
19+
Wine holdings(lab, yrs);
20+
holdings.GetBottles();
21+
holdings.Show();
22+
23+
const int YRS = 3;
24+
int y[YRS] = {1993, 1995, 1998};
25+
int b[YRS] = {48, 60, 72};
26+
27+
Wine more("Gushing Grapd Red", YRS, y, b);
28+
more.Show();
29+
30+
cout << "Total bottles for " << more.Label()
31+
<< ": " << more.sum() << endl;
32+
33+
cout << "Bye\n";
34+
return 0;
35+
}

chapter14/assignment/14.7.1pairs.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#ifndef PAIRS_H_
3+
#define PAIRS_H_
4+
5+
#include <iostream>
6+
#include <string>
7+
8+
9+
template <typename T1, typename T2>
10+
class Pair
11+
{
12+
private:
13+
T1 a;
14+
T2 b;
15+
public:
16+
T1 & first();
17+
T2 & second();
18+
T1 first() const {return a;}
19+
T2 second() const {return b;}
20+
Pair(const T1 & aval, const T2 & bval): a(aval), b(bval) {}
21+
Pair(){}
22+
};
23+
24+
25+
template<typename T1, typename T2>
26+
T1 & Pair<T1, T2>::first()
27+
{
28+
return a;
29+
}
30+
31+
32+
template<typename T1, typename T2>
33+
T2 & Pair<T1, T2>::second()
34+
{
35+
return b;
36+
}
37+
38+
39+
#endif

chapter14/assignment/14.7.1winec.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "14.7.1winec.h"
2+
#include <iostream>
3+
4+
5+
Wine::Wine()
6+
{
7+
label = "";
8+
ArrayInt _yrs;
9+
ArrayInt _bot;
10+
yearBottles = PairArray(_yrs, _bot);
11+
years = 0;
12+
}
13+
14+
15+
Wine::Wine(const char * l, int y, const int yr[], const int bot[])
16+
{
17+
label = std::string(l, l + strlen(l));
18+
years = y;
19+
ArrayInt _yrs(yr, y);
20+
ArrayInt _bot(bot, y);
21+
yearBottles = PairArray(_yrs, _bot);
22+
}
23+
24+
25+
Wine::Wine(const char * l, int y)
26+
{
27+
label = std::string(l, l + strlen(l));
28+
years = y;
29+
ArrayInt _yrs;
30+
ArrayInt _bot;
31+
yearBottles = PairArray(_yrs, _bot);
32+
}
33+
34+
void Wine::GetBottles()
35+
{
36+
std::cout << "Enter " << label << " data for " << years << " year(s):" << std::endl;
37+
int index = 0;
38+
int _yearArr[years];
39+
int _bottlesArr[years];
40+
41+
while(index < years)
42+
{
43+
std::cout << "Enter year: ";
44+
int _tempYr;
45+
std::cin >> _tempYr;
46+
_yearArr[index] = _tempYr;
47+
48+
std::cout << "Enter bottles for that year: ";
49+
int _tempBt;
50+
std::cin >> _tempBt;
51+
_bottlesArr[index] = _tempBt;
52+
index++;
53+
}
54+
55+
ArrayInt _years(_yearArr, years);
56+
ArrayInt _bottles(_bottlesArr, years);
57+
58+
yearBottles = PairArray(_years, _bottles);
59+
60+
}
61+
62+
63+
void Wine::Show()
64+
{
65+
std::cout << "Wine: " << label << std::endl;
66+
std::cout.width(10);
67+
std::cout << "Year";
68+
std::cout.width(10);
69+
std::cout << "Bottles" << std::endl;
70+
for(int i = 0; i < years; i++)
71+
{
72+
std::cout.width(10);
73+
std::cout << yearBottles.first()[i];
74+
std::cout.width(10);
75+
std::cout << yearBottles.second()[i]<< std::endl;
76+
}
77+
}
78+
79+
std::string & Wine::Label()
80+
{
81+
return label;
82+
}
83+
84+
85+
int Wine::sum()
86+
{
87+
int sum = 0;
88+
for(int i = 0; i < years; i++)
89+
{
90+
sum += yearBottles.second()[i];
91+
}
92+
93+
return sum;
94+
}

chapter14/assignment/14.7.1winec.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef WINEC_H_
2+
#define WINEC_H_
3+
4+
#include "14.7.1pairs.h"
5+
6+
7+
#include <valarray>
8+
#include <string>
9+
10+
11+
typedef std::valarray<int> ArrayInt;
12+
typedef Pair<ArrayInt, ArrayInt> PairArray;
13+
14+
15+
class Wine
16+
{
17+
private:
18+
std::string label;
19+
PairArray yearBottles;
20+
int years;
21+
public:
22+
Wine();
23+
Wine(const char * l, int y, const int yr[], const int bot[]);
24+
Wine(const char * l, int y);
25+
void GetBottles();
26+
void Show();
27+
std::string & Label();
28+
int sum();
29+
};
30+
31+
#endif

chapter14/assignment/14.7.2main.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include "14.7.1winec.h"
3+
4+
5+
int main(void)
6+
{
7+
using std::cout;
8+
using std::cin;
9+
using std::endl;
10+
11+
12+
cout << "Enter name of wine: ";
13+
char lab[50];
14+
cin.getline(lab, 50);
15+
cout << "Enter number of years: ";
16+
int yrs;
17+
cin >> yrs;
18+
19+
Wine holdings(lab, yrs);
20+
holdings.GetBottles();
21+
holdings.Show();
22+
23+
const int YRS = 3;
24+
int y[YRS] = {1993, 1995, 1998};
25+
int b[YRS] = {48, 60, 72};
26+
27+
Wine more("Gushing Grapd Red", YRS, y, b);
28+
more.Show();
29+
30+
cout << "Total bottles for " << more.Label()
31+
<< ": " << more.sum() << endl;
32+
33+
cout << "Bye\n";
34+
return 0;
35+
}

chapter14/assignment/14.7.2pairs.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#ifndef PAIRS_H_
3+
#define PAIRS_H_
4+
5+
#include <iostream>
6+
#include <string>
7+
8+
9+
template <typename T1, typename T2>
10+
class Pair
11+
{
12+
private:
13+
T1 a;
14+
T2 b;
15+
public:
16+
T1 & first();
17+
T2 & second();
18+
T1 first() const {return a;}
19+
T2 second() const {return b;}
20+
Pair(const T1 & aval, const T2 & bval): a(aval), b(bval) {}
21+
Pair(){}
22+
};
23+
24+
25+
template<typename T1, typename T2>
26+
T1 & Pair<T1, T2>::first()
27+
{
28+
return a;
29+
}
30+
31+
32+
template<typename T1, typename T2>
33+
T2 & Pair<T1, T2>::second()
34+
{
35+
return b;
36+
}
37+
38+
39+
#endif

chapter14/assignment/14.7.2winec.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "14.7.2winec.h"
2+
#include <iostream>
3+
4+
Wine::Wine(const char * l, int y, const int yr[], const int bot[]): std::string(l)
5+
{
6+
years = y;
7+
PairArray::operator=(PairArray(ArrayInt(yr, y), ArrayInt(bot, y)));
8+
}
9+
10+
11+
Wine::Wine(const char * l, int y): std::string(l)
12+
{
13+
years = y;
14+
PairArray::operator=(PairArray(ArrayInt(y), ArrayInt(y)));
15+
}
16+
17+
18+
void Wine::GetBottles()
19+
{
20+
std::cout << "Enter " << (std::string &)*this << " data for " << years << " year(s):" << std::endl;
21+
int index = 0;
22+
int _yearArr[years];
23+
int _bottlesArr[years];
24+
25+
while(index < years)
26+
{
27+
std::cout << "Enter year: ";
28+
int _tempYr;
29+
std::cin >> _tempYr;
30+
_yearArr[index] = _tempYr;
31+
32+
std::cout << "Enter bottles for that year: ";
33+
int _tempBt;
34+
std::cin >> _tempBt;
35+
_bottlesArr[index] = _tempBt;
36+
index++;
37+
}
38+
39+
ArrayInt _years(_yearArr, years);
40+
ArrayInt _bottles(_bottlesArr, years);
41+
42+
// PairArray(_years, _bottles);
43+
PairArray::operator=(PairArray(_years, _bottles));
44+
45+
46+
}
47+
48+
49+
void Wine::Show()
50+
{
51+
std::cout << "Wine: " << (std::string &)*this << std::endl;
52+
std::cout.width(10);
53+
std::cout << "Year";
54+
std::cout.width(10);
55+
std::cout << "Bottles" << std::endl;
56+
for(int i = 0; i < years; i++)
57+
{
58+
std::cout.width(10);
59+
std::cout << PairArray::first()[i];
60+
std::cout.width(10);
61+
std::cout << PairArray::second()[i]<< std::endl;
62+
}
63+
}
64+
65+
std::string & Wine::Label()
66+
{
67+
return (std::string &)*this;
68+
}
69+
70+
71+
int Wine::sum()
72+
{
73+
int sum = 0;
74+
for(int i = 0; i < years; i++)
75+
{
76+
sum += PairArray::second()[i];
77+
}
78+
79+
return sum;
80+
}

chapter14/assignment/14.7.2winec.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef WINEC_H_
2+
#define WINEC_H_
3+
4+
#include "14.7.2pairs.h"
5+
6+
7+
#include <valarray>
8+
#include <string>
9+
10+
11+
typedef std::valarray<int> ArrayInt;
12+
typedef Pair<ArrayInt, ArrayInt> PairArray;
13+
14+
15+
class Wine: private std::string, private PairArray
16+
{
17+
private:
18+
int years;
19+
public:
20+
Wine(const char * l, int y, const int yr[], const int bot[]);
21+
Wine(const char * l, int y);
22+
void GetBottles();
23+
void Show();
24+
std::string & Label();
25+
int sum();
26+
};
27+
28+
#endif

0 commit comments

Comments
 (0)