C++ Primer Plus 第十章 课后编程练习题1-8

//1
//类声明文件.h
#ifndef bank_hpp
#define bank_hpp
#include
class BankAccount
{
private:
std::string fullname;
std::string account;
double deposit;
public:
//默认构造函数
BankAccount();
//录入函数
BankAccount(std::string & fname, std::string & acc,
double deposit = 0.0);
//存入
bool dps(double dps);
//取出
bool take(double dps);
//查看
void show() const;
};
#endif /* bank_hpp */
//接口文件(类共用成员函数定义)
#include
#include
#include “bank.hpp”
//默认构造
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 <<“请输入全名 :\n”;
getline(cin, fname);
cout <<“请输入账户 :\n”;
getline(cin, acc);
bank_acc1 = BankAccount(fname, acc, pd);
cout <<“请选择你的操作: \n”;
cout <<“1) 显示信息。 2)存钱。\n”;
cout <<“3) 取钱。 4) 退出。\n”;
while (cin >> ch && ch != ‘4’)
{
switch(ch)
{
case ‘1’: bank_acc1.show();
break;
case ‘2’: cout <<"请输入要存入多少钱: ";
cin >> pd;
if (!bank_acc1.dps(pd))
cout <<“存钱错误!\n”;
break;
case ‘3’: cout <<"请输入要取的钱数: ";
cin >> pd;
if (!bank_acc1.take(pd))
cout <<“取钱错误!\n”;
break;
}
cout <<“请选择你的操作: \n”;
cout <<“1) 显示信息。 2)存钱。\n”;
cout <<“3) 取钱。 4) 退出。\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

//接口公用函数定义
#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题
//类定义
#ifndef bank_hpp_
#define bank_hpp_

class Golf {
private:
static const int Len = 40;
char fullname[Len];
int handicap;
public:
//默认构造函数
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 */

//类函数定义
#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;

}
//修改差点
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题
//类声明
#ifndef stack_hpp
#define stack_hpp

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

class Stack
{
private:
enum { Max = 10 }; //栈空间大小10 * customer
Item items_[Max];
int top;
double total_ ;
public:
//默认结构
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 */

/类函数定义
#include
#include “stack.hpp”

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

//是否空栈
bool Stack::is_empty() const
{
return top == 0;
}

//栈是否已满
bool Stack::is_full() const
{
return top == Max;
}

//如果堆已满 返回false 否则 true
bool Stack::push(const Item & item)
{
if (top < Max)
{
items_[top++] = item;
return true;
}
else
return false;
}

//如果堆栈已经为空,pop()返回false,否则返回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题
//类定义
#ifndef move_hpp
#define move_hpp

class Move
{
private:
double x;
double y;
public:
//构造函数
Move(double a = 0, double b = 0);
//显示信息
void showmove() const;
//俩个类对象相加返回一个类对象
Move add(const Move & m)const;
//重置
void reset(double a, double b);
};

#endif /* move_hpp */

//类函数定义
#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题
//类定义
#ifndef plorg_hpp
#define plorg_hpp

class Plorg
{
private:
char name[19] = “Plorg”;
int index;
public:
//默认构造函数
Plorg();
//构造函数
Plorg(char *pl, int in = 50);
//修改CI指数
void Index(int in);
//查看信息
void Showplorg();
};

#endif /* plorg_hpp */

//类函数定义
#include
#include “plorg.hpp”

//默认构造函数
Plorg::Plorg()
{
index = 50;
}
//构造函数
Plorg::Plorg(char *pl, int in)
{
for (int i = 0; i < 19; i++)
name[i] = pl[i];
index = in;
}
//修改CI指数
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题
//类定义 和普通函数声明
#ifndef list_hpp
#define list_hpp

#include
typedef int Item;
class List_
{
private:
Item * items_; //记录数组或链表首地址,用于数组索引和链表节点指针。
int top_; //列表存储数据情况
int max_; //列表最大存储量。
public:
//默认构造函数
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));

};
//读取数组元素或链表成员内容
void show(Item & num);

//修改数组元素或链表成员内容
void modify(Item & item, int n = 1);

#endif /* list_hpp */

//类函数定义, 和普通函数定义
#include
#include “list.hpp”
//数组,链表 一次性分配够需要的动态存储空间
void List_::Lists(const int n)
{
items_ = new Item[n];
max_ = n;
}

//如果为空返回true 否则false
bool List_::is_empty()const
{
return top_ == 0;
}

//如果已满返回true 否则false
bool List_::is_full()const
{
return top_ == max_;
}

//录入列表数据项
bool List_::push_(const Item & item)
{
if (top_ < max_)
{
*(items_ + top_++) = item;
return true;
}
else
std::cout <<" Is full!\n";
return false;
}

//查看列表内的数据
void List_::visit_(void (pf) (Item & item))
{
for (int i = 0; i < top_; i++)
pf(
(items_ + i));
}
//修改列表内某一项的数据
void List_::modify_(void (*pf) (Item & item, int n))
{
int num;
std::cout <<“请输入要修改的项: (1 – " << top_ <<”)";
std::cin >> num;
std::cin.get();
pf(*items_ , num - 1);
}
///////////////////////////普通函数定义////////////////////
//读取数组元素或链表成员内容
void show(Item & item)
{
std::cout << item << std::endl;
}

//修改数组元素或链表成员内容
void modify(Item & item, int n)
{
std::cout << “请输入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;

}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/104202007
今日推荐