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. Exec sql/c

    Exec sql/c 利用高级语言的过程性结构来弥补SQL语言实现复杂应用方面的不足. 嵌入SQL的高级语言称为主语言或宿主语言. 在混合编程中,SQL语句负责操作数据库,高级语言语句负责控制程序流程 ...

  2. iso-开发基础知识-1-程序流程

    main-应用程序委托-视图控制器 main()---主函数 应用程序委托  ---AppDelegate     视图控制器 ---ViewController - (BOOL)applicatio ...

  3. SDOTOJ2088 refresh的停车场(栈和队列)

     refresh的停车场 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit S ...

  4. 【图像配准】基于灰度的模板匹配算法(一):MAD、SAD、SSD、MSD、NCC、SSDA、SATD算法

    简介: 本文主要介绍几种基于灰度的图像匹配算法:平均绝对差算法(MAD).绝对误差和算法(SAD).误差平方和算法(SSD).平均误差平方和算法(MSD).归一化积相关算法(NCC).序贯相似性检测算 ...

  5. 想买一套arduino玩玩

    对于物联网,去年才接触,认识但不熟悉.做过一个项目,买了一块树莓派,看了一段网络电视. 最近被邀分享一些关于物联网的东西,翻了些大学公开课,很系统但太偏理论和框架.听了一个TED感觉很不错,但太精简. ...

  6. iOS 模态视图转场的动画效果

    ModalViewController * modalView = [[ModalViewController alloc]init]; modalView.modalTransitionStyle ...

  7. Scala io操作

    1. 读文件 scala特有的是scala.io.Source,例如: import scala.io._ Source.fromFile(“cn.scala”,”utf8”).mkString 逐行 ...

  8. Cglib学习站点(转)

    1.CGlib简单介绍,访问地址:http://www.blogjava.net/stone2083/archive/2008/03/16 /186615.html,从简单的示例到不同业务场景的变化, ...

  9. day7_python学习笔记_chapter9_文件

    1. open(), file(), 作用完全相同 2. 语法: file_object = open(file_name, access_mode='r', buffering='-1') acce ...

  10. Centos6.4 搭建Git服务器 (最简单的方法)

    下载 git-1.8.2.tar.gz tar -zvxf git-1.8.2.tar.gz cd git-1.8.2.2 sudo make prefix=/usr/local/git all su ...