第3章函数
约 3251 字大约 11 分钟
2026-11-10
3-1 编写一个求x的n次方的函数
/*
3-1 P66
编写一个求x的n次方的函数
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//求x的n次方函数
double power(double x,int n){
double var=1.0;
while(n--)
var*=x;
return var;
}
//便于理解的等效函数
double myPower(double x,int n){
double var=1.0;
while(n){
var=var*x;
n--;
}
return var;
}
int main()
{
//输出5的2次方的结果
cout<<"5 to the power 2 is "<<power(5,2)<<endl;
cout<<"5 to the power 2 is "<<myPower(5,2)<<"(myPower)"<<endl;
system("pause");
return 0;
}3-2 输入一个8位二进制数,将其转换为十进制数输出
/*
3-2 P67
输入一个8位二进制数,将其转换为十进制数输出
每一位的数乘其相应的权,求和
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//计算x的n次方
double power(double x,int n);
int main()
{
//我的代码
// int value=0,bin,bit;
// cout<<"Please enter an 8 bit binary number:";
// cin>>bin;
// for(int i=0;i<=7;i++){
// bit=bin%10;//取末位
// value+=bit*power(2,i);//每位乘权,求和
// bin=bin/10;//去除末位
// }
// cout<<"Decimal value is "<<value<<endl;
//书上代码
int value=0;//对值进行初始化
cout<<"Enter an 8 bit binary number: ";
for(int i=7;i>=0;i--){
char ch;
cin>>ch;//接收一个字符
if(ch=='1') //若这个字符是1
value+=static_cast<int>(power(2,i));//静态投射类型转换
}
cout<<"Decimal value is "<<value<<endl;
system("pause");
return 0;
}
double power(double x,int n){
double var=1.0;
while(n--)
var*=x;
return var;
}3-3 编写公式求pi的值
/*
3-3 P67
编写公式求pi的值,公式如下:
pi=16*arctan(1/5)-4*arctan(1/239)
arctan用如下级数计算:
arctan(x)=x - (x^3)/3 + (x^5) -(x^7)/7 + ...
直到级数某项绝对值不大于10^(-15)为止;pi和x均为double型
每一位的数乘其相应的权,求和
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//计算arctan(x)
double arctan(double x);
double myArctan(double x);
int main()
{
double a=16.0*arctan(1/5.0);
double b=4.0*arctan(1/239.0);
cout<<"PI="<<a-b<<" arctan()"<<endl;
//注意:因为整数相除结果取整,如果参数写1/5,1.239,结果就是0
//我的函数
double mya=16.0*myArctan(1/5.0);
double myb=4.0*myArctan(1/239.0);
cout<<"PI="<<mya-myb<<" myArctan()"<<endl;
system("pause");
return 0;
}
double myArctan(double x){
double sqr=x*x;
double e=x;
double r=0;
int i=1;
int flag=1;//符号
while(e/i>1E-15)
{
double f=e/i;
r+=flag*f;
flag=-flag;//符号取反
e*=sqr;
i+=2;
}
return r;
}
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*=sqr;
i+=2;
}
return r;
}3-4 寻找并输出11~999的数m,它满足m、m^2和m三次方均为回文数。
/*
3-4 P68
寻找并输出11~999的数m,它满足m、m^2和m三次方均为回文数。
回文数:正着写和倒着写一样,例:121、676、94249
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//判断N是否为回文数
bool symn(unsigned N);
int main()
{
for(unsigned m=11;m<=999;m++){
if(symn(m) && symn(m*m) && symn(m*m*m)){
cout<<"m="<<m<<ends;
cout<<"m*m="<<m*m<<ends;
cout<<"m*m*m="<<m*m*m<<endl;
}
}
system("pause");
return 0;
}
bool symn(unsigned N){
unsigned i=N;//用i计算反转后的值
unsigned reverseN=0;//存储反转后的数值
while(i>0){
reverseN=i%10+reverseN*10;//乘10加个位数
i/=10;
}
return reverseN==N;
}3-5 计算公式并输出结果
/*
3-5 P69
计算公式并输出结果(公式太麻烦不想打了,看书吧)
*/
#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;
const double TINY_VALUE=1E-10;
//函数
double tsin(double x);
double funk(double r,double s);
int main()
{
double k,r,s;
//输入参数,计算
cout<<"r=";
cin>>r;
cout<<"s=";
cin>>s;
k=funk(r,s);
//输出结果
cout<<k<<endl;
system("pause");
return 0;
}
double tsin(double x){
double g=0;//返回值
double t=x;//第一项
int n=1;
do{
g+=t;//t是加了的最后一个
n++;
t=t*(-1)*x*x/(2*n-1)/(2*n-2);//第二项,第三项,第四项....
}while(fabs(t)>=TINY_VALUE);//没加的第一个
return g;
}
double funk(double r,double s){
if(r*r<=s*s){
return sqrt(tsin(r)*tsin(r)+tsin(s)*tsin(s));
}else{
return tsin(r*s)/2.0;
}
}3-6 掷骰子的随机游戏
/*
3-6 P70
掷骰子的随机游戏
一次投两个骰子,求和
第一次:
7、11胜。2、3、12负。其他情况记下点数为x,继续
之后:
若和数与x相同则胜;
若和数为7则失败。
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//掷骰子函数,计算和数,输出和数
int rollDice(){
int die1=1+rand()%6;
int die2=1+rand()%6;
int sum=die1+die2;
cout<<"player rolled "<<die1<<"+"<<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: //如果和数为7或11则为胜,状态为WIN
case 11:
status=WIN;
break;
case 2: //和数为2、3或12则为负,状态为LOSE
case 3:
case 12:
status=LOSE;
break;
default: //其他情况,游戏尚无结果,状态为PLAYING,记下点数,为下一轮做准备
status=PLAYING;
myPoint=sum;
cout<<"point is "<<myPoint<<endl;
}
while(status==PLAYING){ //只要状态仍未PLAYING,就继续进行下一轮
sum=rollDice();
if(sum==myPoint) //某轮的和数等于点数则取胜,状态置为WIN
status=WIN;
else if(sum==7) //出现和数为7则为负,状态置为LOSE
status=LOSE;
}
if(status==WIN)
cout<<"player wins"<<endl;
else
cout<<"player loses"<<endl;
system("pause");
return 0;
}3-7 输入两个整数,求他们的平方和。使用函数的嵌套实现。
/*
3-7 P73
输入两个整数,求他们的平方和。使用函数的嵌套实现。
在这里设计两个函数:求平方和函数fun1和求一个整数的平方函数fun2
*/
#include<iostream>
#include<stdlib.h>
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;
system("pause");
return 0;
}3-8 求n!阶乘
/*
3-8 P74
求n!
用递归的思想,将n!转化为n*(n-1)!,
最终的0!是1,是一个已经解决的问题。
之前的问题便可迎刃而解
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//求n的阶乘
unsigned fac(unsigned n){
unsigned f;
if(n==0) f=1;
else f=n*fac(n-1);
return f;
}
int main()
{
unsigned n;
cout<<"Enter a positive integer:";
cin>>n;
unsigned y=fac(n);//调用函数求解
cout<<n<<"!="<<y<<endl;
system("pause");
return 0;
}3-9 用递归法计算从n个人中选择k个人组成一个委员会的不同组合数
/*
3-9 P75
用递归法计算从n个人中选择k个人组成一个委员会的不同组合数
递归式:
C(n,k)=C(n-1,k)+C(n-1,k-1)
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//求n的阶乘
int comn(int n,int k){
if(n==k || k==0) return 1;
else return comn(n-1,k)+comn(n-1,k-1);
}
int main()
{
int n,k;
cout<<"Please enter twointegers n and k: ";
cin>>n>>k;
cout<<"C(n,k)="<<comn(n,k)<<endl;
system("pause");
return 0;
}3-10 汉诺塔问题
/*
3-10 P76
汉诺塔问题,将A针上的盘移到C针上,可借助B针,
注意:盘必须保持大的在下面,小的在上面。每次只可移动一个盘
================
1 | |
2 | |
3 | |
4 | |
5 | |
---------
A B C
================
分析:
*/
#include<iostream>
#include<stdlib.h>
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){//n头-中-尾
if(n==1)
move(src,dest);//1头-尾
else{
hanoi(n-1,src,dest,medium); //n-1头->中。将n-1个碍事的移到中转站
//第一行也是n-1阶的问题,但是从头->中
move(src,dest); //头->尾。碍事的没了,把最难处理的大盘直接移到目标盘
//大盘移好后,就相当于平地了,此时只考虑n-1阶的相同问题,1阶问题是可解的
hanoi(n-1,medium,src,dest); //n-1中->尾。最难移的大盘搞定了,还要考虑将n-1个盘子如何移到目标盘
//最后一行是n-1阶的问题,注意n-1个盘子存在的位置,头-中
}
}
int main()
{
//输入盘的个数
int m;
cout<<"Enter the number of desks:";
cin>>m;
//输出解决方案
cout<<"the steps to move "<<m<<"desks:"<<endl;
hanoi(m,'A','B','C');
system("pause");
return 0;
}3-11 将两个整数交换次序后输出
/*
3-11 P78
将两个整数交换次序后输出?(其实并没有交换)
原因:采用的是值传递,传递的是实参的值。
形参值的改变对实参不起作用
*/
#include<iostream>
#include<stdlib.h>
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;
system("pause");
return 0;
}3-12 使用引用传递修改3-11,将两个整数交换次序后输出
/*
3-12 P79
使用引用传递修改3-11,将两个整数交换次序后输出?
引用是一种特殊类型的变量,可以被认为是另一个变量的别名
*/
#include<iostream>
#include<stdlib.h>
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;
system("pause");
return 0;
}3-13 值传递和引用传递的比较
/*
3-13 P80
值传递和引用传递的比较
*/
#include<iostream>
#include<iomanip>
#include<stdlib.h>
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;
system("pause");
return 0;
}3-13-1 含有可变数量形参的函数
/*
3-13-1 P82
这个书上好像本来应该有例程,但没有出现在书上
含有可变数量形参的函数
initializer
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//形参为string
void print_err(initializer_list<string>lst){
for(auto beg=lst.begin();beg!=lst.end();++beg)
cout<<*beg<<' ';
cout<<endl;
}
//第二个形参为int
void print_err(initializer_list<string>lst , int err_code){
}
int main()
{
string expected="as";
string actual="asd";
//expected和actual是string对象
if(expected!=actual)
print_err({"return",actual,",",expected,"is","expected"});
else
print_err({"function","right"});
system("pause");
return 0;
}3-14 内联函数应用举例
/*
3-14 P83
内联函数应用举例
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
const double PI=3.14159265358979;
//内联函数,根据圆的半径计算其面积
inline double calArea(double radius){
return PI*radius*radius;
}
int main()
{
double r=3.0; //r是圆的面积
//调用内联函数求圆的面积,编译时此处被替换为calArea函数体语句
//展开为area=PI*radius*radius;
double area=calArea(r);
cout<<area<<endl;
system("pause");
return 0;
}3-15 带默认形参值的函数举例
/*
3-15 P85
带默认形参值的函数举例
本程序的功能是计算长方形的体积。子函数getVolume是计算体积的函数,
有3个形参:length(长)、width(宽)、height(高),其中width和height带有默认值。
主函数中以不同形式调用getVolume函数,分析程序的运行结果。
*/
#include<iostream>
#include<iomanip>
#include<stdlib.h>
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;
system("pause");
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 编写两个名为sumOfSquare的重载函数,分别求两整数的平方和及两实数的平方和。
/*
3-16 P87
编写两个名为sumOfSquare的重载函数,分别求两整数的平方和及两实数的平方和。
*/
#include<iostream>
#include<stdlib.h>
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 integers: ";
cin>>m>>n;
cout<<"Their sum of square: "<<sumOfSquare(m,n)<<endl;
double x,y;
cout<<"Enter two real numbers: ";
cin>>x>>y;
cout<<"Their sum of square: "<<sumOfSquare(x,y)<<endl;
system("pause");
return 0;
}3-17 从键盘输入一个角度值,求出该角度的正弦值、余弦值和正切值
/*
3-17 P88
从键盘输入一个角度值,求出该角度的正弦值、余弦值和正切值
*/
#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;
const double PI=3.14159265358979;
int main()
{
//输入角度
double angle;
cout<<"Please enter an angle: ";
cin>>angle;
//计算结果并输出
double redian=angle*PI/180;
cout<<"sin("<<angle<<")="<<sin(redian)<<endl;
cout<<"cos("<<angle<<")="<<cos(redian)<<endl;
cout<<"tan("<<angle<<")="<<tan(redian)<<endl;
system("pause");
return 0;
}