【学習C++】C++Primer Plus(第6版)第7章プログラミング練習6-10

7244 ワード

6.
#include <iostream>

using namespace std;
int Fill_array(double [], int size);
void Show_array(double [], int size);
void Reverse_array(double [], int size);
int main()
{
	int size,rsize;
	cin >> size;
	double *arr = new double [size];
	rsize = Fill_array(arr, size);
	Show_array(arr, rsize);
	Reverse_array(arr, rsize);
	Show_array(arr, rsize);
	cin.get();
	cin.get();
	return 0;
}
int Fill_array(double arr [], int size){
	int i;
	for (i = 0; i < size; i++)
	{
		cin >> arr[i];
		if (!cin)
			break;
	}
	return i;
}
void Show_array(double arr [], int size){
	for (int i = 0; i < size; i++){
		cout << "arr[" << i << "] = " << arr[i] << endl;
	}
	cout << "
"; } void Reverse_array(double arr [], int size){ double m; int begin, end; for (begin = 1, end = size - 2; begin < end; begin++, end--){ m = arr[begin]; arr[begin] = arr[end]; arr[end] = m; } }
7.
#include <iostream>
const int Max = 5;
using namespace std;
double * fill_array(double *begin, double *end);
void show_array(const double *begin,const double *end);
void revalue(double r, double *begin, double *end);
int main()
{
	double properties[Max];
	double *end = fill_array(properties, properties + Max);
	show_array(properties, end+1);
	if (end != properties){
		cout << "Enter revaluation factor: ";
		double factor;
		while (!(cin >> factor)){
			cin.clear();
			while (cin.get() != '
') continue; cout << "Bad input;Please enter a number: "; } revalue(factor, properties, end+1); show_array(properties, end+1); } cout << "Done.
"; cin.get(); cin.get(); return 0; } double * fill_array(double *begin, double *end){ double temp; double *m; int i; for (m = begin,i=0; m != end; m++,i++){ cout << "Enter value #" << (i + 1) << ": "; cin >> temp; if (!cin){ cin.clear(); while (cin.get() != '
') continue; cout << "Bad input;input process terminated.
"; break; } else if (temp < 0) break; *m = temp; } return m-1; } void show_array(const double *begin, const double *end){ int i; const double *m; for (m = begin, i = 0; m != end; m++, i++){ cout << "Property #" << (i + 1) << ": $"; cout << *m<<endl; } } void revalue(double r, double *begin, double *end){ double *m; for (m = begin; m != end; m++) *m = (*m)*r; }

8.a
#include <iostream>
using namespace std;
const int Seasons = 4;
const char *Snames[Seasons] = { "Spring", "summer", "Fall", "Winter" };
void fill(double pa[],int n);
void show(const double da[],int n);
int main()
{
	double expenses[Seasons];
	fill(expenses,Seasons);
	show(expenses,Seasons);
	cin.get();
	cin.get();
	return 0;
}
void fill(double pa[],int n){
	for (int i = 0; i < n; i++){
		cout << "Enter " << Snames[i] << " expenses:";
		cin >> pa[i];
	}
}
void show(const double da [], int n){
	double total = 0;
	cout << "EXPENSES
"; for (int i = 0; i < n; i++){ cout << Snames[i] << ": $" << da[i] << endl; total += da[i]; } cout << "Total Expenses : $" << total << endl; }

8.b
#include <iostream>
using namespace std;
const int Seasons = 4;
const char *Snames[Seasons] = { "Spring", "summer", "Fall", "Winter" };
struct Expenses{
	double exp[Seasons];
};
void fill(Expenses *);
void show(Expenses);
int main()
{
	Expenses a;
	fill(&a);
	show(a);
	cin.get();
	cin.get();
	return 0;
}
void fill(Expenses *a){
	for (int i = 0; i < Seasons; i++){
		cout << "Enter " << Snames[i] << " expenses:";
		cin >> a->exp[i];
	}
}
void show(Expenses a){
	double total = 0;
	cout << "EXPENSES
"; for (int i = 0; i < Seasons; i++){ cout << Snames[i] << ": $" << a.exp[i] << endl; total += a.exp[i]; } cout << "Total Expenses : $" << total << endl; }
9.
#include <iostream>
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(const student * ps);
void display3(const student pa [], int n);
int main()
{
	cout << "Enter class size: ";
	int class_size;
	cin >> class_size;
	while (cin.get() != '
') 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; cin.get(); cin.get(); return 0; } int getinfo(student pa [], int n){ int i; for (i = 0; i < n; i++){ cin.getline(pa[i].fullname, SLEN); if ((pa[i].fullname)[0] == '\0'){ break; } cin.getline(pa[i].hobby, SLEN); cin >> pa[i].ooplevel; cin.get(); } return i; } void display1(student st){ cout << "name: " << st.fullname<<endl; cout << "hobby: " << st.hobby<<endl; cout << "ooplevel: " << st.ooplevel << endl; } void display2(const student * ps){ cout << "name: " << ps->fullname<<endl; cout << "hobby: " << ps->hobby << endl; cout << "ooplevel: " << ps->ooplevel << endl; } void display3(const student pa [], int n){ int i; for (i = 0; i < n; i++){ cout << "name: " << pa[i].fullname << endl; cout << "hobby: " << pa[i].hobby << endl; cout << "ooplevel: " << pa[i].ooplevel << endl; } }
10.
#include <iostream>
using namespace std;
double add(double x, double y);
double max(double x, double y);
double min(double x, double y);
double calculate(double x, double y, double(*p)(double a, double b));
int main()
{
	double x,y;
	double q;
	double (*pf[3])(double, double) = {add,max,min};
	while (cin >> x >> y){
		for (int i = 0; i < 3; i++){
			q = calculate(x, y, *pf[i]);
			if (i==0)
				cout << "the sum of two numbers: " << q << endl;
			else if (i==1)
				cout << "the max number of the two numbers; " << q << endl;
			else if (i==2)
				cout << "the min number of the two numbers; " << q << endl;
		}
	}
	cin.get();
	cin.get();
	return 0;
}
double add(double x, double y){
	return x + y;
}
double calculate(double x, double y, double(*p)(double a, double b)){
	double m;
	m = (*p)(x, y);
	return m;
}
double max(double x, double y){
	return x > y ? x : y;
}
double min(double x, double y){
	return x < y ? x : y;
}