函数指针也可作为函数的参数
2009-10-21 09:33
302 查看
#include <iostream>
using namespace std;
void square(float&x,float&y){x=x*x;y=y*y;}
void cube(float&x,float&y){x=x*x*x;y=y*y*y;}
void print(void (*p)(float&,float&),float&x,float&y)
{
cout<<"执行函数前:"<<endl;
cout<<"x:"<<x<<"/ty:"<<y<<endl;
p(x,y);
cout<<"执行函数后:"<<endl;
cout<<"x:"<<x<<"/ty:"<<y<<endl;
}
void Swap(float&x,float&y){int z;z=x;x=y;y=z;}
int main()
{
float a=2,b=3;
char choice='0';
int i;
void (*p[5])(float&x,float&y);
for (i=0;i<5;i++)
{
cout<<"[0]退出[1]平方[2]立方[3]交换x和y的值:";
cin>>choice;
bool quit=false;
switch (choice)
{
case '0':quit=true;break;
case '1':p[i]=square;break;
case '2':p[i]=cube;break;
case '3':p[i]=Swap;break;
default:p[i]=0;
}
if (quit==true){break;}
if (p[i]==0){cout<<"请输入一个从0到3之间的数字!"<<endl;i=i-1;continue;}
print(p[i],a,b);
}
return 0;
}
using namespace std;
void square(float&x,float&y){x=x*x;y=y*y;}
void cube(float&x,float&y){x=x*x*x;y=y*y*y;}
void print(void (*p)(float&,float&),float&x,float&y)
{
cout<<"执行函数前:"<<endl;
cout<<"x:"<<x<<"/ty:"<<y<<endl;
p(x,y);
cout<<"执行函数后:"<<endl;
cout<<"x:"<<x<<"/ty:"<<y<<endl;
}
void Swap(float&x,float&y){int z;z=x;x=y;y=z;}
int main()
{
float a=2,b=3;
char choice='0';
int i;
void (*p[5])(float&x,float&y);
for (i=0;i<5;i++)
{
cout<<"[0]退出[1]平方[2]立方[3]交换x和y的值:";
cin>>choice;
bool quit=false;
switch (choice)
{
case '0':quit=true;break;
case '1':p[i]=square;break;
case '2':p[i]=cube;break;
case '3':p[i]=Swap;break;
default:p[i]=0;
}
if (quit==true){break;}
if (p[i]==0){cout<<"请输入一个从0到3之间的数字!"<<endl;i=i-1;continue;}
print(p[i],a,b);
}
return 0;
}
相关文章推荐
- 函数指针作为参数传递给函数
- C语言提高-第23/24/25讲: 指针的运算/数组名作为函数参数/指针作函数参数(一维数组Vs指针)
- C++编程入门系列之二十六(数组、指针和字符串:数组的存储与初始化、对象数组、数组作为函数参数)
- 指针作为函数参数(备忘)
- c++将引用或者是指针作为函数参数实现实参的运算
- 用结构体指针作为函数参数
- 线程的封装技巧:把任意类的成员函数指针作为参数
- 指针作为函数参数
- 指针【3-1-24】数组作为函数参数
- 指针作为函数参数
- 指针作为函数参数传递
- 数组和指针作为函数参数——高质量C、C++编程指南
- c_指针_一维数组名作为函数参数
- 函数指针作为某个函数的参数及定义函数指针
- 如何理解数组作为函数参数时,数组名就是指针?
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析
- 指针作为函数参数的讨论
- 面试笔试系列3-指针作为函数参数传递
- 说明指针作为函数参数(一)
- 指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为参数,泛型函数