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 第五章:语句 ...
随机推荐
- iOS 数组里面取字典的值
NSArray *arrData = @[@"1",@"2",@"3",@"4"]; NSArray *arrKey = ...
- append与after
apend与apendTo就如同after与insertAfter,表达意思相同,表达不同.A.after(B)=B.insertAfter(A).apend在元素里面添加,after在元素外面添加. ...
- 接着上一个版本在上一个分离access-token和ticket的版本
上代码: 本次修改将获取token和ticket分离出来,分别封装在函数中: 每个函数最后一个参数是一个回调参数: 回调函数的参数,是这一步中需要处理的结果; 结果怎么处理,根据传递进去的函数: va ...
- Cookie、Session
Cookie.Session Cookie 保存在浏览器端. 4kb 只能保存字符串,还不能是中文. 获取:Request.getCookies(); 设置时间:setMaxAge(); 小于零是浏览 ...
- poj 1149 PIGS(最大流经典构图)
题目描述:迈克在一个养猪场工作,养猪场里有M 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的 ...
- WINFORM中几句程序获取整个屏幕的图片及当前窗口的图片快照
/// <summary> /// 获取整个屏幕的图片 /// </summary> /// <returns></returns ...
- 内容高度小于窗口高度时版权div固定在底部
<!doctype html><html><head><meta charset="utf-8"><title>文档内容 ...
- Struts1、Struts2的线程安全问题
Struts 1.x和Struts 2的Action是不是线程安全的? Struts 1.x在第一次请求某个Action时,会创建这个Action实例.但之后再请求该Action实例时,就用之前创建好 ...
- Oracle分析函数之开窗子句-即WINDOWING子句
Oracle的分析函数,对我们进行统计有很大的帮助,可以避免一些子查询等操作,在统计中,我们对开窗函数的接触较少,下面主要介绍下开窗函数的使用; http://www.itpub.net/thread ...
- WebApi服务
WCF 它利用TCP.HTTP.MSMQ等传输协议构建“契约先行”的服务.WCF最初为基于SOAP的服务而设计[xml],繁琐.冗余.慢.沉重 WebApi 基于http协议,轻量级的,支持URL路由 ...