IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++prime plus第九章编程练习答案 -> 正文阅读

[C++知识库]C++prime plus第九章编程练习答案

1.(多文件程序)

tips:1.在一个项目中自定义头文件时,头文件的标题是以.h结尾,而.cpp是指源文件,且在其他同项目的文件中引用该头文件是以#include“”
2.若在以上操作中对于自定义的头文件仍显示“无法打开源文件,可试验以下方法

我的项目下新建的头文件为 my.h

找到其在硬盘上的路径,D:\C++\day1\project1\project1\hwenjian,在VS项目project1上右键属性,C/C+±>常规->附加包含目录->编辑中,把此路径添加上

//golf.h
#ifndef GOLF_H
#define GOLF_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 // !GOLF_H

//main.cpp
#include<iostream>
#include "golf.h"
using namespace std;

int main()
{
	golf list[len];
	cout << "Please enter the basic information of the member:" << endl;
	 
	int i = 0;
	char full[len];
	int hand;
	cout << "#" << i + 1 << endl;
	cin.getline(full, len);
	cin >> hand;
	cin.get();
	setgolf(list[i], full, hand);
	while (list[i].fullname != "\0" && i < 3)
	{
		i++;
		cout << "#" << i + 1 << endl;
		cin.getline(full, len);
		cin >> hand;
		cin.get();
		setgolf(list[i], full, hand);
	}
	cout << "Which member would you like to correct and how to correct?";
	int n, value;
	cin >> n;
	cin >> value;
	handicap(list[n-1], value);
	cout << "This is the result after correction" << endl;
    showgolf(list[n-1]);
	return 0;
}
 

```cpp
//golf.cpp
#include<iostream>
#include<cstring>
#include "golf.h"


using namespace std;
void setgolf(golf& g, const char* name, int hc)
{
	strcpy_s(g.fullname, name);
	g.handicap = hc;

};

int setgolf(golf& g)
{
	cin.getline(g.fullname, len);
	if (g.fullname == NULL)
		return 0;
	cin >> g.handicap;
	return 1;
}
void handicap(golf& g, int hc)
{
	g.handicap = hc;
}
void showgolf(const golf& g)
{
	cout << g.fullname << endl;
	cout << g.handicap << endl;
}

3.(new定位运算符)

#include<iostream>
#include<cstring>
#include<new>
using namespace std;
const int len = 20;
struct chaff
{
	char doss[len];
	int slag;
};
void show_data(chaff & c);
 
int main()
{
	int a[100];
	/*chaff* pd = new(a)chaff;
	cout << "Please enter the data of #1" << endl;
	char d1[len], d2[len];
	int s1, s2;
	cin.getline(d1, len);
	cin >> s1;
	cin.get();
	cout << "Please enter the data of #2" << endl;
	cin.getline(d2, len);
	cin >> s2;
	cin.get();
	strcpy_s(pd[0].doss, d1);
	pd[0].slag = s1;
	strcpy_s(pd[1].doss, d2);
	pd[1].slag = s2;
	for (int i = 0; i < 2; i++)
		show_data(pd[i]);*/

	chaff* pi = new chaff[2];
	chaff* pd = new(pi)chaff;
	cout << "Please enter the data of #1" << endl;
	char d1[len], d2[len];
	int s1, s2;
	cin.getline(d1, len);
	cin >> s1;
	cin.get();
	cout << "Please enter the data of #2" << endl;
	cin.getline(d2, len);
	cin >> s2;
	cin.get();
	strcpy_s(pd[0].doss, d1);
	pd[0].slag = s1;
	strcpy_s(pd[1].doss, d2);
	pd[1].slag = s2;
	for (int i = 0; i < 2; i++)
		show_data(pd[i]);
	delete[]pi;



}
void show_data(chaff& c)
{
	cout << c.doss << endl;
	cout << c.slag << endl;
}

4.(名称空间)

//sale.h
#ifndef SALE_H
#define SALE_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 // !SALE_H

#include<iostream>
#include"sale.h"
using namespace std;
int main()
{
	using SALES::Sales;
	Sales a, b;
	SALES::setsales(a);
	cout << "Please enter the non-interactive version";
		cout << "please enter the data for 4:" << endl;
    double ar[4];
	for (int i = 0; i < 4; i++)
	{
		cout << "#" << i + 1;
		cin >> ar[i];
	}
	SALES::setsales(b, ar, 4);
	cout << "We can start to show the two structures";
	cout << "#a" << endl;
	SALES::showsales(a);
	cout << "#b" << endl;
	SALES::showsales(b);

}
#include<iostream>
using namespace std;
#include"sale.h"
namespace SALES
{
	 
	/*struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};*/
	void setsales(Sales& s, const double ar[], int n)
	{
		int m = (QUARTERS < n) ? QUARTERS : n;
		double sum = 0, max = ar[0], min = ar[0];
		for (int i = 0; i < m; i++)
		{
			s.sales[i] = ar[i];
			sum += ar[i];
			max = (ar[i] > max ? ar[i] : max);
			min = (ar[i] < min ? ar[i] : min);
		}
		s.average = sum / m;
		s.max = max;
		s.min = min;
	}
	void setsales(Sales& s)
	{
		cout << "please enter the data for 4:" << endl;
		for (int i = 0; i < 4; i++)
		{
			cout << "#" << i + 1;
			cin >> s.sales[i];
		}
		double sum = 0, max = s.sales[0], min = s.sales[0];
		for (int i = 0; i < 4; i++)
		{
			sum += s.sales[i];
			max = (s.sales[i] > max ? s.sales[i] : max);
			min = (s.sales[i] < min ? s.sales[i] : min);
		}
		s.average = sum / QUARTERS;
		s.max = max;
		s.min = min;
	}
	void showsales(const Sales& s)
	{
		cout << "Now we are going to show the salas;" << endl;
		for (int i = 0; i < QUARTERS; i++)
		{
			cout << s.sales[i] << " ";
		}
		cout << endl;
		cout << "average:" << s.average << endl;
		cout << "max:" << s.max << endl;
		cout << "min:" << s.min << endl;
	}
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-13 11:45:37  更:2021-08-13 11:47:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 12:15:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码