C ++ Primer Plus Chapter 10 After-Class Programming Exercises 1-8

// 1
// Class declaration
file.h #ifndef bank_hpp
#define bank_hpp
#include
class BankAccount
{
private:
std :: string fullname;
std :: string account;
double deposit;
public:
// default constructor
BankAccount ();
/ / Input function
BankAccount (std :: string & fname, std :: string & acc,
double deposit = 0.0);
// Deposit into
bool dps (double dps);
// Remove
bool take (double dps);
// View
void show () const;
};
#endif / * bank_hpp * /
// Interface file (class common member function definition)
#include
#include
#include "bank.hpp"
// Default construction
BankAccount :: BankAccount ()
{
deposit = 0.0;
}

//录入信息
BankAccount::BankAccount(std::string & fname, std::string & acc,
double dp)
{
fullname = fname;
account = acc;
deposit = dp;
}

//存入
bool BankAccount::dps(double dp)
{
if (deposit < dp + deposit)
{
deposit += dp;
return true;
}
else
return false;
}

//取出
bool BankAccount::take(double dp)
{
if (deposit > deposit - dp)
{
deposit -= dp;
return true;
}
else
return false;
}

//显示
void BankAccount::show() const
{
using std::cout;
cout <<fullname <<’ ’ << account << ’ ’ << "$ " << deposit << ‘\n’;
}

//main.cpp
#include
#include
#include "bank.hpp"
int main ()
{
using namespace std;
char ch;
double pd = 0.0;
BankAccount bank_acc1;
string fname, acc;
cout << "Please enter the full name: \ n ”;
getline (cin, fname);
cout <<“ Please enter account: \ n ”;
getline (cin, acc);
bank_acc1 = BankAccount (fname, acc, pd);
cout <<“ Please select your operation : \ n ”;
cout <<“ 1) Display information. 2) Save money. \ n ”;
cout <<“ 3) Withdraw money. 4) Exit. \ n ”;
while (cin >> ch && ch! = '4')
{
switch (ch)
{
case '1': bank_acc1.show ();
break;
case '2': cout << "Please enter how much money to deposit:";
cin >> pd;
if (!bank_acc1.dps(pd))
cout << "Error saving money! \ n";
break;
case '3': cout << "Please enter the amount of money you want to withdraw:";
cin >> pd;
if (! bank_acc1.take (pd))
cout < <"Money withdrawal error! \ N";
break;
}
cout << "Please choose your operation: \ n";
cout << "1) Display information. 2) Save money. \ N";
cout << "3 ) Withdraw money. 4) Exit. \ N ";
}
cout <<" Bye! \ N ";

return 0;

}

//2题
//类定义
#ifndef name_hpp_
#define name_hpp_
#include
using namespace std;
// class definition
class Person
{
private:
static const int LIMIT = 25;
string lname; //Person’s last name
char fname[LIMIT]; //Person’s first name
public:
Person() { lname = “”; fname[0] = ‘\0’; } //#1
Person(const string & ln, const char *fn = “Heyyou”); //#2
//the following methods display lname and fname
void Show() const; //firstname lastname format
void FormalShow() const; //lastname,firstname format
};

#endif

// Interface common function definition
#include
#include
#include
#include "name.hpp"

Person::Person(const string & ln,const char *fn)
{
lname = ln;
strcpy(fname, fn);
}

void Person::Show() const
{
std::cout << “Full Name:” << std::endl;
std::cout << fname << " " << lname << std::endl;
}

void Person::FormalShow() const
{
std::cout << “Formal Name:” << std::endl;
std::cout << lname << " " << fname << std::endl;
}

//mian.cpp
#include
#include “name.hpp”
int main()
{
using namespace std;
Person one;
Person two(“Smythecraft”);
Person three(“Dimwiddy”, “Sam”);
one.Show();
one.FormalShow();
two.Show();
two.FormalShow();
three.Show();
three.FormalShow();

return 0;

}

// 3 questions
// class definition
#ifndef bank_hpp_
#define bank_hpp_

class Golf {
private:
static const int Len = 40;
char fullname [Len];
int handicap;
public:
// default constructor
Golf () {fullname [0] = '\ 0'; handicap = 0;}

//构造函数
Golf(const char * ln, const int hd = 0);

//互交版录入函数
const Golf & SetGolf();

//查看信息
void Show();

//更改差点
void HandCap(const int hc);

};

#endif /* bank_hpp */

// Class function definition
#include
#include "bank.hpp"

//构造函数
Golf::Golf(const char * name, int hc)
{
for (int i = 0; i < Len; i++)
fullname[i] = name[i];
handicap = hc;
}

//互交录入函数
const Golf & Golf::SetGolf()
{
Golf g;
std::cout <<“Please enter the full name of the contestant: (Direct return exit)\n”;
if(std::cin.getline(g.fullname, Len) && g.fullname[0] != ‘\0’)
{
std::cout <<"Please enter " << g.fullname <<“golf handicap:\n”;
std::cin >> g.handicap;
std::cin.get();
}

return *this = g;

}
// Modify handicap
void Golf :: HandCap (const int hc)
{
handicap = hc;
}

//显示信息
void Golf::Show()
{
std::cout <<fullname <<" handicap = " << handicap << std::endl;
}

//main.cpp
#include
#include “bank.hpp”

int main()
{
using namespace std;
Golf golf1;
Golf golf2 = {“AAAAAA” , 10 };
golf1.SetGolf();
golf1.Show();
golf2.Show();
golf1.HandCap(13);
golf1.Show();

return 0;

}

// 5 questions
// class declaration
#ifndef stack_hpp
#define stack_hpp

typedef struct customer {
char fullname[35];
double payment;
} Item;

class Stack
{
private:
enum {Max = 10}; // Stack space size 10 * customer
Item items_ [Max];
int top;
double total_;
public:
// default structure
Stack ();

//是否空栈
bool is_empty() const;

//栈是否已满
bool is_full() const;

//如果堆已满 返回false 否则 true
bool push(const Item & item);

//如果堆栈已经为空,pop()返回false,否则返回true
bool pop(Item & item);

//显示交易信息
void Show();

};

#endif /* stack_hpp */

/ Class function definition
#include
#include "stack.hpp"

Stack::Stack()
{
top = 0;
total_ = 0.0;
}

// Whether the empty stack
bool Stack :: is_empty () const
{
return top == 0;
}

// Whether the stack is full
bool Stack :: is_full () const
{
return top == Max;
}

// If the heap is full return false otherwise
bool Stack :: push (const Item & item)
{
if (top <Max)
{
items_ [top ++] = item;
return true;
}
else
return false;
}

// If the stack is already empty, pop () returns false, otherwise it returns true
bool Stack :: pop (Item & item)
{
if (top> 0)
{
item = items _ [– top];
return true;
}
else
return false ;
}

//显示信息
void Stack::Show()
{
total_ += items_[top].payment;
std::cout <<“total = " << total_ << std::endl;
std::cout << “PO #” << items_[top].fullname
<<” " << items_[top].payment << std::endl;
}

//mian()
#include
#include
#include “stack.hpp”

int main()
{
using namespace std;
Stack st;
customer item;
char ch;
cout <<“Please enter A to add a purchase order,\n”
<<“P to process a PO, or Q to quit.\n”;
while (cin >> ch && toupper(ch) != ‘Q’)
{
while (cin.get() != ‘\n’)
continue;
if (!isalpha(ch))
{
cout << “Please enter A to add a purchase order,\n”
<<“P to process a PO, or Q to quit.\n”;
continue;
}
switch(ch)
{
case ‘A’:
case ‘a’: cout <<"Please enter fullname: ";
cin.getline(item.fullname, 35);
cout <<"Please enter payment amount: ";
cin >> item.payment;
cin.get();
if (st.is_full())
cout <<“Stack already full\n”;
else
st.push(item);
break;
case ‘P’:
case ‘p’: if (st.is_empty())
cout <<“Stack already empty\n”;
else
{
st.pop(item);
st.Show(); }
break;
}
cout << “Please enter A to add a purchase order,\n”
<<“P to process a PO, or Q to quit.\n”;
}
cout <<“Bye\n”;
return 0;
}

// 6 questions
// class definition
#ifndef move_hpp
#define move_hpp

class Move
{
private:
double x;
double y;
public:
// Constructor
Move (double a = 0, double b = 0);
// Display information
void showmove () const;
// Add two class objects and return one Class object
Move add (const Move & m) const;
// Reset
void reset (double a, double b);
};

#endif /* move_hpp */

// Class function definition
#include
#include "move.hpp"

Move::Move(double a, double b)
{
x = a;
y = b;
}
//显示信息
void Move::showmove() const
{
std::cout <<"X = " << x << std::endl;
std::cout <<"Y = " << y << std::endl;
}
//this 指针
Move Move::add(const Move & m)const
{
return Move(x + m.x, y + m.y);
}
//重置
void Move::reset(double a, double b)
{
x = a;
y = b;
}

//main()
#include
#include “move.hpp”
int main()
{
Move mo1(1, 2);
Move mo2(3, 4);
Move mo3(5, 6);
mo1.showmove();
mo1.add(mo2).showmove();
mo3.reset(4.5, 6.5);
mo3.showmove();

return 0;

}

// 7 questions
// class definition
#ifndef plorg_hpp
#define plorg_hpp

class Plorg
{
private:
char name [19] = "Plorg";
int index;
public:
// The default constructor
Plorg ();
// The constructor
Plorg (char * pl, int in = 50);
// Modify the CI index
void Index (int in);
// View information
void Showplorg ();
};

#endif /* plorg_hpp */

// Class function definition
#include
#include "plorg.hpp"

// default constructor
Plorg :: Plorg ()
{
index = 50;
}
// constructor
Plorg :: Plorg (char * pl, int in)
{
for (int i = 0; i <19; i ++)
name (i ] = pl [i];
index = in;
}
// Modify the CI index
void Plorg :: Index (int in)
{
index = in;
}

//查看信息
void Plorg::Showplorg()
{
std::cout <<"Plorg name = " << name << std::endl;
std::cout <<"Plorg CI = " << index << std::endl;
}

//main()
#include
#include “plorg.hpp”
int main()
{
Plorg pl1;
Plorg pl2(“WWWW”);
pl1.Showplorg();
pl2.Showplorg();
pl1.Index(80);
pl1.Showplorg();
return 0;
}

// 8 questions
// Class definition and ordinary function declaration
#ifndef list_hpp
#define list_hpp

#include
typedef int Item;
class List_
{
private:
Item * items_; // Record the first address of the array or linked list, used for array index and linked list node pointer.
int top_; // List storage data int max_; //
Maximum storage of list.
public:
// default constructor
List_ () {items_ = NULL; top_ = 0; max_ = 0;};

//数组,链表 一次性分配够需要的动态存储空间
void Lists(const int n);

//如果为空返回true 否则false
bool is_empty()const;

//如果已满返回true 否则false
bool is_full()const;

//录入列表数据项
bool push_(const Item & item);

//查看列表内的数据
void visit_(void (*pf) (Item &));

//修改列表内的数据
void modify_(void (*pf) (Item & , int));

};
// Read the contents of array elements or linked list members
void show (Item & num);

// Modify the contents of array elements or linked list members
void modify (Item & item, int n = 1);

#endif /* list_hpp */

// Class function definition, and ordinary function definition
#include
#include "list.hpp"
// Array, linked list allocate enough dynamic storage space at one time
void List _ :: Lists (const int n)
{
items_ = new Item [n ];
max_ = n;
}

// If it is empty return true otherwise false
bool List _ :: is_empty () const
{
return top_ == 0;
}

// return true if full, otherwise false
bool List _ :: is_full () const
{
return top_ == max_;
}

// Enter list data item
bool List _ :: push_ (const Item & item)
{
if (top_ <max_)
{
* (items_ + top _ ++) = item;
return true;
}
else
std :: cout << "Is full ! \ n ";
return false;
}

// View the data in the list
void List _ :: visit_ (void ( pf) (Item & item))
{
for (int i = 0; i <top_; i ++)
pf (
(items_ + i));
}
// modify The data of an item in the list
void List _ :: modify_ (void (* pf) (Item & item, int n))
{
int num;
std :: cout << “Please enter the item to be modified: (1 –" < <top_ << ”)";
std :: cin >> num;
std :: cin.get ();
pf (* items_, num-1);
}
////////////// ///////////// Ordinary function definition /////////////////////
// Read the contents of array elements or linked list members
void show (Item & item)
{
std :: cout << item << std :: endl;
}

// Modify the contents of array elements or linked list members
void modify (Item & item, int n)
{
std :: cout << "Please enter the value of item: \ n";
std :: cin >> item;
}

//main()函数
#include
#include “list.hpp”
int main()
{
List_ list;
Item item1 = 5;
Item item2 = 6;
Item item3 = 7;
Item item4 = 8;
Item item5 = 9;
list.Lists(4);
list.push_(item1);
list.push_(item2);
list.push_(item3);
list.push_(item4);
if (list.is_empty())
list.push_(item5);

if (list.is_full())
    list.visit_(show);
list.modify_(modify);
list.visit_(show);

return 0;

}

Published 85 original articles · Like1 · Visits1889

Guess you like

Origin blog.csdn.net/Tekkenwxp/article/details/104202007