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 第五章:语句 ...
随机推荐
- BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3223 [题目大意] 给出一数列,问m次区间翻转后的结果. [题解] Splay 区间翻 ...
- bootstrap-dialog的使用
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Swift语法总结(精简版)
第一部分: 1. Swift简介 2010年的夏天,苹果公司的开发人员Chris Lattne接到了一个特别的任务,为OS X 和iOS平台开发下一代的编程语言,也就是Swift. 苹果公司于2014 ...
- B. Wet Shark and Bishops(思维)
B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- [置顶] android利用jni调用第三方库——第二篇——编写库android程序直接调用第三方库libhello.so
0:前言 1:本文主要作为丙方android公司的身份来写 2:作者有不对的地方,请指出,谢谢 [第一篇:android利用jni调用第三方库——编写库libhello.so] [第二篇:androi ...
- Android中SharedPreferences函数具体解释
Android平台提供了一个SharedPreferences类,它是一个轻量级应用程序内部轻量级的存储方案,特别适合用于保存软件配置參数,比方boolean,int,float,long,Strin ...
- Effective C++ 条款11
在operator=中处理"自我赋值" 什么是自我赋值,非常明显. 就是自己的值赋值给了自己.以下的代码就是自我赋值: class Widget { public: Widget& ...
- CSS用法简介
CSS(Cascading Style Sheets层叠样式表)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言. 1.基本使用语法 ...
- css Tab选项卡
css tab 选项卡据说有2中实现方式 1. target css3 2. 描点 2的 核心原理是利用描点显示问题(描点父级 overflow). <style> body,div,ul ...
- iframe自适应高度代码
var adjustIframe = function (id) { var iframe = document.getElementById(id) var idoc = iframe.conten ...