第3章课后练习
约 2786 字大约 9 分钟
2026-11-10
习题3-01 P96
C++中的函数是什么?什么叫主调函数?什么叫被调函数?二者之间有什么关系?如何调用一个函数?
- 在面向对象的结构化程序设计过程中,函数是模块划分的基本单位,是对处理问题过程的一种抽象。通常将相对独立的、经常使用的功能抽象为函数。
- 调用其他函数的函数被称为主调函数,被其他函数调用的函数被称为被调函数。一个函数很可能既调用别的函数又被另外的函数调用,这样它在某一个调用与被调用关系中充当主调函数,而在另一个调用与被调用函数关系中充当被调函数。
- 函数在调用前需要声明。通过指明函数名和参数,来调用函数。
- 调用函数的过程:调用一个函数时,首先计算函数实参列表中各个表达式的值,然后主调函数暂停执行,开始执行被调函数,被调函数中形参的初值就是主调函数中实参表达式的求值结果。当被调函数执行到return语句,或执行到函数末尾时,被调函数执行完毕,继续执行主调函数。
习题3-2 P96
/*
习题3-2 P96
观察下面程序的运行输出,与你设想的有何不同?仔细体会引用的用法
输出:
intOne: 5
rSomeRef: 5
intOne: 8
intTwo: 8
rSomeRef: 8
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int intOne;
int &rSomeRef=intOne;
intOne=5;
cout<<"intOne:\t"<<intOne<<endl;
cout<<"rSomeRef:\t"<<rSomeRef<<endl;
int intTwo=8;
rSomeRef=intTwo;
cout<<"\nintOne:\t"<<intOne<<endl;
cout<<"intTwo:\t"<<intTwo<<endl;
cout<<"rSomeRef:\t"<<rSomeRef<<endl;
system("pause");
return 0;
}习题3-03 P96
比较值传递和引用传递的相同点和不同点。
- 同:他们都可以用实参初始化形参
- 不同:值传递是参数值的单向传递过程,函数中形参的改变无法影响实参。引用传递在函数中对形参的修改会对实参生效。引用传递是一种特殊类型的变量,可以被认为是另一个变量的别名。
习题3-04 P96
什么叫内联函数?他有什么特点?
- 程序设计时可将功能简单,规模较小又使用频繁的函数设计为内联函数。
- 内联函数不是在调用时发生控制转移,而是在编译时将函数体嵌入在每一个调用处。
- 这样做可节省参数转移、控制转移等开销。
习题3-05 P96
函数原型中的参数名与函数定义中的参数名以及函数调用中的参数名必须一致吗?
- 不需要,参数是根据位置和类型而不是名字而划分的。
习题3-06 P96
调用被重载的函数时,通过什么来区分被调用的是哪个函数?
- 通过形参的类型和数量
- 同名的重载函数其形参类型或个数必须不同
习题3-7 P97
/*
习题3-7 P97
完成函数,参数为两个unsigned short int型数,返回值为第一个参数除以第二个参数的结果,数据类型为short int;
如果第二个参数为0,则返回值为-1.在主程序中实现输入输出。
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//函数
short int myfun(unsigned short int a,unsigned short int b){
if(b==0) return -1;
return a/b;
}
int main()
{
unsigned short int a,b;
cout<<"Pleasr enter two integers(a and b): "<<endl;
cin>>a>>b;
cout<<"ans is "<<myfun(a,b)<<endl;
system("pause");
return 0;
}习题3-8 P97
/*
习题3-8 P97
编写函数把华氏温度转换为摄氏温度,公式:
C=5*(F-32)/9
在主程序中提示用户输入一个华氏温度,转化后输出相应的摄氏温度。
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//华氏温度转换为摄氏温度函数
double CtoF(double F){
return 5*(F-32)/9;
}
int main()
{
//输入华氏温度
double F;
cout<<"Please enter a temperature in Fahrenheit: ";
cin>>F;
//输出摄氏温度
cout<<"The temperature in Celsius is "<<CtoF(F)<<endl;
system("pause");
return 0;
}习题3-9 P97
/*
习题3-9 P97
编写函数判别一个数是否是质数,在主程序中实现输入和输出
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//判断整数是否为质数
bool isPrime(unsigned num){
if(num==1) return false;
for(int i=1;i<=num;i++){
if(num%i==0 && i != 1 && i != num) return false;
}
return true;
}
int main()
{
//输入一个正整数
unsigned num;
cout<<"Please enter a positive integer: ";
cin>>num;
//输出结果
if(isPrime(num)) cout<<"This number is prime"<<endl;
else cout<<"This number is not prime"<<endl;
system("pause");
return 0;
}习题3-10 P97
/*
习题3-10 P97
编写函数求两个整数的最大公约数和最小公倍数
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//最大公约数,辗转相除法,递归
int maxDivisor(int a,int b){
int x,y;
x=a/b;y=a%b;
if(y==0) return b;
else return maxDivisor(b,y);
}
//最大公约数,更相减损术,递归
int maxDivisor_1(int a,int b){
int x;
static int s=1;//存储2^(个数)
//可半者半之
if(a%2==0 && b%2==0){
a=a/2;
b=b/2;
s=s*2;
}
//不可半者,副置分母、子之数,以少减多,更相减损,
if(a>b) x=a-b;
else if(a<b) x=b-a;
//求其等也。以等数约之。
if(b==x) return s*x;
else return maxDivisor_1(b,x);
}
//最小公倍数,两数乘积=最大公约数*最小公倍数
int minMultiple(int a,int b){
return a*b/maxDivisor(a,b);
}
int main()
{
//输入整数
int a,b;
cout<<"Pleasr enter two integers(a and b): "<<endl;
cin>>a>>b;
//输出最大公约数-辗转相除法
cout<<"The greatest common divisor of two numbers is "<<maxDivisor(a,b)<<"(辗转相除法)"<<endl;
//输出最小公倍数
cout<<"The least common multiple of two numbers is "<<minMultiple(a,b)<<endl;
//输出最大公约数-更相减损术
cout<<"The greatest common divisor of two numbers is "<<maxDivisor_1(a,b)<<"(更相减损术)"<<endl;
system("pause");
return 0;
}习题3-11 P97
什么叫做嵌套调用?什么叫做递归?
- 如果函数1调用了函数2,函数2又调用了函数3,便形成了函数的嵌套调用
- 函数可以直接或间接地调用自身,称为递归调用
习题3-12 P97
/*
习题3-12 P97
在主函数中提示输入整数n,编写函数用递归的方法求1+2+3+...+n的值
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//求1+2+3+...+n
int plusfun(int n){
if(n==1) return 1;
else return n+plusfun(n-1);
}
int main()
{
//输入整数
int n;
cout<<"请输入一个整数n: "<<endl;
cin>>n;
//输出1+2+3+...+n的值
cout<<"1+2+3+...+n的值为: "<<plusfun(n)<<endl;
system("pause");
return 0;
}习题3-13 P97
/*
习题3-13 P97
用递归的方法编写函数求Fibonacci级数,公式为:
F(n)=F(n-1)+F(n-2),F(1)=F(2)=1
观察递归调用过程
n=100时计算时间很长,说明这个计算量很大,前100项的结果如下,供参考
1 / 1 / 2 / 3 / 5 / 8 / 13 / 21 / 34 / 55
/ 89 / 144 / 233 / 377 / 610 / 987 / 1597
/ 2584 / 4181 / 6765 / 10946 / 17711 / 28657
/ 46368 / 75025 / 121393 / 196418 / 317811 / 514229
/ 832040 / 1346269 / 2178309 / 3524578 / 5702887 / 9227465
/ 14930352 / 24157817 / 39088169 / 63245986 / 102334155 / 165580141
/ 267914296 / 433494437 / 701408733 / 1134903170 / 1836311903 / 2971215073
/ 4807526976 / 7778742049 / 12586269025 / 20365011074 / 32951280099 / 53316291173
/ 86267571272 / 139583862445 / 225851433717 / 365435296162 / 591286729879
/ 956722026041 / 1548008755920 / 2504730781961 / 4052739537881
/ 6557470319842 / 10610209857723 / 17167680177565 / 27777890035288
/ 44945570212853 / 72723460248141 / 117669030460994 / 190392490709135
/ 308061521170129 / 498454011879264 / 806515533049393 / 1304969544928657
/ 2111485077978050 / 3416454622906707 / 5527939700884757 / 8944394323791464
/ 14472334024676220 / 23416728348467684 / 37889062373143900 / 61305790721611580
/ 99194853094755490 / 160500643816367070 / 259695496911122560 / 420196140727489660
/ 679891637638612200 / 1100087778366101900 / 1779979416004714000 / 2880067194370816000
/ 4660046610375530000 / 7540113804746346000 / 12200160415121877000 / 19740274219868226000
/ 31940434634990100000 / 51680708854858330000 / 83621143489848430000 / 135301852344706760000
/ 218922995834555200000 / 354224848179262000000
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//求Fibonacci级数
unsigned long long myFibonacci(unsigned long long n){
//cout<<n<<ends;//打印调试信息
if(n==1 || n==2) return 1;
else return myFibonacci(n-1)+myFibonacci(n-2);
}
int main()
{
//输入
unsigned long long n;
cout<<"请输入一个整数n: "<<endl;
cin>>n;
//输出Fibonacci级数的值
cout<<"F(n): "<<myFibonacci(n)<<endl;
system("pause");
return 0;
}习题3-14 P97
/*
习题3-14 P97
用递归的方法编写函数求n阶勒让德多项式的值,公式为:
funP(n,x)=(x*(2*n-1)*funP(n-1,x)-(n-1)*funP(n-2,x))/n
funP(1,x)=x
funP(0,x)=1
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//求Fibonacci级数
double funP(unsigned n,double x){
//cout<<n<<ends;//打印调试信息
if(n==0) return 1;
else if(n==1) return x;
else return ( x*(2*n-1)*funP(n-1,x) - (n-1)*funP(n-2,x) )/n;
}
int main()
{
//输入
unsigned n;
double x;
cout<<"请输入一个整数n和一个浮点数x: "<<endl;
cin>>n>>x;
//输出n阶勒让德多项式的值
cout<<"F(n): "<<funP(n,x)<<endl;
system("pause");
return 0;
}习题3-15 P97
/*
习题3-15 P97
编写递归函数getPower计算x的y次方,在同一个程序中针对整型和实型实现两个重载函数:
int getPower(int x,int y); //整型版本,当y<0时返回0
double getPower(double x,int y); //实型版本
在主程序中实现输入输出,分别输入一个整数a和一个实数b作为底数,
再输入一个整数m作为指数,输出a的m次方和b的m次方.
另外请读者思考,如果在调用getPower函数计算a的m次方时希望得到一个实型结果
(实型结果表示范围更大,而且可以准确表示m<0时的结果),该如何调用?
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
//整型版本,当y<0时返回0
int getPower(int x,int y){
if(y<0) return 0;
else if(y==0) return 1;
else return x*getPower(x,y-1);
}
//实型版本
double getPower(double x,int y){
if(y<0) return 0;
else if(y==0) return 1;
else return x*getPower(x,y-1);
}
// //整型版本,支持负次幂
double getPower_1(int x,int y){
if(y<0) return 1.0/x*getPower_1(x,y+1);
else if(y==0) return 1;
else return x*getPower_1(x,y-1);
}
//实型版本,支持负次幂
double getPower_1(double x,int y){
//cout<<x<<y;
if(y<0) return getPower_1(x,y+1)/x;
else if(y==0) return 1;
else return x*getPower_1(x,y-1);
}
int main()
{
//输入
int a,m;
double b;
cout<<"请输入一个整数a和一个实数b作为底数: ";
cin>>a>>b;
cout<<"请输入一个整数m作为指数: ";
cin>>m;
//输出值
cout<<"a^m="<<getPower(a,m)<<endl;
cout<<"b^m="<<getPower(b,m)<<endl;
//输出值
cout<<"负次幂:"<<endl;
cout<<"a^(-m)="<<getPower_1(a,-m)<<endl;
cout<<"b^(-m)="<<getPower_1(b,-m)<<endl;
system("pause");
return 0;
}习题3-16 P97
当函数发生递归调用时,同一个局部变量在不同递归深度上可以同时存在不同的取值,这在底层是如何做到的?
- 局部变量在函数内部生效,每个相同局部变量的地址不同,所以他们的取值(地址中储存的数据)可以不同
