问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:fun.cpp
*作 者:单昕昕
*完成日期:2014年12月11日
*版 本 号:v1.0
*
*问题描述:用函数指针调用函数。
*程序输出:调用后的结果。
*/
#include <iostream>
using namespace std;
void eat();
void sleep();
void hitdoudou();
void run(void (*f)());
int main()
{
int iChoice;
do
{
cout<<"请选择(1-吃;2-睡;3-打;其他-退)";
cin>>iChoice;
if(iChoice==1)
run(eat);
else if(iChoice==2)
run(sleep);
else if(iChoice==3)
run(hitdoudou);
else
return 0;
}
while(true);
return 0;
}
void eat()
{
cout<<"我吃吃吃..."<<endl;
}
void sleep()
{
cout<<"我睡睡睡..."<<endl;
}
void hitdoudou()
{
cout<<"我打打打..."<<endl;
}
void run(void (*f)())
{
f();
}
运行结果:
知识点总结:
用函数指针调用函数。
学习心得:
退出程序就是直接return 0 。
本文介绍如何使用函数指针实现灵活的函数调用,通过案例演示了选择不同功能并执行的操作,包括吃、睡、打等行为,强调了函数指针在编程中的实用性和灵活性。

被折叠的 条评论
为什么被折叠?



