1787: 使用函数指针数组,模拟菜单功能实现方法示例
Memory Limit:128 MB
Time Limit:1.000 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
要求:
使用函数指针数组,模拟菜单功能实现方法示例
设置5个函数分别输出test1至test5
定义一个指针函数类型
声明一个指针函数类型的数组
数组内容与设置的5个函数建立连接
然后输入一个数字,输出函数的输出内容
Input
一行,一个数字
Output
一行,字符串
Sample Input Copy
2
Sample Output Copy
test3
HINT
#include<iostream>
using namespace std;
void t1() { cout<<"test1"; }
void t2() { cout<<"test2"; }
void t3() { cout<<"test3"; }
void t4() { cout<<"test4"; }
void t5() { cout<<"test5"; }
typedef void(*LP)(); //定义了一个函数指针变量类型LP
int main()
{
LP a[]={t1,t2,t3,t4,t5}; //定义了一个LP类型的函数指针数组a,并初始化
int x;
cin>>x;
a[x](); //使用a[x]()来调用选择的函数
return 0;
}
using namespace std;
void t1() { cout<<"test1"; }
void t2() { cout<<"test2"; }
void t3() { cout<<"test3"; }
void t4() { cout<<"test4"; }
void t5() { cout<<"test5"; }
typedef void(*LP)(); //定义了一个函数指针变量类型LP
int main()
{
LP a[]={t1,t2,t3,t4,t5}; //定义了一个LP类型的函数指针数组a,并初始化
int x;
cin>>x;
a[x](); //使用a[x]()来调用选择的函数
return 0;
}