p296.3

 #include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<new>
using namespace std;
struct chaff
{
char dross[];
int slag;
};
void setarray(chaff *pt);
void showarray(chaff *pt); int main(){
char buffer[];
chaff ar[], *pt;
pt = new(buffer)chaff[];
setarray(pt);
showarray(pt);
system("pause");
return ;
} void setarray(chaff *pt){
int i;
char adross[], pdross[], ch;
cout << "enter a line\n";
cin.get(adross, );
while (cin){
cin.get(ch);
while (ch != '\n')
cin.get(ch);
break;
}
strcpy(pt->dross, adross);
cout << "enter another line\n";
cin.get(pdross, );
while (cin){
cin.get(ch);
while (ch != '\n')
cin.get(ch);
break;
}
strcpy((pt + )->dross, pdross);
cout << "enter two integer\n";
cin >> pt->slag;
cin >> (pt + )->slag;
cin.get();
} void showarray(chaff *pt){
cout << pt->dross << " " <<
pt->slag << endl <<
(pt + )->dross << " " <<
(pt + )->slag << endl;
}

p296.4

 sales.cpp

 #include<iostream>
#include"sales.h"
using namespace std; void SALES::setsales(Sales &s, const double ar[], int n){
int num, i, sum=;
if (n <= QUARTERS)
num = n;
else num = QUARTERS;
for (i = ; i < num; i++){
s.sales[i] = ar[i];
sum += s.sales[i];
}
s.average = sum / num;
s.max = s.sales[];
s.min = s.sales[];
for (i = ; i < num; i++){
if (s.max < s.sales[i])
s.max = s.sales[i];
if (s.min>s.sales[i])
s.min = s.sales[i];
}
} void SALES::setsales(Sales &s){
cout << "enter double into an array\n";
int i;
double sum = ;
for (i = ; i < QUARTERS; i++){
cin >> s.sales[i];
sum += s.sales[i];
}
s.average = sum / QUARTERS;
s.max = s.sales[];
s.min = s.sales[];
for (i = ; i < QUARTERS; i++){
if (s.max < s.sales[i])
s.max = s.sales[i];
if (s.min>s.sales[i])
s.min = s.sales[i];
}
} void SALES::showsales(const Sales & s){
for (int i = ; i < QUARTERS; i++)
cout << s.sales[i] << " ";
cout << "average is " << s.average << endl <<
"max is " << s.max << endl <<
"min is " << s.min << endl;
} 驱动程序 #include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include"sales.h"
using namespace std; int main(){
cout << "enter a number you want to fill the array\n";
int n, i;
cin >> n;
double *ar = new double[n+];
cout << "enter an double array\n";
for (i = ; i < n; i++)
cin >> ar[i];
SALES::Sales ex1, ex2;
SALES::setsales(ex1, ar, n);
SALES::setsales(ex2);
SALES::showsales(ex1);
SALES::showsales(ex2);
delete[]ar;
system("pause");
return ;
} 头文件 namespace SALES
{
const int QUARTERS = ;
struct Sales{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setsales(Sales &s, const double ar[], int n);
void setsales(Sales &s);
void showsales(const Sales&s);
}

p332.1

 #include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std; class Bankacount{
char name[];
char num[];
double deposit;
public:
Bankacount(char *pt="nobody", char *ar="", double balance=0.0);
~Bankacount();
void storage(double ct);
void withdraw(double ct);
void show()const;
}; Bankacount::Bankacount(char *pt, char *ar, double balance)
{
strncpy(name, pt, );
name[] = '\0';
strncpy(num, ar, );
num[] = '\0';
deposit = balance;
} Bankacount::~Bankacount(){ } void Bankacount::storage(double ct){
if (ct < )
cout << "the deposit must be positive\n";
else deposit += ct;
} void Bankacount::withdraw(double ct){
if (ct<)
cout << "the deposit must be positive\n";
else deposit -= ct;
} void Bankacount::show()const{
cout << "name is " << name << endl
<< "number is " << num << endl
<< "deposit now is " << deposit << endl;
} int main(){
Bankacount acount1("GE", "asdf", 0.0);
Bankacount acount2;
Bankacount acount3 = Bankacount("DELL", "34WE", 2.3);
acount1.show();
acount2.show();
acount3.show();
acount1.storage(4.5);
acount2.withdraw(2.7);
acount1.show();
acount2.show(); system("pause");
return ;
}

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

  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 习题答案(4)

    p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&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. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

  2. How many ways(记忆化搜索)

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  3. codechef Chef and The Right Triangles 题解

    Chef and The Right Triangles The Chef is given a list of N triangles. Each triangle is identfied by ...

  4. OpenGL绘制简单场景,实现旋转缩放平移和灯光效果

    本项目实现了用OpenGL绘制一个简单场景,包括正方体.球体和网格,实现了物体的旋转.缩放.平移和灯光效果.附有项目完整代码.有具体凝视.适合刚開始学习的人熟悉opengl使用. 开发情况 开发环境V ...

  5. 深度学习工具caffe具体安装指南

    caffe安装指南-吐血整理 前言: 在一台系统环境较好的linux机器上能够非常easy的安装caffe,可是假设系统本身非常旧,又没有GPU的话.安装就太麻烦了,全部都得从头做起,本文档旨在尽可能 ...

  6. Canvas路径、描边、填充

    <script> var context = document.getElementById('canvas').getContext('2d'); context.font = '48p ...

  7. .cs文件与aspx.cs文件之间的区别是什么???他们的作用是什么???ASPX文件的作用是什么?

    一般在vs里面新建一个页面会产生两种文件:一种是后缀名为.cs的,一种是.aspx. 简单的说,.cs文件一般是在里面实现功能的,而.aspx就是实现界面效果的. 区别:.cs文件里面写的是.net的 ...

  8. JSP标签库

    step1: 定义一个标签处理类:改类必须实现SimpleTag接口,在实际中是拓展它的子类SimpleTagSupport.复写doTag方法 public class MyTag extends ...

  9. R与数据分析旧笔记(⑦)回归诊断

    回归诊断 回归诊断 1.样本是否符合正态分布假设? 2.是否存在离群值导致模型发生较大误差? 3.线性模型是否合理? 4.误差是否满足独立性.等方差.正态分布等假设条件? 5.是否存在多重共线性 正态 ...

  10. 《JavaScript+DOM编程艺术》的摘要(一)---基本知识点

    保持良好的编程习惯:在同一脚本中,保持引号的一致性,都用单引 var mood = "don\"t worry";alert(mood); 数组:var arr=Arra ...