C++ primer Plus(第六版)_课后编程练习_第七章

本博客提供了多个C++编程实例,涵盖函数定义、数组操作、结构体应用、概率计算、数组填充与显示、数组逆序、动态数组填充与显示、结构体成员函数、学生信息管理、数学运算函数调用等主题,旨在帮助读者理解和掌握C++编程的基本概念和技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习代码为本人所写,欢迎大家学习交流

若有疑问请留言,我将尽快回复!!!!

题7.1:

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

double Harmonic(double a, double b);

int main()
{
	double x, y;
	cout << "Please enter two double numble: ";
	cin >> x >> y;
	cout << "The Harmonic average of x and y: " << Harmonic(x, y);

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

double Harmonic(double a, double b)
{
	double Harmonic_average = 2.0 * a * b / (a + b);
	return Harmonic_average;
}

题7.2:

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

const int MAX = 10;
int fill_score(double ar[], int limit);
void show(double ar[], int n);

int main()
{
	double score_golf[MAX];
	int size = fill_score(score_golf, MAX);
	show(score_golf, size);


	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

int fill_score(double ar[], int limit)
{
	double temp;
	int i = 0;
	for (i = 0; i < limit; i++)
	{
		cout << "Enter the " << i + 1 << " score: ";
		cin >> temp;
		if (!cin || temp < 0)
			break;
		ar[i] = temp;
	}
	return i;   //注意这里返回的就是个数
}

void show(double ar[], int n)
{
	double sum = 0;
	double average_score;

	if (n == 0)
		cout << "there is no score!!!!!";  
	else
	{
		for (int i = 0; i < n; i++)
		{
			cout << ar[i] << "   ";
			sum += ar[i];
		}
		average_score = sum / n;
		cout << endl << "average of score: " << average_score;
	}
}

题7.3:

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

struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volum;
};

void show(box t);
void set_volum(box * ps);

int main()
{
	box box1={"Jiaqing Huang", 2, 3, 4};
	show(box1);    //记住直接用结构是不能改变内容的
	set_volum(&box1);   //用指针可以改变其中的结构

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

void show(box t)
{
	cout << "maker: " << t.maker << "  ||  height: " << t.height << "  ||  width: " << t.width << "  ||  length: " << t.length << endl;
}

void set_volum(box * ps)
{
	ps->volum = ps->height * ps->width * ps->length;
	cout << "volum: " << ps->volum;
}

题7.4:

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

long double probability(int numb_normal_field, int pick, int numb_special_field);

int main()
{
	int numb_normal_field_your, pick_your,  numb_special_field_your;
	cout << "Please enter the number of normal field, number picked, number of special field: ";
	
	while (cin >> numb_normal_field_your >> pick_your >> numb_special_field_your)
	{
		cout << "You have ";
		cout << probability(numb_normal_field_your, pick_your, numb_special_field_your) * 100 ;  
		cout << "% chance of winning!!";
	}

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

long double probability(int numb_normal_field, int pick, int numb_special_field)
{
	int n, p;
	long double result = 1.0;

	for (n = numb_normal_field, p = pick; p > 0; n--, p--)
		result = result * p / n;

	result = result / numb_special_field;
	return result;
}

题7.5:

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

int factorial(int n);

int main()
{
	int numb;
	cout << "Enter a number: ";
	cin >> numb;

	cout << "The factorial of " << numb << ": " << factorial(numb);

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

int factorial(int n)
{
	if (n == 1 || n == 0)
		return 1;
	else 
		return n * factorial(n-1);
}

题7.6:

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

const int MAX = 10;

int Fill_array(double ar[], int limit);
void Show_array(double ar[], int n);
void Reverse_array(double ar[], int n);

int main()
{
	double numb[MAX];

	cout << "Input:" << endl;
	int size = Fill_array(numb, MAX);

	cout << "Show:" << endl;
	Show_array(numb, size);

	cout << "Reverse and show:" << endl;
	Reverse_array(numb, size);
	Show_array(numb, size);

	cout << "Reverse and show(In addition to the first and last number):" << endl;
	Reverse_array(numb + 1, size - 2);
	Show_array(numb, size);

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 50 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

int Fill_array(double ar[], int limit)   //数组是可以直接改变的,不会使用副本(因为数组名原理上是指针)
{
	int i;
	for (i = 0; i < limit; i++)
	{
		double temp;
		cout << "Enter the " << i + 1 << " double number: ";
		cin >> temp;
		if (!cin)
			break;
		ar[i] = temp;
	}
	return i;
}

void Show_array(double ar[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << ar[i] << "   ";
	}
	cout << endl;
}

void Reverse_array(double ar[], int n)
{
	double temp;
	for (int i = 0; i < (n / 2); i++)
	{
		temp = ar[(n-1)-i];
		ar[(n-1)-i] = ar[i];
		ar[i] = temp;
	}
}

题7.7:

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

const int MAX = 10;

double * Fill_array(double ar[], int limit);
void Show_array(double ar[], double * pt);
void Reverse_array(double r, double ar[], double * pt);

int main()
{
	double numb[MAX];

	cout << "Input:" << endl;
	double *p_end = Fill_array(numb, MAX);

	cout << "Show:" << endl;
	Show_array(numb, p_end);

	cout << "* r and show:" << endl;
	Reverse_array(0.5, numb, p_end);
	Show_array(numb, p_end);


	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 50 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

double * Fill_array(double ar[], int limit)
{
	int i;
	for (i = 0; i < limit; i++)
	{
		double temp;
		cout << "Enter the " << i + 1 << " double number: ";
		cin >> temp;
		if (!cin)
			break;
		ar[i] = temp;
	}
	return &ar[i - 1];
}

void Show_array(double ar[], double * pt)
{
	for (double * ps = &ar[0]; ps <= pt; ps++)
	{
		cout << *ps << "   ";
	}
	cout << endl;
}

void Reverse_array(double r, double ar[], double * pt)
{
	for (double * ps = &ar[0]; ps <= pt; ps++)
	{
		*ps *= r;
	}
}

题7.8:

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

const int Seasons = 4;
const char *Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" }; //相当于是把四                          个字符串的首地址传给了sname

struct family
{
    double expenses_b[Seasons];
};

void fill_a(double *pa);
void show_a(double *pa);
void fill_b(family *pb);
void show_b(family *pb);


int main()
{
	cout << "练习a: " << endl;
	double expenses_a[Seasons] = {0};
	fill_a(expenses_a);
        show_a(expenses_a);
	cout << "---------------------------------------------------" << endl;

	cout << "练习b: " << endl;
	family my_family;
	fill_b(&my_family);   //记住用指针才可以改变结构变量,直接用结构名是不行的
        show_b(&my_family);

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 50 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

void fill_a(double *pa)
{
    for (int i = 0; i < Seasons; i++)
    {
        cout << "Enter the expenses of " << Snames[i] << " : ";
        cin >> pa[i];
    }
}

void show_a(double *pa)
{
    double total = 0.0;
    cout << "\nEXPENSES\n";
    for (int i = 0; i < Seasons; i++)
    {
        cout << Snames[i] << ": $" << pa[i] << endl;
        total += pa[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

void fill_b(family *pb)
{
    for (int i = 0; i < Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> pb->expenses_b[i];
    }
}

void show_b(family *pb)
{
    double total = 0.0;
    cout << "\nEXPENSE\n";
    for (int i = 0; i < Seasons; i++)
    {
        cout << Snames[i] << ": $" << pb->expenses_b[i] << endl;
        total += pb->expenses_b[i];
    }
    cout << "Total Expense: $" << total << endl;
}

题7.9:

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

const int SLEN = 30;

struct student
{
    char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};

int getinfo(student pa[], int n);
void display1(student st);
void display2(student * ps);
void display3(const student pa[], int n);


int main()
{
	cout << "Enter class size: ";
	int class_size;
	cin >> class_size;

	while (cin.get() != '\n')
	{
		continue;
	}

	student * ptr_stu = new student[class_size];
	int entered = getinfo(ptr_stu, class_size);
	for (int i = 0; i < entered; i++)
	{
		cout << "Student " << i + 1 << " :-------------------------------------------" << endl;
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete [] ptr_stu;
	cout << "DONE" << endl;

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 50 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

int getinfo(student pa[], int n)
{
    int i;
	for (i = 0; i < n; i++)
    {
        cout << "Enter the information of the " << i + 1 << "student!" << endl;
		cout << "Fullname: ";
		cin.getline(pa[i].fullname, SLEN);
		cout << "Hobby: ";
		cin.getline(pa[i].hobby, SLEN);
		cout << "Ooplevel: ";
		cin >> pa[i].ooplevel;
		cin.get();      //记住在输入数字之后一定要把回车给读掉才能下一步读入一行
    }
	return i;
}

void display1(student st)
{
    cout << "Display1--------------" << endl;
	cout << "Fullname: " << st.fullname << endl;
	cout << "Hobby: " << st.hobby << endl;
	cout << "Ooplevel: " << st.ooplevel << endl;
}

void display2(student * ps)
{
    cout << "Display2--------------" << endl;
	cout << "Fullname: " << ps->fullname << endl;
	cout << "Hobby: " << ps->hobby << endl;
	cout << "Ooplevel: " << ps->ooplevel << endl;
}

void display3(const student pa[], int n)
{
    cout << "Display3********************************************************" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << "Student " << i + 1 << " :----------------" << endl;
		cout << "Fullname: " << pa[i].fullname << endl;
		cout << "Hobby: " << pa[i].hobby << endl;
		cout << "Ooplevel: " << pa[i].ooplevel << endl;
	}
}

题7.10:

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

double add(double a, double b);
double sub(double a, double b);
double mul(double a, double b);
double div(double a, double b);
double calculate(double a, double b, double (*fun)(double, double));

int main()
{
	double x, y;
	char function;

	cout << "Enter two numbers and the function of calculate:" << endl;
	cout << "A:add    B:sub    C:mul    D:div" << endl;
	while (cin >> x >> y >> function)
	{
		switch (function)
		{
		case 'A':	cout << "Calculation results: " << calculate(x, y, add) << endl;
					break;
		case 'B':	cout << "Calculation results: " << calculate(x, y, sub) << endl;
					break;
		case 'C':	cout << "Calculation results: " << calculate(x, y, mul) << endl;
					break;
		case 'D':	cout << "Calculation results: " << calculate(x, y, div) << endl;
					break;
		default :	cout << "This is not a choice! Enter again" << endl;
		}
	}

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 50 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start <delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

double add(double a, double b)
{
	return a + b;
}

double sub(double a, double b)
{
	return a - b;
}

double mul(double a, double b)
{
	return a * b;
}

double div(double a, double b)
{
	return a / b;
}

double calculate(double a, double b, double (*fun)(double, double))
{
	return fun(a, b);
}

 

资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持多种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问题。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个项目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值