C++ Primer Plus(第六版)第十章课后习题

10.10.1

10.10.1.h
#ifndef d
#define d
#include

class bank
{
private:
std::string name_;
std::string acount_;
double balance_;
public:
bank(std::string & name,std::string &acount,double balance);
void deposite(double money);
void withdraw(double money);
void show();
};
#endif

10.10.1.cpp
#include
#include “10.10.1.h”
bank::bank(std::string & name,std::string &acount,double balance)
{
name_=name;
acount_=acount;
balance_=balance;
}
void bank::deposite(double money)
{
if(money<0)
std::cout << “Number of money that you deposite couldn’t be negative.\n”;
else
balance_+=money;
}
void bank::withdraw(double money)
{
if(money<0)
std::cout << “Number of money that you deposite couldn’t be negative.\n”;
else if(money>balance_)
std::cout << “The balance is not enough.\n”;
else
balance_-=money;
}
void bank::show()
{
std::cout << "Name: " << name_<< “\n”
<< "acount: " << acount_ << “\n”
<< "balance: " << balance_ << “\n\n”;
}

main.cpp
#include
#include
#include “10.10.1.h”
int main()
{
std::string b=“LinWang”,c=“188408”;
bank a(b,c,1120);
a.show();
a.deposite(600);
a.show();
a.withdraw(400);
a.show();
}

10.10.2

10.10.2.h
#include
class Person
{
private:
static const int LIMIT=25;
std::string lname;
char fname[LIMIT];
public:
Person() {lname="";fname[0]=’\0’; } //#1
Person(const std::string & ln,const char *fn=“Heyyou”); //#2
void Show() const; //firstname lastname format
void FormalShow() const; //lastname, firstname format
};

10.10.2.cpp
#include"10.10.2.h"
#include
#include
Person::Person(const std::string & ln,const char *fn)
{
lname=ln;
strcpy(fname,fn);
}
void Person::Show() const
{
std::cout << "Fullname is " << fname << " " <<lname << “\n\n”;
}
void Person::FormalShow() const
{
std::cout << "Fullname is " << lname << “,” <<fname << “\n\n”;
}

main.cpp
#include
#include"10.10.2.h"
int main()
{
Person one;
Person two=Person(“Smythecraft”);
Person three(“Dimwiddy”,“Sam”);
one.Show();
std::cout << std::endl;
one.FormalShow();
two.Show();
two.FormalShow();
three.Show();
three.FormalShow();
return 0;
}

10.10.3

10.10.3.1.h
const int Len=40;
class Golf
{
private:
char fullname[Len];
int handicap;
public:
Golf(const char *name,int hc);
Golf();
void Handicap(int hc);
void Showgolf() const;
};

10.10.3.2.cpp
#include
#include
#include “10.10.3.1.h”
Golf::Golf(const char *name,int hc)
{
strcpy(fullname,name);
handicap=hc;
}
Golf::Golf()
{
strcpy(fullname,“0”);
handicap=0;
}

void Golf::Handicap(int hc)
{
handicap=hc;
}
void Golf::Showgolf() const
{
using std::cout;
cout << "User’s fullname is " << fullname << “\n”;
cout << "User’s handicap is " << handicap << “\n”;
}

main.cpp
#include
#include “10.10.3.1.h”
const int Arsize=3;
int main()
{
using std::cout;
using std::cin;
using std::endl;
Golf a[Arsize];
char b[Len];
int c;
for(int i=0;i<Arsize;i++)
{
cout << “Enter a user’s fullname:” << endl;
cin.getline(b,Len);
if(b[0]’\0’||b[0]’ ')
break;
cout << “Enter a number of handicap:” << endl;
cin >> c;
cin.get();
a[i]=Golf(b,c);
a[i].Showgolf();
}
cout << “Done!” << endl;
return 0;
}

10.10.4

10.10.4.h
namespace SALES
{
const int QUARTERS=4;
class Sales
{
private:
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales(double ar[],int n);
Sales();
void ShowSales() const;
};
}

10.10.4.cpp
#include
#include “10.10.4.h”
namespace SALES
{
Sales::Sales(double ar[],int n)
{
for(int i=0;i<n;i++)
sales[i]=ar[i];
max=sales[0];
min=sales[0];
double sum=0.0;
for(int i=0;i<n;i++)
{
max=max>sales[i]?max:sales[i];
min=min<=sales[i]?min:sales[i];
sum+=sales[i];
}
average=sum/n;
}
Sales::Sales()
{
for(int i=0;i<QUARTERS;i++)
sales[i]=0;
average=0;
max=0;
min=0;
}
void Sales::ShowSales() const
{
using std::cout;
using std::endl;
for(int i=0;i<QUARTERS;i++)
cout << sales[i]<<endl;
cout << "Max= " << max << endl;
cout << "Min= " << min << endl;
cout << "Average= " << average << endl;
}
}

main.cpp
#include
#include “10.10.4.h”
const int A=4;
int main()
{
using namespace SALES;
double arr[A]={45.6,456.7,44.6,123.9};
Sales a;
a.ShowSales();
Sales b(arr,A);
b.ShowSales();
return 0;
}

10.10.5

stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer
{
char fullname[35];
double payment;
};

typedef customer Item;

class Stack
{
private:
enum{MAX=10};
Item item[MAX];
int top;
public:
Stack();
bool isempty() const;
bool isfull() const;
bool push(const Item &t);
bool pop(Item &t);
};
#endif

stack.cpp
#include “stack.h”
Stack::Stack()
{
top=0;
}
bool Stack::isempty() const
{
return top0;
}
bool Stack::isfull() const
{
return topMAX;
}
bool Stack::push(const Item &t)
{
if(top<MAX)
{
item[top++]=t;
return true;
}
else
return false;
}
bool Stack::pop(Item &t)
{
if(top>0)
{
t=item[–top];
return true;
}
else
return false;
}

main.cpp
#include
#include “stack.h”
#include
int main()
{
using namespace std;
Stack st;
char ch;
customer ct;
double total=0;
cout << “Enter A to add a purchase order,” << endl;
cout << “Enter P to process a PO, or Q to quit.” << endl;
while(cin >> ch&& toupper(ch)!=‘Q’)
{
while(cin.get()!=’\n’)
continue;
if(!isalpha(ch))
{
cout << ‘\a’;
continue;
}
switch(ch)
{
case ‘A’:
case ‘a’:
if(st.isfull())
cout << “Stack already full\n”;
else
{
cout << "Enter custmer’s fullname: ";
cin.getline(ct.fullname, 35);
cout << "Enter payment: ";
cin >> ct.payment;
cin.get();
st.push(ct);
}
break;
case ‘P’:
case ‘p’:
if(st.isempty())
cout << “Stack already empty\n”;
else
{
st.pop(ct);
total+=ct.payment;
cout << "Total payment: " << total << endl;
}
break;
}
cout << “Enter A to add a purchase order,” << endl;
cout << “Enter P to process a PO, or Q to quit.” << endl;
}
cout << “Bye\n”;
return 0;
}

10.10.6

10.10.6.h
#include
class Move
{
private:
double x;
double y;
public:
Move(double a=0,double b=0);
void ShowMove() const;
Move add(const Move &m) ;
void reset(double a=0,double b=0);
};

10.10.6.cpp
#include"10.10.6.h"
#include
Move::Move(double a,double b)
{
x=a;
y=b;
}
void Move::ShowMove() const
{
using std::cout;
cout << "x= " << x << “\n”;
cout << "y= " << y << “\n”;
}
Move Move::add(const Move &m)
{
this->x+=m.x;
this->y+=m.y;
std::cout << “x+x=\n”;
std::cout << “y+y=\n”;
return *this;
}
void Move::reset(double a,double b)
{
std::cout << “reset\n”;
x=a;
y=b;
}

main.cpp
#include
#include “10.10.6.h”
int main()
{
Move a(45,60);
a.ShowMove();
Move b(78,69);
b.ShowMove();
a=a.add(b);
a.ShowMove();
b.ShowMove();
a.reset(1,1);
a.ShowMove();
b.ShowMove();
a=a.add(b);
a.ShowMove();
}

10.10.7

10.10.7.h
#ifndef D
#define D
class Plorg
{
private:
char name[19];
int CI;
public:
Plorg(const char ch[],int n);
Plorg();
void SetPlorg(int n);
void ShowPlorg();
};
#endif

10.10.7.cpp
#include
#include
#include"10.10.7.h"
Plorg::Plorg(const char ch[],int n)
{
strcpy(name,ch);
CI=n;
}

Plorg::Plorg()
{
strcpy(name,“Plorga”);
CI=0;
}
void Plorg::SetPlorg(int n)
{
CI=n;
}
void Plorg::ShowPlorg()
{
using std::cout;
cout << "Name: " << name << “\n”;
cout << "CI: " << CI << “\n”;
}

main.cpp
#include
#include"10.10.7.h"
int main()
{
Plorg a;
a.ShowPlorg();
a.SetPlorg(7);
a.ShowPlorg();
Plorg b(“Lin Wang”,50);
b.ShowPlorg();
}

10.10.8

list.h
#ifndef D
#define D
typedef int Item;
class List
{
private:
enum{MAX=10};
Item list[MAX];
int top;
public:
List();
bool isempty() const;
bool isfull() const;
bool push(const Item &n);
bool pop(Item &n);
void visit(void (*pf)(Item &));
void ShowList();
};
void x2 (Item & n);
#endif

list.cpp
#include"list.h"
#include
List::List()
{
top=0;
}
bool List::isempty() const
{
return top0;
}
bool List::isfull() const
{
return topMAX;
}
bool List::push(const Item &n)
{
if(top<MAX)
{
list[top++]=n;
return true;
}
else
return false;
}
bool List::pop(Item &n)
{
if(top > 0)
{
n=list[–top];
return true;
}
else
return false;
}

main.cpp
#include
#include
#include"list.h"
int main()
{
using namespace std;
List a;
a.push(10);
a.push(20);
a.push(30);
if(a.isfull())
cout << “Already full\n”;
if (a.isempty())
cout << “Already empty\n”;
int n;
a.ShowList();
cout << endl;
a.pop(n);
cout << “n=” << n << endl;
a.ShowList();
cout << endl;
a.visit(x2);
a.ShowList();
return 0;
}

C++ Primer Plus(第六版)第十章课后习题相关推荐

  1. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  2. C Primer Plus第六版(中文版)编程练习答案(完美修订版)汇总

    //本文是博主编写的C Primer Plus第六版(中文版)编程练习答案的所有链接; //使用超链接汇总于此,若是有用请点赞收藏并分享给他人; C Primer Plus 第六版(中文版)第二章(完 ...

  3. 深夜里学妹竟然问我会不会C?我直接把这篇文章甩她脸上(C Primer Plus 第六版基础整合)

    C Primer Plus 第六版 前言 第一章 初识C语言 一.C语言的起源 二.C语言的应用 三.C语言的特点 四.编译的过程 五.编码机制 1.简述 2.完成机制 六.在UNIX系统上使用C 七 ...

  4. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  5. C primer plus(第六版)第十一章源代码

    C primer plus(第六版)第十一章源代码 /* 11.1 */ #include<stdio.h> #define MSG "I am a symbolic strin ...

  6. 2008版计算机基础,计算机应用基础2008版各章课后习题解析

    计算机应用基础2008版各章课后 习题解析 主编 汪燮华 张世正 一.单选题 1.一般认为,信息(information)是 A数据 B人们关心的事情的消息 C反映物质及其运动属性及特征的原始事实 D ...

  7. java语言程序设计郑莉课后答案_java语言程序设计 第2版 (郑莉)课后习题答案.doc...

    java语言程序设计 第2版 (郑莉)课后习题答案.doc JAVA语言程序设计第2版郑莉第二章习题答案1什么是对象.类,它们之间的联系答1)对象是包含现实世界物体特征的抽象实体,它反映系统为之保存信 ...

  8. 计算机网络(第三版)胡亮 课后习题一答案

    计算机网络(第三版)胡亮 课后习题一答案 1.计算机网络所涉及的两大主要技术是什么? 2.什么是计算机网络?计算机网络具有哪些功能? 3.举例说明计算机网络有哪些应用? 4.什么是通信子网?什么是资源 ...

  9. 新视野大学英语(第三版)第一册课后习题答案(完整版)

    想看更多算法题,可以扫描上方二维码关注我微信公众号"数据结构和算法",截止到目前我已经在公众号中更新了500多道算法题,其中部分已经整理成了pdf文档,截止到目前总共有900多页( ...

最新文章

  1. 我在 GitHub 上都见过哪些沙雕项目?
  2. 我的NHibernate之路(1)---基本配置篇
  3. 构建创业公司突击小团队
  4. NOI.AC#2144-子串【SAM,倍增】
  5. Java:JDK安装
  6. Codeforces Round #432 B
  7. Windows下载FFmpeg最新版(踩了一上午的坑终于成功)
  8. ubuntu 下安装 phpmyadmin 过程记录
  9. .php对收录有影响吗,你知道吗?网站收录其实对网站排名影响并不大
  10. 喜马拉雅音频解析插件
  11. SQL删除重复数据并只保留一条
  12. 【综合篇】Web前端性能优化原理问题
  13. 小米路由器3是基于linux,小米路由器3(MI-3)刷华硕固件不用虚拟机刷华硕固件无需虚拟机方法...
  14. 前端登陆界面 html+js
  15. 互联网大佬生存法则 如何防守周鸿祎
  16. Vue中video播放m3u8视频
  17. 简单的快递管理系统(c语言版)--大一下的总结
  18. 通过Fiddler实现部分静态资源代理
  19. 如何删除字符串中的数字
  20. SEO优化:Sitemap插件生成WordPress网站地图

热门文章

  1. (附源码)计算机毕业设计SSM家教管理系统
  2. Android 二进制、10进制、16进制互相转换
  3. Java斗地主游戏部分实现
  4. GitHub安装包下载
  5. 网页制作APP和APP定制开发有哪些区别
  6. 基于Python tensorflow2.3实现的水果识别系统源码+模型+数据集,卷积神经网络的入门案例
  7. JumpServer RCE复现
  8. 用伪元素画横线,并使用一个盒子覆盖一部分
  9. Error Code: 1318. Incorrect number of arguments for PROCEDURE account_check.temp_i; expected 1, got
  10. 只有潮水退去后,才知道谁在裸泳