c++ primer plus 习题答案(4)
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)的更多相关文章
- 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 习题答案(3)
p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&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 第五章:语句 ...
随机推荐
- Robot Framework作者建议如何选择自动化测试框架
本文摘自:InfoQ中文站http://www.infoq.com/cn/news/2012/06/robot-author-suggest-autotest Robot Framework作者建议如 ...
- python-大话装饰器
装饰器 装饰器是个什么玩意呢?是个会了不难,装逼神器.但是如果不了解理解起来还是很抽象,让我们试试这个装逼神器吧! 1.什么是装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典 ...
- Count the Colors(线段树,找颜色段条数)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- Python文件或目录操作的常用函数
◆ os.listdir(path) Return a list containing the names of the entries in the directory given by path. ...
- 潜在语义分析Latent semantic analysis note(LSA)原理及代码
文章引用:http://blog.sina.com.cn/s/blog_62a9902f0101cjl3.html Latent Semantic Analysis (LSA)也被称为Latent S ...
- 2014年同年CFA考试中哪些CFA资料没有变化?
从2014年起,美国CFA协会将官方教材.题库.模拟题等CFA资料捆绑在报名费用之中,而以往可以单独选购的纸质版教材也变成了额外购买.这让非常多參加12月的CFA考生产生了借阅6月考生CFA资料的想法 ...
- telnet测试端口号
telnet Ip 端口号 如:telnet localhost 1433 详见此链接(转) http://www.3lian.com/edu/2012/11-08/43232.html
- 在工作空间中构建和使用catkin包
在这篇博客中将会介绍,如何在工作空间中构建和使用一个包. 首先,我们来看一下如何在catkin工作空间中,使用catkin_make工具从源文件构建和安装一个包.使用catkin_make来构建一个c ...
- nginx gzip on
# Gzip settings. gzip on; gzip_http_version 1.0;默认值是1.1 gzip_comp_level ; #压缩级别,1压缩比最小处理速度最快,9压缩比最大但 ...
- JSP page include taglib
page include taglib 语法:<%@ 指令名称 属性=值 属性=值 -%> ------------------- page 1.language 默认值java 2.ex ...