[C++ Primer Plus] 第9章、内存模型和名称空间(二)课后习题
一、复习题


2.using声明和using编译指令的区别
using声明: using std::cin; using std::cout; using std::endl;
using编译指令:using namespace std;
二、编程练习


头文件golf.h
const int Len = ;
struct golf {
char fullname[Len];
int handicap;
}; void setgolf(golf &g,const char * name,int hc);
int setgolf(golf &g);
void handicap(golf &g, int hc);
void showgolf(const golf &g);
golf.cpp
#include<iostream>
#include<string>
#include "golf.h"
//using std::cin;
//using std::cout;
//using std::endl;
using namespace std; void setgolf(golf &g, const char * name, int hc) {
strcpy_s(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf &g) {
cout << "Enter name:";
cin >> g.fullname;
if (g.fullname[] == '\0')
return ;
cout << "Enter handicap value: ";
while (!(cin >> g.handicap)) //如果输入错误
{
cin.clear();
cout << "请输入整数:";
}
while (cin.get() != '\n')
continue;
return ;
}
void handicap(golf &g, int hc) {
g.handicap = hc;
}
void showgolf(const golf &g) {
cout << "Golfer: " << g.fullname <<endl;
cout << "Handicap: " << g.handicap <<endl;
}
main.cpp
#include<iostream>
#include<string>
#include "golf.h"
using namespace std; const int Mems = ;
void main()
{
golf team[Mems];
cout << "输入 " << Mems << " 球队成员:\n";
int i;
for (i = ; i<Mems; i++)
if (setgolf(team[i]) == )
break;
cout << endl;
for (int j = ; j<i; j++)
showgolf(team[j]);
setgolf(team[], "Fred Norman", );
showgolf(team[]);
handicap(team[], );
showgolf(team[]);
system("pause");
}

2、修改程序清单9.9:用string对象代替字符数组.这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串""进行比较,比判断是否为空行
修改前
#include<iostream>
using namespace std; const int Size=;
void strcount(const char *str){//const表示str指针不能修改指向的内容(不过可以指向另外一块内容)
static int total=;//static静态变量,首次初始化后,其值一直存在(即第二次调用strcount函数时,total的值不会再次初始化)
int count=;
cout<<"\""<<str<<"\" contains ";
while (*str++)//先判断*str是否为NULL,然后再str++
count++;
total+=count;
cout<<count<<" characters\n";
cout<<total<<" characters total!\n";
} void main() {
char in[Size];
char next;
cout<<"Enter a line:"<<endl;
cin.get(in,Size);//最多接收Size-1个字符+1个'\0'
while (cin) // ==while(!cin.fail()),即读入流成功
{
cin.get(next);
while(next!='\n') //若next不是换行符
cin.get(next);
strcount(in);
cout<<"Enter next line (empty line to quit):\n";
cin.get(in,Size);
}
cout<<"Bye!"<<endl;
system("pause");
}
修改后
#include<iostream>
#include<string>
using namespace std; void strcount(const string &str){
static int total=;//static静态变量,首次初始化后,其值一直存在(即第二次调用strcount函数时,total的值不会再次初始化)
int count=str.length();
cout<<"\""<<str<<"\" contains ";
total+=count;
cout<<count<<" characters\n";
cout<<total<<" characters total!\n";
} void main() {
string input;
cout<<"Enter a line:"<<endl;
getline(cin,input);
while (""!=input)
{
strcount(input);
cout<<"Enter next line (empty line to quit):\n";
getline(cin, input);
}
cout<<"Bye!"<<endl;
system("pause");
}

3.下面是一个结构声明

#include<iostream>
#include<cstring>
using namespace std; const int BUF = ;
const int N = ;
char buffer[BUF];
struct chaff
{
char dross[];
int slag;
}; void main()
{
//使用静态数组作为缓冲区
chaff *cf = new(buffer)chaff[N]; //定位new运算符:将数组cf放在了数组buffer中
for (int i = ; i < N; i ++)
{
cout << "Please enter dross: ";
char dross[];
cin.getline(dross,);
strcpy_s(cf[i].dross, dross);
cout << "Please enter slag:";
cin >> cf[i].slag;
cin.get();
}
for (int i = ; i < N; i++)
cout << cf[i].dross << " : " << cf[i].slag << endl; //使用动态数组作为缓冲区
char* buffer2 = new char[BUF];
chaff* cf2 = new(buffer2)chaff[N];
for (int i = ; i < N; i++)
{
cout << "Please enter dross: ";
char dross[];
cin.getline(dross, );
strcpy_s(cf2[i].dross, dross);
cout << "Please enter slag:";
cin >> cf2[i].slag;
cin.get();
}
for (int i = ; i < N; i++)
cout << cf2[i].dross << " : " << cf2[i].slag << endl;
cf2 = NULL;//把这个置为空指针
delete[] buffer2;//把缓冲区删除了 system("pause");
}


sale.h头文件
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);
}
sale.cpp函数定义
#include <iostream>
#include "sale.h"
using namespace std; void SALES::setSales(Sales & s, const double ar[], int n)//使用命名空间SALES后就可不必添加SALES::
{
double total = ;
for (int i = ; i < QUARTERS; i++)
{
if (i >= n)
s.sales[i] = ;
else
s.sales[i] = ar[i];
if (i == )
{
s.max = s.sales[i];
s.min = s.sales[i];
}
else
{
if (s.sales[i] > s.max)
s.max = s.sales[i];
if (s.sales[i] < s.min)
s.min = s.sales[i];
}
total += s.sales[i];
}
s.average = total / QUARTERS;
} void SALES::setSales(Sales & s)
{
double d[QUARTERS];
for (int i = ; i < QUARTERS; i++)
{
cout << "Enter the sales:";
cin >> d[i];
}
setSales(s, d, QUARTERS);
} void SALES::showSales(const Sales& s)
{
cout << "Sales:";
for (int i = ; i < QUARTERS; i++)
{
cout << s.sales[i];
cout << "\t\t";
}
cout << "\nMin:" << s.min << " \tMax:" << s.max << " \taverage:" << s.average << endl;
}
main.cpp主函数
#include<iostream>
#include "sale.h"
using namespace std; void main() {
double d[] = { 123.3, , 342.333, };
SALES::Sales s1, s2;
setSales(s1, d, );
setSales(s2);
showSales(s1);
showSales(s2);
system("pause");
}

[C++ Primer Plus] 第9章、内存模型和名称空间(二)课后习题的更多相关文章
- C++ primer plus读书笔记——第9章 内存模型和名称空间
第9章 内存模型和名称空间 1. 头文件常包含的内容: 函数原型. 使用#define或const定义的符号常量. 结构声明. 类声明. 模板声明. 内联函数. 2. 如果文件名被包含在尖括号中,则C ...
- 《C++ Primer Plus》第9章 内存模型和名称空间 学习笔记
C++鼓励程序员在开发程序时使用多个文件.一种有效的组织策略是,使用头文件来定义用户类型,为操纵用户类型的函数提供函数原型,并将函数定义放在一个独立的源代码文件中.头文件和源代码文件一起定义和实现了用 ...
- 《C++ Primer Plus 6th》读书笔记 - 第九章 内存模型和名称空间
1. 单独编译 1.1 头文件中常包含的内容: 函数原型 使用#define或const定义的符号常量 结构声明 类声明 模板声明 内联声明 1.2 只需将源代码文件加入到项目中,而不用加入头文件.这 ...
- 《C++ Primer Plus》读书笔记之七—内存模型和名称空间
第九章 内存模型和名称空间 1.不要将函数定义或者变量声明放到头文件中. 2.头文件常包含的内容:函数原型.使用#define或者const定义的常量.结构声明.类声明.模板声明.内联函数. 3.避免 ...
- [C++ Primer Plus] 第9章、内存模型和名称空间(一)程序清单
程序清单9.9(静态存储连续性.无链接性) #include<iostream> using namespace std; ; void strcount(const char *str) ...
- (8)C++ 内存模型与名称空间
一.单独编译 头文件 不要将函数定义或者变量声明放到头文件中,引入多个文件时可能会造成同一个函数定义多次 引入头文件 #include "文件名" File1.h #ifndef ...
- C++ Primer Plus读书笔记(九)内存模型和名称空间
1.作用域和链接 int num3; static int num4; int main() { } void func1() { static int num1; int num2; } 上边的代码 ...
- C++学习 内存模型和名称空间
1.单独编译 C++鼓励程序员将组件函数放在独立的文件中,如果只修改了一个文件,则可以只重新编译该文件,然后将它与其他文件的编译版本链接. 一般非常有用的组织程序的策略是把程序分成三部分: 头文件:包 ...
- Java内存模型解惑--观深入理解Java内存模型系列文章有感(二)
1.volatile关键字修饰的域的特性 当我们声明共享变量为volatile后,对这个变量的读/写将会很特别.理解volatile特性的一个好方法是:把对volatile变量的单个读/写,看成是使用 ...
随机推荐
- 关于Java8:StreamAPI的一点记录
关于 Stream ,Functional Interface 的一点记录 stream对于集合操作的便捷度提升: import java.util.ArrayList; import java.ut ...
- [daily][archlinux] TODO LIST
reminder: https://wikemacs.org/wiki/Buffer_management TO READ: https://www.kernel.org/doc/htmldocs/k ...
- [daily] 不让NetworkManger自动接管网络设备
一 场景描述 有一个dpdk程序,在运行时使用了rte kni. 它启动的时候,会自动给系统增加一个网卡设备,停止运行的时候又会把它去掉.像这样: [root@T9 gen-py]# ip a : ...
- 快速搭建一个直播Demo
缘由 最近帮朋友看一个直播网站的源码,发现这份直播源码借助 阿里云 .腾讯云这些大公司提供的SDK 可以非常方便的搭建一个直播网站.下面我们来给大家讲解下如何借助 腾讯云 我们搭建一个简易的 直播示例 ...
- python之dict
一.字典的定义 在python中,字典数据类型使用{}来定义,在大括号中,存储的是键值对,即key:value的形式,并且key不能有重复值,如果有重复,后面的值会覆盖前面的:值可以重复 # 字典的定 ...
- linux sudo 运行找不到java、python命令
在Ubuntu环境中安装好Java环境后设置环境变量:在/etc/profile中设置好了JAVA_HOME变量并引入到PATH中, 由于Ubuntu默认是不以root用户登录的,这时echo $PA ...
- JQ基本选择器
JQ选择器采用CSS和Xpath选择器语法规范,满足用户在DOM中快速匹配元素或元素集合. 1.JQ支持CSS1.CSS2.CSS3.不同版本的所有选择器,而早期的很多浏览器并没有完全支持CSS3版本 ...
- vue + ts @Prop boolean 问题
假设btn组件有一prop属性radio,声明如下 @Prop({ default: false }) radio!: boolean; 在组件传递 <btn radio /> 此时的 r ...
- python学习笔记1-基础知识
# 0.输入输出 # print数值型直接输出计算结果 pirnt( + ) # 输出 + = # input输入(可在括号内加提示语句) name = input('please enter you ...
- oracle 主键,非空,检查,唯一,默认,外键约束
--首先添加主键约束alter table studentadd constraint PK_student_sno primary key(sno) --删除约束alter table studen ...