C++primer plus 第七章习题(自己写的,欢迎指正)

第七章
#include<iostream>
#include<stdlib.h>

//第七章
using namespace std;

double aver(const int *x, const int *y);
int main()
{
	int x, y;
	cout << "Please enter two figures:";
	cin >> x >> y;
	double res = aver(&x, &y);
	cout << "Your result is:" << res << endl;
	system("pause");
	return 0;
}


double aver(const int *x,const int *y)
{ 
	double result = 0;
	result = 2.0*(*x)*(*y) / (*x + *y);
	return result;
}





#include<iostream>
#include<stdlib.h>

//第七章
using namespace std;
const int Max = 10;

int fill(double arr[],int len);
double deal(double arr[], int len);
void display(double arr[], int len,double sum);
int main()
{
	double arr[Max];
	int x;
	double y;
	x = fill(arr, Max);
	y = deal(arr, x);
	display(arr, x, y);
	system("pause");
	return 0;
}

int fill(double arr[], int len)
{
	int i;
	double num;
	for (i = 0; i < len; i++)
	{
		cout << "Please enter your figures:";
		while (!(cin >> num))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input!" << endl;
			break;
		}
		if (num < 0)
		{
			break;
		}
		arr[i] = num;
	}
	return i;
}


double deal(double arr[], int len)
{
	double sum=0;
	for (int i = 0; i < len; i++)
	{
		sum += arr[i];
	}
	return sum;
}
void display(double arr[], int len, double sum)
{
	for (int i = 0; i < len; i++)
	{
		cout << "Grade:" << arr[i] << " ";
	}
	cout << endl;
	cout << "Total grade:" << sum << endl;
	cout << "Average grade:" << sum / len << endl;
}





#include<iostream>
#include<stdlib.h>

//第七章
using namespace std;
const int Max = 10;

struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
box fill();
void show(box b);
void fill(box *b);
void show(box *b);
int main()
{
	box a;
	a = fill();
	show(a);
	box c;
	/*fill(&c);
	show(&c);*/
	system("pause");
	return 0;
}
box fill()
{
	box b;
	cout << "Please enter your maker:";
	cin >> b.maker;
	cout << "Please enter your height:";
	cin >> b.height;
	cout << "Please enter your width:";
	cin >> b.width;
	cout << "Please enter your length:";
	cin >> b.length;
	cout << "Please enter your volume:";
	cin >> b.volume;
	return b;
}
void show(box b)
{
	cout << "Your maker:" << b.maker << endl;
	cout << "Your height:" << b.height << endl;
	cout << "Your width:" << b.width << endl;
	cout << "Your length:" << b.length << endl;
	cout << "Your volume:" << b.volume << endl;
	
}

void fill(box *b)
{
	cout << "Please enter your maker:";
	cin >> b->maker;
	cout << "Please enter your height:";
	cin >> b->height;
	cout << "Please enter your width:";
	cin >> b->width;
	cout << "Please enter your length:";
	cin >> b->length;
	cout << "Please enter your volume:";
	cin >> b->volume;

}
void show(box *b)
{
	cout << "Your maker:" << b->maker << endl;
	cout << "Your height:" << b->height << endl;
	cout << "Your width:" << b->width << endl;
	cout << "Your length:" << b->length << endl;
	cout << "Your volume:" << b->volume << endl;
}






#include<iostream>
#include<stdlib.h>
#define ui unsigned int
//第七章
using namespace std;
const int Max = 10;
struct area
{
	ui ar1;
	ui ar2;
};

void probiba(area ar1, area ar2);

int main()
{
	area are1, are2;
	cout << "Please enter first ar1 and ar2:";
	cin >> are1.ar1 >> are1.ar2;
	cout << "Please enter second ar1 and ar2:";
	cin >> are2.ar1 >> are2.ar2;
	probiba(are1, are2);
	system("pause");
	return 0;
}

void probiba(area are1, area are2)
{
	long double res1 = 1.0,res2=1.0;
	ui i, j;
	for (i = are1.ar1,j=are1.ar2; j>0; i--,j--)
	{
		res1 = res1*i/j;
	}
	cout << "pro1:" << res1 << endl;
	for (i = are2.ar1,j = are2.ar2; j>0; i--, j--)
	{
		res2 = res2*i / j;
	}
	cout << "pro2:" << res2 << endl;
	cout << "End pro:" << res1*res2 << endl;
}




#include<iostream>
#include<stdlib.h>
#define ui unsigned int
//第七章
using namespace std;

long long jiecheng(int x);
int main()
{
	int num;
	cout << "Please enter your num:";
	cin >> num;
	long long res=jiecheng(num);
	cout << "Result:" << res << endl;
	system("pause");
	return 0;
}
long long jiecheng(int x)
{
	if (x == 1 || x == 0)
		return 1;
	else
		return x*jiecheng(x-1);

}

#include<iostream>
#include<stdlib.h>
//第七章
const int Size = 10;
using namespace std;
int fillnum(double arry[], int size);
void show(double array[], int size);
void reserve(double array[], int size);
int main()
{
	double array[Size];
	int i = fillnum(array, Size);
	show(array, i);
	reserve(array, i);
	show(array, i);
	system("pause");
	return 0;
}

int fillnum(double arry[], int size)
{
	int i;
	for ( i = 0; i < size; i++)
	{
		cout << "Please enter your number:";
		if(!(cin >> arry[i]))
			break;
	}
	return i;
}
void show(double array[], int size)
{
	cout << "The number of array:";
	for (int i = 0; i < size; i++)
	{
		cout << array[i] << " ";
	}
	cout << endl;
}
void reserve(double array[], int size)
{
	int j;
	for (int i = 0,j=size-1; i < size/2; i++,j--)
	{
		double temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
}




#include<iostream>
#include<stdlib.h>
//第七章
const int Size = 5;
using namespace std;
double * fillnum(double arry[], int size);
void show(double array[], double *);
void revalue(double array[], double *,double r);
int main()
{
	double array[Size];
	double * i = fillnum(array, Size);
	show(array, i);
	double r;
	cout << "Please enter your r:";
	cin >> r;
	revalue(array, i,r);
	show(array, i);
	system("pause");
	return 0;
}

double* fillnum(double arry[], int size)
{
	int i;
	for (i = 0; i < size; i++)
	{
		cout << "Please enter your number:";
		if (!(cin >> arry[i]))
		{
			cin.get();
			break;
		}
		else if (arry[i] < 0)
		{
			cin.get();
			break;
		}
	}
	return &arry[i];
}
void show(double array[], double *end)
{
	cout << "The number of array:";
	double * i;
	for (i = array; i !=end; i++)
	{
		cout << *i << " ";
	}
	cout << endl;
}
void revalue(double array[], double *end,double r)
{
	double *i;
	for (i = array; i != end; i++)
	{
		(*i) *= r;
	}
}




#include<iostream>
#include<stdlib.h>
//第七章
const int Size = 4;
const char *seasons[4] = { "spring", "summer", "autumn", "winter" };
using namespace std;
double * fillnum(double arry[], int size);
void show(double array[], double *);
int z;
int main()
{
	double array[Size];
	double * i = fillnum(array, Size);
	show(array, i);
	system("pause");
	return 0;
}

double* fillnum(double arry[], int size)
{
	for (z = 0; z < size; z++)
	{
		cout << "Please enter "<<seasons[z]<<" expense:";
		if (!(cin >> arry[z]))
		{
			cin.get();
			break;
		}
		else if (arry[z] < 0)
		{
			cin.get();
			break;
		}
	}
	return &arry[z];
}
void show(double array[], double *end)
{
	double * i;
	int j = 0;
	for (i = array,j=0; i != end,j<z; i++,j++)
	{
		cout << seasons[j] << " espense:";
		cout << *i <<endl;
	}
}




#include<iostream>
#include<stdlib.h>
//第七章
const int SLEN = 30;
using namespace std;
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++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
		
	}
	display3(ptr_stu, entered);
	delete[]ptr_stu;
	cout << "Done\n";
	system("pause");
	return 0;
}


int getinfo(student pa[], int n)
{
	int i;
	for (i = 0; i < n; i++)
	{
		cout << "Please your name:";
		cin >> pa[i].fullname;
		if (pa[i].fullname == " ")
			break;
		cout << "Please your hobby:";
		cin >> pa[i].hobby;
		cout << "Please your opplevel:";
		cin >> pa[i].ooplevel;
	}
	return i;
}
void display1(student st)
{
	cout << "Your name is:"<<st.fullname<<endl;
	cout << "Your hobby is:" << st.hobby << endl;
	cout << "Your opplevel is:" << st.ooplevel << endl;
}
void display2(student *ps)
{
	cout << "Your name is:" << ps->fullname << endl;
	cout << "Your hobby is:" << ps->hobby << endl;
	cout << "Your opplevel is:" << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << "Your name is:" << (pa + i)->fullname << endl;
		cout << "Your hobby is:" << (pa + i)->hobby << endl;
		cout << "Your opplevel is:" << (pa + i)->ooplevel << endl;
	}
}




#include<iostream>
#include<stdlib.h>
//第七章
typedef double(*pf)(double, double);
using namespace std;
double calculate(double, double, pf);
double add(double, double);
double sub(double, double);
double mut(double, double);
double div(double, double);
int main()
{
	int num1, num2;
	pf ca[4]= {add,sub,div,mut};
	char *cal[] = { "add", "sub", "div", "mut" };
	cout << "Please enter two figures:";
	cin >> num1 >> num2;
	for (int i = 0; i < 3; i++)
	{
		double x = calculate(num1, num2,ca[i]);
		cout << cal[i] <<":"<< x << endl;
	}
	system("pause");
	return 0;
}
double calculate(double x, double y, pf ca)
{
	return (*ca)(x, y);
}
double add(double x, double y)
{
	return x + y;
}
double sub(double x, double y)
{
	return x - y;
}
double mut(double x, double y)
{
	return x*y;
}
double div(double x, double y)
{
	return x / y;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值