C++ Primer Plus课后复习题及编程练习第九章-内存模型和名称空间

文章介绍了C++中的内存管理技术,如自动存储、文件共享和静态链接,以及如何处理用户输入验证和结构体的定义与操作,包括使用命名空间和动态数组。还包含两个编程练习实例,展示了相关概念的实际应用。
摘要由CSDN通过智能技术生成

9.5 复习题

1.a:利用自动存储

b:多个文件共享,仅需在一个文件进行定义,存储形式是静态外部链接。

c:一个文件中所有函数可以使用,其他文件无法使用,使用内部静态链接

d:利用本地静态变量。

3.

#include<iostream>

int main() {

	double x;
	std::cout << "Enter value: ";
	while (!(std::cin >> x)) {
		std::cout << "Bad input.Please enter a number: ";
		std::cin.clear();
		while (std::cin.get() != '\n')
			continue;
	}
	std::cout << "Value = " << x << std::endl;

	system("pause");
	return 0;
}

4.

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

int main() {

	double x;
	cout << "Enter value: ";
	while (!(cin >> x)) {
		cout << "Bad input.Please enter a number: ";
		cin.clear();
		while (cin.get() != '\n')
			continue;
	}
	cout << "Value = " << x << endl;

	system("pause");
	return 0;
}

6.

7.

9.6 编程练习

1.

//头文件“demo1.h”
#pragma once

#ifndef COORDIN_H_
#define COORDIN_H_

const int Len = 40;

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);

#endif // !COORDIN_H_
//golf.cpp
#include<iostream>
#include<string>
#include"demo1.h"
using namespace std;

void setgolf(golf& g, const char* name, int hc) {
	for (int i = 0; i < Len; i++) {
		g.fullname[i] = *name;
		name++;
	}
	g.handicap = hc;
}

int setgolf(golf& g) {
	char name[Len];
	int hc;
	cout << "Please enter the name: ";
	cin.get(name, Len);
	cin.ignore();
	cout << "and its hc: ";
	cin >> hc;
	if (name[0] != '\0') {
		setgolf(g,name,hc);
		return 1;
	}
	else
	{
		return 0;
	}
}

void handicap(golf& g, int hc) {
	g.handicap = hc;
}

void showgolf(const golf& g) {
	cout << g.fullname << endl;
	cout << g.handicap << endl;
}
//main函数
#include<iostream>
#include"demo1.h"
using namespace std;

int main() {

	golf g;
	setgolf(g, "sdhsu", 100);
	showgolf(g);
	
	setgolf(g);
	showgolf(g);

	system("pause");
	return 0;
}

 2.

#include<iostream>
#include<string>
using namespace std;

int main() {

    //2.
	void strcount(const string str);

	string str;
	string str_test;

	cout << "Enter a line:" << endl;;
	getline(cin, str);
	while (cin) {
		str_test = str;
		while (str_test == "") {
			cout << "It's Null" << endl;
			cout << "Bye!" << endl;
			return 0;
		}
		strcount(str);
		cout << "Enter next line (empty line to quit):\n ";
		getline(cin, str);
	}
	
	system("pause");
	return 0;
}

void strcount(const string str) {
	static int total = 0;
	int count = 0;

	cout << "\"" << str << "\" contains ";
	count = str.length();
	total += count;
	cout << count << " characters\n";
	cout << total << " characters total\n";
}

4.

//头文件namespace4.h
#pragma once

#ifndef COORDIN_H_
#define COORDIN_H_

namespace SALES {
	const int QUARTERS = 4;
	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);
}

#endif // !COORDIN_H_
//函数定义func.cpp
#include<iostream>
#include"namespace4.h"
using namespace SALES;

//注意这里的“SALES::”必须写,因为这个函数声明在名称空间“SALES”中
void SALES::setSales(Sales& s, const double ar[], int n) {
	using namespace std;

	double sum = 0;
	double max, min;

	if (n >= QUARTERS) {
		for (int i = 0; i < QUARTERS; i++) {
			s.sales[i] = ar[i];
		}
	}
	else {
		for (int i = 0; i < n; i++) {
			s.sales[i] = ar[i];
		}
	}
	max = min = s.sales[0];
	cout << "Data at a glance:" << endl;
	for (int i = 0; i < QUARTERS; i++) {
		cout << "NO." << i + 1 << "'s sales:" << s.sales[i] << endl;
		sum += s.sales[i];
		if (max < s.sales[i]) max = s.sales[i];
		if (min > s.sales[i]) min = s.sales[i];
	}
	s.average = sum / QUARTERS;
	s.max = max;
	s.min = min;
}

void SALES::setSales(Sales& s) {
	using namespace std;

	double ar[QUARTERS];

	for (int i = 0; i < QUARTERS; i++) {
		cout << "Please enter the NO." << i + 1 << " 's sales:";
		cin >> ar[i];
	}
	setSales(s, ar, 4);
	
}

void SALES::showSales(const Sales& s) {
	using namespace std;
	
	cout << "The average: " << s.average << endl;
	cout << "The max: "<< s.max << endl;
	cout << "The min: "<< s.min << endl;
}
//主函数main
#include<iostream>
#include"namespace4.h"
using namespace std;
using namespace SALES;

int main() {

	Sales s;
	setSales(s);
	showSales(s);

	system("pause");
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值