C++ Primer Pluse_7_课后题

#include <iostream>

using namespace std;

double Sum2(double x, double y)
{
	double sum = 0;

	if (x + y < 0.0000000001)
	{
		cout << "x, y 的调和数为无穷大;\n";
		system("pause");
		exit(0);
	}
	sum = 2.0*x*y / (x + y);
	return sum;
}

void test7_1()
{
	double x;
	double y;
	double sum = 0;

	while ((cin>>x>>y) && x != 0 && y!=0)
	{
		sum = Sum2(x, y);
		cout << "x,y的调和为: " << sum << endl;
	}
}

/************************************************************************/
/* 2                                                                     */
/************************************************************************/
const int SIZE = 10;
int GetScores(double scores [],int *size)
{
	cout << "Enter the scores( press q to quit).\n";
	int i = 0;
	for (i = 0; i < 10 && cin >> scores[i]; i++)
	{
		if (scores[i]<0)
		{
			cout << "score below zero, bad input.\n";
			cout << "Input terminated.\n";
			return -1;
		}
	}

	*size = i;
	return 0;
}

void Show(const double * scores, int size)
{
	int i = 0;
	for (i = 0; i < size; i++)
	{
		cout << scores[i] << " ";
	}
	cout << endl;
}

double Average(const double *scores, int size)
{
	double sum = 0;

	if (size == 0)
	{
		return 0;
	}
	for (int i = 0; i < size; i ++)
	{
		sum += scores[i];
	}

	return sum/size;
}
void test7_2()
{
	int size = 0;
	double scores[SIZE] = { 0 };

	int ret = 0;
	ret = GetScores(scores, &size);
	while (ret != 0)
	{
		cout << "Please input again.\n";
		ret = GetScores(scores, &size);
	}

	Show(scores, size);
	double avg = Average(scores, size);
	cout << "平均成绩为:" << avg << endl;

}

/************************************************************************/
/* 3                                                                     */
/************************************************************************/
typedef struct Box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
}box;

void ShowBox(box b1)
{
	cout << "Showing the box......\n";
	cout << b1.maker << endl;
	cout << b1.height << endl;
	cout << b1.width << endl;
	cout << b1.length << endl;
	cout << b1.volume << endl;
}

void GetVol(box * b1)
{
	b1->volume = b1->height *b1->length*b1->width;
}
void test7_3()
{
	box b1 = { "Cat", 10, 2, 3, 0 };
	ShowBox(b1);
	GetVol(&b1);
	ShowBox(b1);
}

/************************************************************************/
/*   4                                                                   */
/************************************************************************/

void test7_4()
{

}
/************************************************************************/
/* 5                                                                     */
/************************************************************************/
long GetFactorial(int n)
{
	if (n == 0 || n == 1)
	{
		return 1;
	}
	else
		return n*GetFactorial(n - 1);

}
void test7_5()
{
	int n = 0;
	cin >> n;
	if (n < 0)
	{
		cout << "bad input.\nprogram terminated.\n";
		return;
	}

	cout << n << "! = " << GetFactorial(n)<<endl;

}

void test7_6()
{

}

/************************************************************************/
/* 7                                                                     */
/************************************************************************/
const int Max = 5;
double * full_array(double ar[], int limit)
{
	double *ar_end = ar;
	double tmp = 0;
	for (int i = 0; i < limit; i++, ar_end++)
	{
		cout << "Enter value #" << (i + 1) << ": ";
		cin >> tmp;

		if (!cin)
		{
			cin.clear();
			while (cin.get()!=‘\n‘)
			{
				continue;
			}

			cout << "Bad input;input process terminated.\n";
			break;
		}
		else if (tmp < 0)
		{
			break;
		}
		*ar_end = tmp;
	}

	return --ar_end;
}

void show_array(double ar[], const double *ar_end)
{
	double *pt = ar;
	for (int i = 0; pt <= ar_end; pt++, i++)
	{
		cout << "Property #" << (i + 1) << ": $";
		cout << *pt << endl;
	}
}

void revalue(double r, double *ar, double *ar_end)
{
	double *pt = ar;
	for (; pt <= ar_end ; pt++)
	{
		*pt *= r;

	}
}

void test7_7()
{
	double properties[Max];
	double *ar_end = NULL;
	ar_end = full_array(properties, Max);
	show_array(properties,ar_end);
	if (ar_end - properties > 0)
	{
		cout << "Enter revaluation factor: ";
		double factor;
		while (!(cin >> factor))
		{
			cin.clear();
			while (cin.get()!= ‘\n‘)
			{
				continue;
			}
			cout << "Bad input. input process terminated.";
		}
		revalue(factor, properties, ar_end);
		show_array(properties, ar_end);
	}
	cout << "Done.\n";
	cin.get();
	cin.get();
}

/************************************************************************/
/* 8                                                                     */
/************************************************************************/
const int Seasons = 4;
const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
void fill(double expenses[], int Seasons)
{
	for (int i = 0; i < Seasons; i++)
	{
		cout << "Enter " << Snames[i] << " expense: ";
		cin >> expenses[i];
	}
}

void show(double expense[], int Seasons)
{
	double total = 0;
	cout << "EXPENSE\n";
	for (int i = 0; i < Seasons; i++)
	{
		cout << Snames[i] << ": $" << expense[i] << endl;
		total += expense[i];
	}

	cout << "Total Expense: $" << total << endl;

}
void test7_8()
{
	double expense[Seasons] = { 0 };
	fill(expense, Seasons);
	show(expense, Seasons);
}

/************************************************************************/
/* 9                                                                     */
/************************************************************************/
const int Size = 30;
typedef struct Student{

	char fullname[Size];
	char hobby[Size];
	int ooplevel;

}student;

int getInfo(student pa[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		cout << "Student #" << i + 1 << ":\n";
		cout << "Enter name: ";
		cin >> pa[i].fullname;
		if (!cin)
		{
			cin.clear();
			while (cin.get()!= ‘\n‘)
			{
				continue;
			}
			cout << "bad input. input process terminated.\n";
		}

		cout << "Enter hobby:";
		cin >> pa[i].hobby;
		cout << "Enter ooplevel:";
		cin >> pa[i].ooplevel;
	}

	return i;
}

void display1(student st)
{
	cout << "Student info:\n";
	cout << "name: " << st.fullname << endl;
	cout << "hobby: " << st.hobby << endl;
	cout << "ooplevel" << st.ooplevel << endl;
}

void display2(student *ps)
{
	cout << "Student info:\n";
	cout << "name: " << ps->fullname << endl;
	cout << "hobby: " << ps->hobby << endl;
	cout << "ooplevel" << ps->ooplevel << endl;
}

void display3(const student pa[], int n)
{
	int i = 0;
	for (; i < n; i++)
	{
		cout << "Student #" << i + 1<< "info:"<< endl;
		cout << "name: " << pa[i].fullname << endl;
		cout << "hobby: " << pa[i].hobby << endl;
		cout << "ooplevel" << (pa+i)->ooplevel << endl;
	}
}

void test7_9()
{
	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";
}
double add(double x, double y)
{
	return x + y;
}

double sub(double x, double y)
{
	return x - y;
}

double mul(double x, double y)
{
	return x*y;
}

void test7_10()
{
	double(*calculate[3])(double, double);

	calculate[0] = add;
	calculate[1] = sub;
	calculate[2] = mul;
	for (int i = 0; i < 3; i++)
	{
		cout << calculate[i] << ": "<<calculate[i](5, 2) << endl;
	}
}
int main()
{
	test7_10();
	system("pause");
	return 0;
}

  

时间: 2024-10-09 06:43:51

C++ Primer Pluse_7_课后题的相关文章

C++ Primer Pluse_8_课后题

#include <iostream> #include <string> #include<cstring> using namespace std; void showStr(const char * str, int & n) { cout << str << endl; if (n > 0) { showStr(str, --n); } } int test8_1() { char str[] = "hello c

C++ Primer Pluse_6_课后题

#include <iostream> #include <cctype> #include <array> #include <string> #include <cstring> #include <fstream> #include <cstdlib> using namespace std; int t6_1() { char ch; cout << "Enter text for analy

C++ Primer 第五版 部分课后题答案

当时刚学C++的时候买了这本书,一开始前面看的一知半解,索性就先缓缓,等学完学校的C++课程(中途自己也写了不少c++的代码),一段时间之后又拿起这本书去看,感觉还是挺有滋味的,这本书对我印象中的C++做了很大的扩展,个人认为这本书不太适合刚学C++就去看,而是写了一定的代码,对C++有一个大体的了解之后再去看会很有味道.在看书的过程中自己也写了上面的课后练习题,现在整理一下,也跟大家分享一下,下面是9~12 15~16章的课后题编程题的答案 (第八章之前的都没保存/(ㄒoㄒ)/~~): 当时保

《算法导论》读书笔记--第三章函数的增长 课后题

本章的课后题看一下即可,比较平凡. 3.1渐近记号 引用一下别人的答案,非常感谢: 原文地址:http://www.cnblogs.com/timebug/archive/2010/03/25/1694286.html |概念回顾| 当输入规模大到使只有运行时间的增长量级有关时,就使在研究算法的渐进效率. 几个重要渐进记号的定义: Θ(g(n))={ f(n): 存在正常数c1,c2和n0,使对所有的n>=n0,有0<=c1g(n)<=f(n)<=c2g(n) } O(g(n))=

课后题3,4

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 练习2 { class Program { static void Main(string[] args) { //输入3个数,将这三个数从大到小排列打印出来: Console.Write("请输入第一个数:"); int a =Convert .ToInt32 (Console.ReadLine

[Python]小甲鱼Python视频第030课(文件系统:介绍一个高大上的东西)课后题及参考解答

# -*- coding: utf-8 -*- """ Created on Fri Mar 8 15:49:32 2019 @author: Administrator """ """ 动动手: 0. 编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图: 1. 编写一个程序,计算当前文件夹下所有文件的大小,程序实现如图 2. 编写一个程序,用户输入文件名以及开始搜索的路径,搜索该文件是否存在.如遇到文件

c++面向对象程序设计 课后题 答案 谭浩强 第四章

c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: double real; double imag;

郭懋正《实变函数与泛函分析》课后题答案

LATEX 精美排版,郭懋正<实变函数与泛函分析课后题答案>,每一题均有详细解答,120元一份,也可单独购买每一题,一题2元,付款后联系邮箱:                    [email protected] . 原文地址:https://www.cnblogs.com/zhangwenbiao/p/12109194.html

《数据库系统概论(第5版)》课后习答案 王珊、萨师煊编著版 课后题解析 高等教育出版社出版 答

<数据库系统概论(第5版)>课后习答案 王珊.萨师煊编著版 课后题解析 高等教育出版社出版 答案与解析 <数据库系统概论(第5版)> 王珊.萨师煊编著版 第二篇 第1章 课后答案与解析 完整答案在页面最下方 前言第一篇 基 础 篇 课后习题答案与解析第1章 绪论 课后习题答案与解析1.1 数据库系统概述1.2 数据模型1.3 数据库系统的结构1.4 数据库系统的组成1.5 小结习题本章参考文献第2章 关系数据库 课后习题答案与解析2.1 关系数据结构及形式化定义2.2 关系操作2.