c++ primer plus 习题答案(3)
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)的更多相关文章
- c++ primer plus 习题答案(1)
c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...
- c++ primer plus 习题答案(8)
p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...
- c++ primer plus 习题答案(7)
p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...
- c++ primer plus 习题答案(6)
p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(5)
p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(4)
p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...
- c++ primer plus 习题答案(2)
p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...
- C++Primer第五版——习题答案目录
目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...
- 《C++Primer》第五版习题答案--第五章【学习笔记】
<C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...
随机推荐
- Android使用VideoView播放网络视频
Android支持播放网络上的视频.在播放网络上的视频时,牵涉到视频流的传输,往往有两种协议,一种是HTTP,一种是RTSP.这 两种协议最大的不同是,HTTP协议,不支持实时流媒体的播放,而RTSP ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- 面向对象程序设计-C++_课时13初始化列表
构造函数设置成员初值方法有两种:一种是在函数体内赋值,另一种是采用初始化列表的形式. 初始化列表BETTER 函数体内赋值 类名::类名(形参1,形参2,...形参n) { 数据成员1=形参1; 数据 ...
- Java Dom解析xml
Dom解析是将xml文件全部载入,组装成一颗dom树,然后通过节点以及节点之间的关系来解析xml文件,下面结合这个xml文件来进行dom解析. <?xml version="1.0&q ...
- Ubuntu常用命令整理
最近开始用Ubuntu系统了,各种命令很不熟练,想收集一下,以便以后查阅,用这个时常更新的随笔 1.Ubuntu设置与修改用户密码 设置ROOT密码方法:sudo passwd root ,然后输入密 ...
- BZOJ 1066: [SCOI2007]蜥蜴( 最大流 )
结点容量..拆点然后随便写 --------------------------------------------------------------- #include<cstdio> ...
- hdu 4034 Graph floyd
题目链接 给出一个有向图各个点之间的最短距离, 求出这个有向图最少有几条边, 如果无法构成图, 输出impossible. folyd跑一遍, 如果dp[i][j] == dp[i][k]+dp[k] ...
- 树莓派高级GPIO库,wiringpi2 for python使用笔记(二)高精度计时、延时函数
学过单片机的同学应该清楚,我们在编写传感器驱动时,需要用到高精度的定时器.延时等功能,wiringpi提供了一组函数来实现这些功能,这些函数分别是: micros() #返回当前的微秒数,这个数在调用 ...
- html 实现网址链接
<a href="http://acm.nyist.net/JudgeOnline/problemset.php">南工oj</a> HTML学习 < ...
- MySQL Select 优化
准备: create table t(x int primary key,y int unique,z int); insert into t(x,y,z) values(1,1,1),(2,2,2) ...