Skip to content

Commit 53b95e1

Browse files
committed
update chapter10 codes
update chapter11 codes
1 parent 77c6cc1 commit 53b95e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1456
-3
lines changed

.DS_Store

4 KB
Binary file not shown.

chapter09/.DS_Store

0 Bytes
Binary file not shown.

chapter09/assignment/.DS_Store

6 KB
Binary file not shown.

chapter10/.DS_Store

6 KB
Binary file not shown.

chapter10/assignment/.DS_Store

8 KB
Binary file not shown.
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "10.10.2assign.h"
2+
#include <cstring>
3+
#include <iostream>
4+
5+
6+
Person::Person()
7+
{ lname = "";
8+
fname[0] = '\0';
9+
}
10+
11+
12+
Person::Person(const std::string& ls, const char * fn)
13+
{
14+
15+
lname = ls;
16+
strcpy(fname, fn);
17+
18+
}
19+
20+
21+
void Person::Show() const
22+
{
23+
std::cout << lname << ": " << fname << std::endl;
24+
}
25+
26+
27+
void Person::FormalShow() const
28+
{
29+
std::cout << fname << ": " << lname << std::endl;
30+
}

chapter10/assignment/10.10.2assign.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef PERSON_H
2+
#define PERSON_H
3+
#include <string>
4+
#include <iostream>
5+
6+
7+
class Person
8+
{
9+
private:
10+
static const int LIMIT = 25;
11+
std::string lname;
12+
char fname[LIMIT];
13+
14+
public:
15+
Person();
16+
Person(const std::string& ln, const char* fn = "Heyyou");
17+
void Show() const;
18+
void FormalShow() const;
19+
};
20+
21+
22+
#endif

chapter10/assignment/10.10.2main.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "10.10.2assign.h"
2+
3+
4+
int main()
5+
{
6+
Person one;
7+
Person two("Smythecraft");
8+
Person three("Dimwiddy", "Sam");
9+
10+
one.Show();
11+
std::cout << std::endl;
12+
13+
one.FormalShow();
14+
15+
two.Show();
16+
two.FormalShow();
17+
18+
three.Show();
19+
three.FormalShow();
20+
21+
return 0;
22+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "10.10.3assign.h"
2+
#include <iostream>
3+
#include <cstring>
4+
5+
6+
Golf::Golf()
7+
{
8+
fullname[0] = '\0';
9+
handicap = 0;
10+
}
11+
12+
13+
Golf::Golf(const char * name, int hc)
14+
{
15+
strcpy(fullname, name);
16+
handicap = hc;
17+
}
18+
19+
void Golf::Setgolf()
20+
{
21+
char tempName[40];
22+
int tempInt;
23+
std::cout << "Enter golf player's name: ";
24+
std::cin.getline(tempName, 40);
25+
std::cout << "Enter golf player's handicap: ";
26+
std::cin >> tempInt;
27+
28+
if (*tempName != '\0')
29+
{
30+
Golf temp(tempName, tempInt);
31+
*this = temp;
32+
}
33+
else
34+
{
35+
Golf temp1;
36+
*this = temp1;
37+
}
38+
}
39+
40+
41+
void Golf::Handicap(int hc)
42+
{
43+
handicap = hc;
44+
}
45+
46+
47+
void Golf::Showgolf()
48+
{
49+
std::cout << fullname << ", " << handicap << std::endl;
50+
51+
}

chapter10/assignment/10.10.3assign.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef GOLF_H_
2+
#define GOLF_H_
3+
4+
5+
class Golf
6+
{
7+
8+
private:
9+
char fullname[40];
10+
int handicap;
11+
public:
12+
Golf();
13+
Golf(const char * name, int hc);
14+
void Setgolf();
15+
void Handicap(int hc);
16+
void Showgolf();
17+
18+
};
19+
20+
21+
#endif
22+

chapter10/assignment/10.10.3main.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "10.10.3assign.h"
2+
3+
4+
int main()
5+
{
6+
7+
Golf golfa("Chuckbruno", 9);
8+
golfa.Showgolf();
9+
10+
golfa.Handicap(54);
11+
golfa.Showgolf();
12+
13+
golfa.Setgolf();
14+
golfa.Showgolf();
15+
16+
17+
return 0;
18+
19+
20+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "10.10.4assign.h"
2+
#include <iostream>
3+
4+
namespace SALES
5+
{
6+
Sales::Sales()
7+
{
8+
average = 0;
9+
max = 0;
10+
min = 0;
11+
}
12+
13+
Sales::Sales(double arr[], int n)
14+
{
15+
double total = 0;
16+
double tmin = arr[0];
17+
double tmax = arr[0];
18+
for (int i = 0; i < n; i++)
19+
{
20+
sales[i] = arr[i];
21+
total += arr[i];
22+
if (arr[i] > tmax)
23+
tmax = arr[i];
24+
25+
if (arr[i] < tmin)
26+
tmin = arr[i];
27+
28+
}
29+
30+
average = total / n;
31+
max = tmax;
32+
min = tmin;
33+
}
34+
35+
void Sales::ShowSales()
36+
{
37+
std::cout << sales[0] << ", " << sales[1] << ", " << sales[2] << ", " << sales[3] <<std::endl;
38+
std::cout << average << std::endl;
39+
std::cout << max << std::endl;
40+
std::cout << min << std::endl;
41+
}
42+
43+
}

chapter10/assignment/10.10.4assign.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace SALES
2+
{
3+
const int QUARTERS = 4;
4+
class Sales
5+
{
6+
private:
7+
8+
double sales[QUARTERS];
9+
double average;
10+
double max;
11+
double min;
12+
void setAverage();
13+
void setMaxMin();
14+
public:
15+
Sales();
16+
Sales(double [], int n);
17+
void ShowSales();
18+
19+
};
20+
21+
}

chapter10/assignment/10.10.4main.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include "10.10.4assign.h"
3+
4+
using namespace SALES;
5+
6+
7+
int main()
8+
{
9+
double ar[4] = {23.45, 56.23, 67.45, 98.45};
10+
Sales sales1(ar, 4);
11+
12+
sales1.ShowSales();
13+
14+
return 0;
15+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "10.10.5assign.h"
2+
#include <iostream>
3+
4+
Stack::Stack()
5+
{
6+
top = 0;
7+
}
8+
9+
10+
bool Stack::isempty() const
11+
{
12+
return top == 0;
13+
}
14+
15+
16+
bool Stack::isfull() const
17+
{
18+
return top == MAX;
19+
}
20+
21+
22+
bool Stack::push(const Item & item)
23+
{
24+
if (top < MAX)
25+
{
26+
items[top++] = item;
27+
return true;
28+
}
29+
else
30+
return false;
31+
}
32+
33+
34+
bool Stack::pop()
35+
{
36+
static double totalPayment = 0;
37+
38+
if (top > 0)
39+
{
40+
std::cout << top-- << std::endl;
41+
totalPayment += items[top].payment;
42+
43+
std::cout << "Total payment is: " << totalPayment << std::endl;
44+
return true;
45+
}
46+
else
47+
return false;
48+
}
49+
50+
51+
52+

chapter10/assignment/10.10.5assign.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef STACK_H
2+
#define STACK_H
3+
4+
5+
struct customer
6+
{
7+
char fullname[35];
8+
double payment;
9+
};
10+
11+
typedef customer Item;
12+
13+
class Stack
14+
{
15+
private:
16+
static double totalPayment;
17+
enum {MAX = 10};
18+
Item items[MAX];
19+
int top;
20+
public:
21+
Stack();
22+
bool isempty() const;
23+
bool isfull() const;
24+
bool push(const Item& item);
25+
bool pop();
26+
};
27+
28+
#endif

chapter10/assignment/10.10.5main.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "10.10.5assign.h"
2+
3+
4+
int main()
5+
{
6+
Stack st;
7+
8+
customer a = {"google", 120};
9+
customer b = {"jiao", 7896};
10+
customer c = {"baidu", 8907};
11+
12+
st.push(a);
13+
st.push(b);
14+
st.push(c);
15+
16+
st.pop();
17+
st.pop();
18+
st.pop();
19+
20+
return 0;
21+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "10.10.7assign.h"
2+
#include <iostream>
3+
#include <string>
4+
5+
6+
Plorg::Plorg(const char * nptr, int rate)
7+
{
8+
strcpy(m_name, nptr);
9+
m_rate = rate;
10+
}
11+
12+
13+
void Plorg::setRate(int rate)
14+
{
15+
m_rate = rate;
16+
}
17+
18+
19+
void Plorg::showPlorg() const
20+
{
21+
std::cout << "name: " << m_name << "; rate: " << m_rate << std::endl;
22+
}

0 commit comments

Comments
 (0)