C++实例——第三章

例3-1 编写一个求x的n次方的函数

//3_1.cpp
#include <iostream>
using namespace std;

//计算x的n次方
double power (double x,int n){
	double val=1.0
	while(n--)
		val *= x;
	return val;
	}

	int main(){
		cout<<"5 to the power 2 is"<<power(5,2)<<endl;
		//函数调用作为一个表达式出现在输出语句中
		return 0;
	}

例3-2 输入一个8位二进制数,将其转换为十进制数输出。

//3_2.cpp
#include <iostream>
using namespace std;

//计算x的n次方
double power(double x,int n);   //调用3-1中的函数power

int main(){
	int value=0;

	cout<<"Enter an 8 bit binary number:";
	for (int i=7;i>=0;i--){
		char ch;
		cin>>ch;
		if (ch=='1')
			value+=static_cast<int>(power(2,i));
	}
	cout<<"Decimal value is"<<value<<endl;
	return 0;
	}

	double power (double x,int n){
		double val=1.0;
		while (n--)
			val *= x;
		return val;
	}

例3-3 编写程序求 π \pi π的值,公式如下。
π = 16 a r c t a n ( 1 5 ) − 4 a r c t a n ( 1 239 ) \pi = 16arctan(\frac{1}{5})-4arctan(\frac{1}{239}) π=16arctan(51)4arctan(2391)
其中 a r c t a n arctan arctan用如下形式的级数计算:
a r c t a n x = x − x 3 3 + x 5 5 − x 7 7 + . . . arctanx = x-\frac{x^{3}}{3}+\frac{x^{5}}{5}-\frac{x^{7}}{7}+... arctanx=x3x3+5x57x7+...
直到级数某项绝对值不大于 1 0 − 15 10^{-15} 1015为止; π \pi π x x x均为double型。

//3_3.cpp
#include <iostream>
using namespace std;

double arctan(double x){
	double sqr=x*x;
	double e=x;
	double r=0;
	int i=1;
	while (e/i>1e-15){
		double f=e/i;
		r=(i%4==1)?r+f:r-f;
		e=e*sqr;
		i+=2;
	}
	return r;
}

int main(){
	double a=16.0*arctan(1/5.0);
	double b=4.0*arctan(1/239.0);
	//注意:因为整数相除结果取整,如果参数写为1/5,1/2,结果就都是0
	cout<<"PI="<<a-b<<endl;
	return 0;
}

例3-4 寻找并输出11~999之间的数m,它满足 m , m 2 m,m^{2} m,m2 m 3 m^{3} m3均为回文数。

//3_4.cpp
#include <iostream>
using namespace std;

//判断n是否为回文数
bool symn(unsigned n){
	unsigned i=n;
	unsigned m=0;
	while (i>0){
		m=m*10+i%10;
		i/=10;
	}
	return m==n;
}

int main(){
	for (unsigned m=11;m<1000;m++)
		if (symm(m) && symm(m*m) && symm(m*m*m)){
			cout<<"m="<<m;
			cout<<"  m*m="<<m*m;
			cout<<"  m*m*m="<<m*m*m<<endl;
		}
	return 0;
}

例3-5 计算如下公式,并输出结果
在这里插入图片描述
其中r,s的值由键盘输入。sinx的近似值按如下公式计算:
在这里插入图片描述
计算精度为 1 0 − 6 10^{-6} 106,当某项的绝对值小于计算精度时,停止累加,累加和即为 s i n x sinx sinx的近似值。

//3_5.cpp
#include <iostream>
#include <cmath> //头文件cmath中具有对C++标准库中数学函数的说明
using namespace std;

const_duble TINY_VALUE=1e-10;
double tsin(double x){
	double g=0;
	double t=x;
	int n=1;
	do{
		g+=t;
		n++;
		t=-t*x*x/(2*n-1)/(2*n-2);
	}while (fabs(t)>=YINY_VALUE);
	return g;
	}

	int main(){
		double k,r,s;
		cout<<"r=";
		cin>>r;
		cout<<"s=";
		cin>>s;
		if (r*r<=s*s)
			k=sqrt(tsin(r)*tsin(r)+tsin(s)*tsin(s));
		else
			k=tsin(r*s)/2;
		cout<<k<<endl;
		return 0;
	}

例3-6 投骰子的随机游戏

  • 游戏规则是:每个骰子有6面,点数分别为1,2,3,4,5,6。游戏者在程序开始时输入一个无符号整数,作为产生随机数的种子。
  • 每轮投两次骰子,第一轮如果和数为7或11则为胜,游戏结束;和数为2,3或12则为负,游戏结束;和数为其他值则将此值作为自己的点数,继续第二轮、第三轮…直到某轮的和数等于点数则取胜,若在此前出现和数为7则为负。
  • 由rollDice函数负责模和数拟投骰子、计算和数并输出和数。
//3_6.cpp
#include <iostream>
#include <cstdlib>
using namespace std;

//投骰子,计算和数,输出和数
int rollDice(){
	int die1=1+rand()%6;
	int die2=1+rand()%6;
	int sum=die1+die2;
	cout<<"player rolled"<<diel<<"+",<die2<<"="<<sum<<endl;
	return sum;
}

enum GameStatus {WIN,LOSE,PLAYING};

int main()[
	int sum,myPoint;
	GameStatus status;

	unsigned seed;
	cout<<"Please enter an unsigned integer:";
	cin>>seed;
	srand(seed);

	sum=rollDice();
	switch(sum){
	case 7;
	case 11;
		status=WIN;
		break;
	case 2:
	case 3:
	case 12:
		status=LOSE;
		break;
	default:
		status=PLAYING;
		myPoint=sum;
		cout<<"point is"<<myPoin<<endl;
		break;
	}

	while (status==PLAYING){
		sum=rollDice();
		if (sum==myPoint)
			status=WIN;
		else if (sum==7)
			status=LOSE;
	}

	//当状态不为PLAYING时上面的循环结束,以下程序段输出游戏结果
	if (status==WIN)
		cout<<"player wins"<<endl;
	else
		cout<<"player loses"<<endl;
	return 0;
}

例3-7 输入两个整数,求它们的平方和。

//3_7.cpp
#include <iostream>
using namespace std;

int fun2(int m){
	return m*m
}

int fun1(int x,int y){
	return fun2(x)+fun2(y)
}

int main(){
	int a,b;
	cout<<"Please enter two integers (a and b):";
	cin>>a>>b;
	cout<<"The sum of square of a and b:"<<fun1(a,b)<<endl;
	return 0;
}

例3-8 求n!

//3_8.cpp
#include <iostream>
using namespace std;

//计算n的阶乘
unsigned fac(unsigned n){
	unsigned f;
	if (n==0)
		f=1;
	else
		f=fac(n-1)*n;
	return f;
}

int main(){
	unsigned n;
	cout<<"Enter a positive integer:";
	cin>>n;
	unsigned y=fac(n);
	cout<<n<<"!="<<y<<endl;
	return 0;
}

例3-9 用递归法计算从n个人中选择k个人组成一个委员会的不同组合数。

//3_9.cpp
#include <iostream>
using namespace std;

//计算从n个人里选k个人的组合数
int comm(int n,int k){
	if (k>n)
		return 0;
	else if (n==k||k==0)
		return 1;
	else
		return comm(n-1,k)+comm(n-1,k-1);
}

int main(){
	int n,k;
	cout<<"Please enter two integers n and k:";
	cin>>n>>k;
	cout<<"C(n,k)="<<comm(n,k)<<endl;
	return 0;
}

例3-10 汉诺塔问题。

//3_10.cpp
#include <iostream>
using namespace std;

//把src针的最上面一个盘子移动到dest针上
void move(char src,char dest){
	cout<<src<<"-->"<<dest<<endl;
}

//把n个盘子从src针移动到dest针,以medium针作为中介
void hanoi(int n,char src,char medium,char dest){
	if (n==1)
		move(src,dest);
	else{
		hanoi(n-1,src,dest,medium);
		move(src,dest);
		hanoi(n-1,medium,src,dest);
	}
}

int main(){
	int m;
	cout<<"Enter the number of diskes:";
	cin>>m;
	cout<<"the steps to moving"<<m<<"diskes"<<endl;
	hanoi(m,'A','B','C');
	return 0;
}

例3-11 将两个整数交换次序后输出。

//3_11.cpp
#include <iostream>
using namespace std;

void swap(int a ,int b){
	int t=a;
	a=b;
	b=t;
}

int main(){
	int x=5,y=10;
	cout<<"x="<<x<<"    y="<<y<<endl;
	swap(x,y);
	cout<<"x="<<x<<"    y="<<y<<endl;
	return 0;
}

例3-12 使用引用传递改写例3-11的程序,使两整数成功地进行交换。

//3_12.cpp
#include <iostream>
using namespace std;

void swap(int &a,int &b){
	int t=a;
	a=b;
	b=t;
}

int main(){
	int x=5,y=10;
	cout<<"x="<<x<<"    y="<<y<<endl;
	swap(x,y);
	cout<<"x="<<x<<"    y="<<y,<endl;
	return 0;
}

例3-13 值传递与引用传递的比较

//3_13.cpp
#include <iostream>
#include <iomanip>
using namespace std;

void fiddle(int in1,int &in2){
	in1=in1+100;
	in2=in2+100;
	cout<<"The values are";
	cout<<setw(5)<<in1;
	cout<<setw(5)<<in2<<endl;
}

int main(){
	int v1=7, v2=12;
	cout<<"The values are";
	cout<<setw(5)<<v1;
	cout<<setw(5)<<v2<<endl;
	fiddle(v1,v2);
	cout<<"The values are";
	cout<<setw(5)<<v1;
	cout<<setw(5)<<v2<<endl;
	return 0;
}

例3-14 内联函数应用举例。

//3_14.cpp
#include <iostream>
using namespace std;

const double PI=3.14159265358979;

//内联函数,根据圆的半径计算其面积
inline double calArea(double radius){
	return PI*radius*radius;
}

int main(){
	double r=3.0;
	//调用内联函数求圆的面积
	double area=calArea(r);
	cout<<area<<endl;
	return 0;
}

例3-15 带默认形参值的函数举例。

//3_15.cpp
#include <iostream>
#include <iomanip>
using namespace std;

int getVolume(int length,int width=2,int height=3);

int main(){
	const int X=10, Y=12, Z=15;
	cout<<"Some box data is";
	cout<<getVolume(X,Y,Z)<<endl;
	cout<<"Some box data is";
	cout<<getVolume(X,Y)<<endl;
	cout<<"Some box data is";
	cout<<getVolume(X)<<endl;
	return 0;
}

int getVolume(int length,int width/*=2*/,int height/*=3*/){
	cout<<setw(5)<<length<<setw(5)<<width<<setw(5)<<height<<'\t';
	return length*width*height;
}

例3-16 重载函数应用举例。

//3_16.cpp
#include <iostream>
using namespace std;

int sumOfSquare(int a, int b){
	return a*a+b*b;
}

double sumOfSquare(double a, double b){
	return a*a=b*b;
}

int main(){
	int m,n;
	cout<<"Enter two integer:";
	cin>>m>>n;
	cout<<"Their sum of square:"<<sumOfSquare(m,n)<<endl;

	doubel x,y;
	cout<<"Enter two real number:";
	cin>>x>>y;
	cout<<"Their sum of square:"<<sumOfSquare(x,y)<<endl;

	return 0;
}

例3-17 系统函数应用举例

//3_17.cpp
#include <iostream>
#include <cmath>
using namespace std;

const double PI=3.1415925358979;

int main(){
	double angle;
	cout<<"Please enter an angle:";
	cin>>angle;

	double radian=angle*PI/180;
	cout<<"sin("<<angle<<")="<<sin(radian)<<endl;
	cout<<"cos("<<angle<<")="<<cos(radian)<<endl;
	cout<<"tan("<<angle<<")="<<tan(radian)<<endl;
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值