c++primerplus第七章编程练习

1.

#include<iostream>
using namespace std;
double average(double a, double b)
{
	return (2.0 * a * b / (a + b));
}
int main()
{
	double x, y;
	while (cin >> x >> y)
	{
		if (x == 0 || y == 0)
		{
			cout << "end";

			break;
		}
		else {
			cout << average(x, y);
		}
	}
	return 0;
}

2.

#include<iostream>
#include<string>
using namespace std;
int input(double a[])
{
	cout << "please input grades(most 10)(q to quit):";
	int i; double c;
	for ( i = 0; i < 10; i++)
	{
		cin >>	c;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')//读取无用输入
			{
				continue;
			}
			break;
		}
		a[i] = c;
	}
	return i;
}
void show(double b[],int n)
{
	for (int i = 0; i<n; i++)
	{
		cout << b[i]<<"  ";
	}
}
double av(double c[],int n)
{
	double average=0,sum=0;
	int i;
	for ( i = 0; i<n; i++)
	{
		sum += c[i];
	}
	average = sum/ i;
	return average;
}
int main()
{
	double a[15];
	int v;
	v=input(a);
	show(a,v);
	cout <<endl<< av(a,v);
	return 0;
}

3.

#include<iostream>
#include<string>
using namespace std;
struct box {
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
void show1(box a)
{
	cout << a.maker<<endl;
	cout << a.height << endl;
	cout << a.width << endl;
	cout << a.length << endl;
	cout << a.volume << endl;
}
void shou2(box* b)
{
	b->volume = b->height * b->width * b->length;
}
int main()
{
	box c = {
		"zhanglincong",
		1.2,
		1.2,
		1.2
	};
	shou2(&c);
	show1(c);
	return 0;
}

4.

#include<iostream>
#include<string>
using namespace std;
const int Num1 = 47;
const int Num2 = 27;
const int Bingo1 = 5;
const int Bingo2 = 1;
double cal(int num1, int num2, int bingo1, int bingo2)
{
    double pro, pro1, pro2;
    pro1 = double(bingo1) / double(num1);
    pro2 = double(bingo2) / double(num2);
    pro = pro1 * pro2;
    return pro;
}
int main()
{
    double a;
    a = cal(Num1, Num2, Bingo1, Bingo2);
    cout << a;
    return 0;
}

5.

#include<iostream>
using namespace std;
int a(int n)
{
	if (n == 0)
		return 1;
	if (n == 1)
		return 1;
	return a(n - 1) * n;
}
int main()
{
	int b;
	cin >> b;
	cout << a(b);
	return 0;
}

6.

#include<iostream>
#include<string>
using namespace std;
int fill_array(double a[], int n)
{
	cout << "input double :";
	int count = 0;
	for (int i = 0; i < n&&cin >> a[i]; i++)
		count++;
	return count;
}
void show_array(double a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << ' ';

	}
	cout << endl;
}
void reverse_array(double a[], int n)
{
	double temp;
	for (int i = 0; i < n / 2; i++)
	{
		temp = a[i];
		a[i] = a[n-1-i];
		a[n - 1-i] = temp;
	}
}
int main()
{
	const int size = 5;
	double a[size];
	int n;
	n=fill_array(a, size);
	show_array(a, n);
	reverse_array(a, n);
	show_array(a, n);
	return 0;
}

7.

#include<iostream>
const int MAX = 5;
using namespace std;
double* fill_array(double ar[], int limit)
{
	double temp;
	int i;
	for (i = 0; i < limit; i++)
	{
		cout << "Enter value #" << (i + 1) << ": ";
		cin >> temp;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input;input process terminated.\n";
			break;
		}
		else if (temp < 0)
			break;
		ar[i] = temp;
	}
	return (&ar[i]);
}
void show_array(const double ar[], double *n)
{
	for (int i = 0; ar+i!=n; i++)
	{
		cout << "Property #" << (i + 1) << ": $";
		cout << ar[i] << endl;
	}
}
void revalue(double r, double ar[], double* n)
{
	for (int i = 0; ar + i != n; i++)
	{
		ar[i] *= r;
	}
}
int main()
{
	double properties[MAX];
	double *n = fill_array(properties, MAX);
	show_array(properties, n);
	if (properties[0] > 0)
	{
		cout << "Enter revaluation factor: ";
		double factor;
		while (!(cin >> factor))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input;Please enter a number: ";

		}
		revalue(factor, properties, n);
		show_array(properties, n);
	}
	cout << "Done.\n";
	cin.get();
	cin.get();
	return 0;
}

8.

#include<iostream>
using namespace std;
const int seasons = 4;
const char* sname[seasons] = { "spring","summer","fall","winter" };
void fill(double *pa)
{
	for (int i = 0; i < seasons; i++)
	{
		cout << "Enter " << sname[i] << " expenses:";
		cin >> pa[i];
	}
}
void show(double* da)
{
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i = 0; i < seasons; i++)
	{
		cout << sname[i] << ": $" << da[i] << endl;
		total += da[i];
	}
	cout << "Total Expenses: $" << total << endl;
}
int main()
{
	double expenses[seasons];
	fill(expenses);
	show(expenses);
	return 0;
}

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)
{
	int count = 0;
	for (int i = 0; i < n; i++)
	{
		cout << "Enter fullname: ";
		cin.getline(pa[i].fullname, SLEN);
		cout << "Enter hobby: ";
		cin >> pa[i].hobby;
		cout << "Enter ooplevel: ";
		cin >> pa[i].ooplevel;
		cin.get();
		count++;
	}
	cout << "over!";
	return count;
}
void display1(student st)
{
	cout << "Fullname: " << st.fullname << "\n";
	cout << "Hobby: " << st.hobby << "\n";
	cout << "Ooplevel: " << st.ooplevel << "\n";
}
void display2(const student* ps)
{
	cout << "Fullname: " << ps->fullname << "\n";
	cout << "Hobby: " << ps->hobby << "\n";
	cout << "Ooplevel: " << ps->ooplevel << "\n";
}
void display3(const student pa[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << "Fullname: " << pa[i].fullname << "\n";
		cout << "Hobby: " << pa[i].hobby << "\n";
		cout << "Ooplevel: " << pa[i].ooplevel << "\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++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete[] ptr_stu;
	cout << "Done\n";
	return 0;
}

10.

#include<iostream>
using namespace std;
double calculate(double a, double b, double (*c)(double x, double y))
{
	return (*c)(a, b);
}
double add(double x, double y)
{
	return x + y;
}
int main()
{
	double a, b;
	while (cin >> a && cin >> b)
	{
		cout<<calculate(a, b, add);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值