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)的更多相关文章

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

    p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&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. C语言入门(12)——递归

    一个函数在它的函数体内调用它自身称为递归调用.有递归调用操作的函数被称为递归函数.递归调用可以是直接调用,也可以是间接调用.也可以理解为函数的嵌套调用是函数本身. 例如实现一个求阶乘的函数: long ...

  2. PHPExcel 多工作表 导出

    //浏览器输出excel header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ...

  3. EOF 空格问题

    mysql -u $USER -p${PASSWORD} $DATABASE << EOF >/tmp/dd-$$ 2>/tmp/ddd-$$select *from $TAB ...

  4. Mac 域名解析

    1. 域名解析 如,解析 www.baidu.com 在终端输入, host www.baidu.com 题外话: 转载自:http://hoarn.blog.51cto.com/1642678/14 ...

  5. The Water Problem(排序)

    The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. Ubuntu小私房(3)--Uubutnu启动美化大变身

    Grub是什么? GNU GRUB 和GRUB是GRand Unified Bootloader的缩写,它是一个多重操作系统启动管理器.用来引导不同系统,如windows,linux.GRUB是多启动 ...

  7. getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()的作用

    getCacheDir()方法用于获取/data/data/<application package>/cache目录 getFilesDir()方法用于获取/data/data/< ...

  8. Web安全測试二步走

    Web安全測试时一个比較复杂的过程,软件測试人员能够在当中做一些简单的測试,例如以下: Web安全測试也应该遵循尽早測试的原则,在进行功能測试的时候(就应该运行以下的測试Checklist安全測试场景 ...

  9. 运行JBoss 5.1.0 GA时出现Error installing to Instantiated:name=AttachmentStore state=Described错误的解决办法

    第一次开JBoss服务器:有些时候会遇到这种情况:把以下的文字替换即可 进到类似目录 server/default/conf/bootstrap,打开文件 profile.xml找到: Xml代码 & ...

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

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