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++ Primer Plus(第6版)---编程作业完成(第七章) -> 正文阅读

[C++知识库]C++ Primer Plus(第6版)---编程作业完成(第七章)

第七章

#include <iostream>
using namespace std;
//符号常量
const int Golf_Num = 10;
struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
//函数原型
double average_0(double,double);
void arr_input(double arr[Golf_Num], int* size);
void arr_display(const double arr[], const int size);
void arr_average(const double arr[], const int size);
void box_inital(void);
void lotto(void);
void compute(void);
void array_plus();
void house(void);
void season_1(void);
void season_2(void);
void excer(void);
void cal_plus(void);

//主函数
int main()
{
	//--------作业一---
	double input_m, input_n;
	double average = 0;
	cout << "Enter two numbers(0 to quit) : ";
	while (!(cin >> input_m >> input_n))
	{
		cin.clear();
		while (cin.get() != '\n')
			continue;
		cout << "Bad input! Please input again!\n";
	}
	while (input_m != 0 && input_n != 0)
	{
		average = average_0(input_m, input_n);
		cout << "input_m: " << input_m << " input_n: " << input_n << endl;
		cout << "average: " << average << endl;
		cout << "Enter two numbers(0 to quit) : ";
		while (!(cin >> input_m >> input_n))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input! Please input again!\n";
		}
	}
	cout << "Done!\n";

	//----------作业二---------
	double array[Golf_Num];
	int size;
	arr_input(array, &size);
	arr_display(array, size);
	arr_average(array, size);

	//-------------作业三------------
	box_inital();

	//----------------作业四------------------
	lotto();

	//------------作业五-------------------
	compute();

	//-------------作业六---------------------
	array_plus();

	//-------------作业七-----------------------
	house();

	//-----------作业八-------------
	season_1();
	season_2();

	//-------------作业九---------------
	excer();

	//----------作业十---------------
	cal_plus();

	return 0;
}

//函数定义
//------------------作业一-------------------
double average_0(double input_m,double input_n)
{
	double average = 0;
	average = 2.0 * input_m * input_n / (input_m + input_n);
	return average;
}


//--------------------作业二-----------------------
//读取
void arr_input(double arr[Golf_Num],int* size)
{
	for (int i = 0;i < Golf_Num;i++)
	{
		cout << "Enter the scores # "<<i+1<<":";
		while (!(cin >> arr[i]))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input!Please enter again!\n";
			cout << "Enter the scores # " << i + 1 << ":";
		}
		if (arr[i] < 0)
		{
			arr[i] = 0;
			*size = i;
			break;
		}
	}
	
	return;
}
//显示
void arr_display(const double arr[],const int size)
{
	for (int i = 0;i < size;i++)
		cout << arr[i] << "\t";
	cout << endl;
	return;
}
//计算平均成绩
void arr_average(const double arr[], const int size)
{
	double sum = 0;
	for (int i = 0;i < size;i++)
	{
		sum += arr[i];
	}
	cout << "average: " << sum / size << endl;
	return ;
}

//------------------------作业三---------------
void box_value(box aa)
{
	for (int i = 0;aa.maker[i] != '\0';i++)
		cout << aa.maker[i];
	cout << endl;
	cout << aa.height << endl;
	cout << aa.width<< endl;
	cout << aa.length << endl;
	cout << aa.volume << endl;
	return;
}
void box_addr(box* aa)
{
	aa->volume = (aa->height) * (aa->length) * (aa->width);
	cout << endl << aa->volume << endl;
}
void box_inital(void)
{
	box apple = { "apple",2,2,2,0 };
	box_value(apple);
	box_addr(&apple);
	return;
}

//------------------作业四-----------------------------
long double probability(unsigned numbers, unsigned picks)
{
	long double result = 1.0;
	//long double n;
	unsigned n;
	unsigned p;
	//采用交替运算乘除,可以避免中间结果超出最大的浮点数
	for (n = numbers, p = picks;p > 0;n--, p--)
		result = result * n / p;
	return 1.0/result;
}

void lotto(void)
{
	//都是正数,因此可用unsigned
	long double result = 1.0;
	result = probability(47, 5) * probability(27, 1);
	cout << "result: " << result << endl;
	return ;
}

//-------------------------作业五------------------
//递归!!!
long Factorial(int n)
{
	while (n > 0)
	{
		if (n == 1||n==0)
			return 1;
		else
			return n* Factorial(n - 1);
	}
	return 0;//会提示不是所有控件都有返回值
}
void compute(void)
{
	cout << "Enter the num: ";
	int num = 0;
	while (!(cin >> num))
	{
		cin.clear();
		while (cin.get() != '\n')
			continue;
		cout << "Error,please enter again: ";
	}
	long  result = Factorial(num);
	cout << "reslut: " << result << endl;
	return;
}

//----------------------作业六--------------------------------
const int Size = 10;
int fill_array(double* arr, int* size)
{
	cout << "Enter the double: " << endl;
	for (int i = 0;i < Size;i++)
	{
		cout << " # " << i + 1 << " : ";
		if (!(cin >> arr[i]))
			break;
		*size = i+1;
	}	
	return *size;
}
void show_array(double* arr,int size)
{
	for (int i = 0;i < size;i++)
		cout << arr[i];
	return;
}
void reverse_array(double* arr, int size)
{
	int i, j;
	double temp = 0.0;
	for (i = 0, j = size - 1;i < j;i++, j--)
	{
		temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	return;
}

void array_plus()
{
	double arr[Size];
	int num = 0;
	num = fill_array(arr, &num);
	show_array(arr, num);
	reverse_array(arr, num);
	cout << "------------------" << endl;
	show_array(arr, num);
	return;
}


//-------------------------------作业七--------------------------
//填充数组
const int Max = 5;
double* fill_array(double* begin)
{
	cout << "Enter the number: " << endl;
	int i = 0;
	double* ps;
	for (ps = begin,i=0;ps != begin+Max;ps++,i++)
	{
		cout << " # " << i + 1 << " : ";
		if (!(cin >> *ps))
			break;
	}
	return ps;
}
//输出数组
void show_array(double* begin,double* end)
{
	for (double* ps = begin;ps != end;ps++)
		cout << *ps << "\t";
	cout << endl;
	return;
}
//重新赋值
void revalue(double* begin, double* end,int n)
{
	for (double* ps = begin;ps != end;ps++)
		*ps = n;
	cout << endl;
	return;
}
void house(void)
{
	double arr[Max];
	double* begin = arr;
	double* end = fill_array(begin);
	show_array(begin, end);
	revalue(begin, end, 2);
	show_array(begin, end);

	return;
}


//-------------------作业八----------------------------------
//a.double数组存储开支
const int Seasons = 4;
const char* Snames[Seasons] = {"Spring","Summer","Fall","Winter"};

void fill_1(double* arr)
{
	for (int i = 0;i < Seasons;i++)
	{
		cout << Snames[i] << " : ";
		cin >> arr[i];
	}
	return;
}
void show_1(double* arr)
{
	double total = 0.0;
	for (int i = 0;i < Seasons;i++)
	{
		cout << Snames[i] << " : " << arr[i] << endl;
		total += arr[i];
	}
	cout << "total: " << total << endl;
	return;
}
void season_1(void)
{
	double array[Seasons];
	fill_1(array);
	show_1(array);
	return;
}

//b.结构,成员是存储开支的double数组
struct expense
{
	double arr[Seasons];
};
void fill_2(expense* ps)
{
	for (int i = 0;i < Seasons;i++)
	{
		cout << Snames[i] << " : ";
		cin >> ps->arr[i];
	}
	return;
}
void show_2(expense ps)
{
	double total=0.0;
	for (int i = 0;i < Seasons;i++)
	{
		cout << Snames[i] << " : " << ps.arr[i] << endl;
		total += ps.arr[i];
	}
	cout << "total: " << total << endl;
	return;
}
void season_2(void)
{
	struct expense exp;
	fill_2(&exp);
	show_2(exp);
	return;
}


//---------------------------作业九----------------
const int SLEN = 30;
struct student
{
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};

int getinfo(student pa[], int n)//结构数组
{
	int i = 0;
	for (i = 0;i < n;i++)
	{
		cout << " # " << i + 1 << " : " << endl;
		cout << "Enter the fullname: ";
		cin.getline(pa[i].fullname,SLEN);
		if (pa[i].fullname == "\n")
			break;
		cout << "Enter the hobby: ";
		cin.getline(pa[i].hobby, SLEN);
		cout << "Enter the ooplevel: ";
		cin>>pa[i].ooplevel ;
		while (cin.get() != '\n')
			continue;
	}
	return i;
}
void display1(student st)
{
	cout << "--------------" << endl;
	cout << "fullname:\t" << st.fullname << "\thobby:\t" << st.hobby
		<< "\toopleavel:\t" << st.ooplevel << endl;
}
void display2(const student* ps)
{
	cout << "--------------" << endl;
	cout << "fullname:\t" << ps->fullname << "\thobby:\t" << ps->hobby
		<< "\toopleavel:\t" << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
	for (int i = 0;i < n;i++)
	{
		cout << "--------------" << endl;
		cout << "fullname:\t" << pa[i].fullname << "\thobby:\t" << pa[i].hobby
			<< "\toopleavel:\t" << pa[i].ooplevel << endl;
	}
}

void excer(void)
{
	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++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete[] ptr_stu;
	cout << "Done\n";
	return;
}


//--------------------作业十---------------------
double add(double x, double y)
{
	return x + y;
}
double mul(double x, double y)
{
	return x * y;
}
double calculate(double x, double y, double (*ps)(double, double))
{	
	return ps(x, y);
}
void cal_plus(void)
{
	cout << "Enter the two numbers:";
	double x, y;
	//创建函数指针数组
	double (*ps[2]) (double, double) = { add,mul};
	double result[2];
	while (cin >> x >> y)
	{
		for (int i = 0;i < 2;i++)
		{
			result[i]=calculate(x, y, ps[i]);
		}
		cout << "x+y=" << result[0] << endl;
		cout << "x*y= " << result[1] << endl;
		cout << "Enter the two numbers:";
	}
	return;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-01-28 11:42:41  更:2022-01-28 11:43:43 
 
开发: 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年11日历 -2024/11/24 9:48:36-

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