题目
假设 n 为非负整数,编写递归函数求 n!。在主函数中,定义一个函数指针,分别通过 函数名和函数指针调用该函数,从屏幕上输入 n 值并输出计算结果。
代码
#include<iostream>
using namespace std;
int jiecheng(int x)
{
if(x > 1) return jiecheng(x-1)*x;
else return 1;
}
int main()
{
int n;
cout << "请输入n值:";
cin >> n;
cout<< "函数名调用结果:"<< jiecheng(n) << endl;
int(*fp)(int x) = jiecheng;//函数指针
cout<< "函数指针调用结果:"<< fp(n) << endl;
return 0;
}