p333.3

 #include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
using namespace std; class Golf{
private:
static const int Len = ;
char fullname[Len];
int handicap;
public:
Golf(char name[Len], int bt);
void showgolf() const;
}; void setgolf(char *name, int extent, int &bt){
cout << "enter a name\n";
cin.get(name, extent);
while (cin.get() != '\n')
continue;
cout << "enter a number\n";
cin >> bt;
} Golf::Golf(char name[Len], int bt){
strncpy(fullname, name, Len);
handicap = bt;
} void Golf::showgolf()const{
cout << "fullname is " << fullname << endl
<< "handicap is " << handicap;
} int main(){
Golf one = Golf("asxc", );
int ct;
char name[];
setgolf(name, , ct);
Golf two = Golf(name, ct);
one.showgolf();
cout << endl;
two.showgolf(); system("pause");
return ;
}

p333.4

 //头文件
namespace SALES
{
class Sales{
private:
enum {QUARTERS=};
double sales[QUARTERS];
double average, max, min;
public:
Sales();
Sales(const double *ar, int n);
void showsales();
};
} //方法
#include<iostream>
#include"sales.h"
using namespace std; SALES::Sales::Sales(){
cout << "enter four number\n";
double sum = ;
for (int i = ; i < QUARTERS; i++){
cin >> sales[i];
sum += sales[i];
}
average = sum / QUARTERS;
max = sales[];
min = sales[];
for (int i = ; i < QUARTERS; i++){
if (max < sales[i])
max = sales[i];
if (min>sales[i])
min = sales[i];
}
} SALES::Sales::Sales(const double *ar, int n){
double sum = ;
for (int i = ; i < n; i++){
sales[i] = ar[i];
sum += sales[i];
}
average = sum / QUARTERS;
max = sales[];
min = sales[];
for (int i = ; i < QUARTERS; i++){
if (max < sales[i])
max = sales[i];
if (min>sales[i])
min = sales[i];
}
} void SALES::Sales::showsales(){
cout << "average is " << average << endl
<< "max is " << max << endl
<< "min is " << min;
} //驱动
#include<iostream>
#include"sales.h"
using namespace std; int main(){
using namespace SALES;
cout << "enter four number\n";
double ar[];
for (int i = ; i < ; i++)
cin >> ar[i];
Sales market1(ar, );
market1.showsales();
Sales market2;
cout << endl;
market2.showsales(); system("pause");
return ;
}

p333.5

 //头文件:
#ifndef _STACK_H_
#define _STACK_H_ struct customer{
char fullname[];
double payment;
}; typedef customer Item; class Stack{
private:
enum{max=};
Item items[max];
int top;
public:
Stack();
bool isfull()const;
bool isempty()const;
void push(const Item &);
bool pop(const Item &);
}; #endif //方法
#include<iostream>
#include"stack.h"
using namespace std; Stack::Stack(){
top = ;
} bool Stack::isfull()const{
if (top == max)
return true;
else return false;
} bool Stack::isempty()const{
if (top == )
return true;
else return false;
} void Stack::push(const Item &temp){
if (isfull())
cout << "the stack has already full\n";
else items[top++] = temp;
} bool Stack::pop(const Item &temp){
if (isempty()){
cout << "the stack has already empty\n";
return false;
}
else {
items[--top] = temp;
return true;
}
} //驱动:
#include<iostream>
#include"stack.h"
using namespace std;
void get_customer(Item &); int main(){
Item temp;
Stack test;
double payments=;
cout << "enter A to push, enter P to pop or"
<<"enter Q to quit\n";
char ch;
while (cin >> ch && (ch != 'Q' && ch != 'q')){
while (cin.get() != '\n')
continue;
switch (ch){
case 'A':
case 'a':
get_customer(temp);
test.push(temp);
break;
case 'P':
case 'p':
if (test.pop(temp))
payments += temp.payment;
cout << "now total payments is " <<
payments << endl;
default:
cout << "please enter A/P or Q\n";
}
cout << "enter A to push, enter P to pop or"
<< "enter Q to quit\n";
}
system("pause");
return ;
} void get_customer(Item &temp){
cout << "enter a line\n";
cin.getline(temp.fullname, );
cout << "enter a number\n";
cin >> temp.payment;
cin.get();
}

c++ primer plus 习题答案(4)的更多相关文章

  1. c++ primer plus 习题答案(1)

    c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...

  2. c++ primer plus 习题答案(8)

    p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...

  3. c++ primer plus 习题答案(7)

    p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...

  4. c++ primer plus 习题答案(6)

    p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  5. c++ primer plus 习题答案(5)

    p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  6. c++ primer plus 习题答案(3)

    p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...

  7. c++ primer plus 习题答案(2)

    p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...

  8. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  9. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

随机推荐

  1. js类方法,对象方法,原型的理解(转)

    function People(name) { this.name=name; //对象方法 this.Introduce=function(){ alert("My name is &qu ...

  2. codeforces #286 Div.2 C DP总是以意外的方式打败我

    题目大意:30001个岛排成一排,编号从0到30000,一共有n个宝物分散在这些岛上,一只猪最开始从0跳到d,之后每一步跳的步长和上一步相差不超过1,第二步步长就是d-1,d,d+1,第二步的位置就是 ...

  3. _.remove的用法

    var array = [1, 2, 3, 4]; var evens = _.remove(array, function(n) { return n % 2 == 0; }); console.l ...

  4. 回收进程用户空间资源 exit()函数 _exit()函数 atexit()函数 on_exit()函数

    摘要:本文主要讲述进程的终止方式,以及怎样使用exit()函数来终止进程.回收进程用户空间资源:分析了exit()函数与_exit()函数,returnkeyword的差异.同一时候具体解读了怎样使用 ...

  5. 关于在App_Code文件夹自定义类中Session无法使用

    由于前台页面需要调用App_Code中自定义类的函数,但在自定义类中找不到Session,解决方法如下: 新建一个类session,并自己定义函数GetSession(),引用命名空间 System. ...

  6. Jsp连接Mysql数据库取数方法

    我将Jsp连接Mysql数据库方法整理如下,供大家学习交流! 1.首先在myslq数据库中新建mldn数据库,并新建emp表.(方法不展开介绍) 插入数据如下: create table `emp` ...

  7. MQ学习(一)----JMS规范(转发整合)

    最近进行ActiveMQ的学习,总结下已被不时之需. JMS规范: JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的A ...

  8. TCP/IP的网际层协议——ARP

    MAC地址对于每一台设备是全球唯一的,该地址被烧录在网卡的硬件电路上.MAC地址由12位十六进制数表示,其中前6位标识网卡的制造厂商,后6位是网卡的序列号.在以太网中,一个主机要和另一个主机进行通信, ...

  9. hadoop笔记之hdfs

    1.HDFS设计基础与目标 1.HDFS设计基础与目标 (1)硬件错误是常态,因此需要冗余. (2)流式数据访问.即数据批量读取而非随机读写,Hadoop擅长做的是数据分析而不是事务处理. (3)大规 ...

  10. R与数据分析旧笔记(十五) 基于有代表性的点的技术:K中心聚类法

    基于有代表性的点的技术:K中心聚类法 基于有代表性的点的技术:K中心聚类法 算法步骤 随机选择k个点作为"中心点" 计算剩余的点到这个k中心点的距离,每个点被分配到最近的中心点组成 ...