函数指针的问题
要求是使用函数指针 显示两个矩阵 1 2 3 和 2 4 6 并将他们相加 4 5 6 8 10 12
#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
const int m = 2;int i,j;
const int n = 3;
void Fnc1();
void Fnc2();
void sum(float (*pf1)(),float (*pf2)(),float (*pf3)());
main()
{
static float y[m][n]={0,0,0,0,0,0};
static float A[m][n]={1,2,3,4,5,6};
static float B[m][n]={2,4,6,8,10,12};
cout<<"Fnc1()为:"<<endl;
Fnc1();
cout<<"Fnc2(为:"<<endl;
Fnc1();
cout<<"sum为"<<endl;
sum(Fnc1,Fnc2,y);
getchar();
}
float * Fnc1()
{
for ( i=0;i<m;i++)
{ for( j=0;j<n;j++)
cout<<setw(5)<<A[i][j];
cout<<endl;
}
return A;
}
float * Fnc2()
{
for ( i=0;i<m;i++)
for( j=0;j<n;j++)
cout<<setw(5)<<B[i][j];
cout<<endl;
return B;
}
void sum(float (*pf1)(),float (*pf2)(),float * y)
{
float * cA;
float * cB;
cA = pf1();
cB = pf2();
for(i=0;i<m;i++)
for(j=0;j<n;j++)
y[i][j]=cA[i][j]+cB[i][j];
return;
for ( i=0;i<m;i++)
for( j=0;j<n;j++)
cout<<setw(5)<<y[i][j];
cout<<endl;
}
调试不对~ 大家帮我改改
#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
const int m = 2;int i,j;
const int n = 3;
void Fnc1();
void Fnc2();
void sum(float (*pf1)(),float (*pf2)(),float (*pf3)());
main()
{
static float y[m][n]={0,0,0,0,0,0};
static float A[m][n]={1,2,3,4,5,6};
static float B[m][n]={2,4,6,8,10,12};
cout<<"Fnc1()为:"<<endl;
Fnc1();
cout<<"Fnc2(为:"<<endl;
Fnc1();
cout<<"sum为"<<endl;
sum(Fnc1,Fnc2,y);
getchar();
}
float * Fnc1()
{
for ( i=0;i<m;i++)
{ for( j=0;j<n;j++)
cout<<setw(5)<<A[i][j];
cout<<endl;
}
return A;
}
float * Fnc2()
{
for ( i=0;i<m;i++)
for( j=0;j<n;j++)
cout<<setw(5)<<B[i][j];
cout<<endl;
return B;
}
void sum(float (*pf1)(),float (*pf2)(),float * y)
{
float * cA;
float * cB;
cA = pf1();
cB = pf2();
for(i=0;i<m;i++)
for(j=0;j<n;j++)
y[i][j]=cA[i][j]+cB[i][j];
return;
for ( i=0;i<m;i++)
for( j=0;j<n;j++)
cout<<setw(5)<<y[i][j];
cout<<endl;
}
调试不对~ 大家帮我改改
...全文
相关推荐
关于在 C 语言结构体中使用函数指针的问题
结构体是 C 语言中一个非常重要的自定义数据类型,它描述了一系列相同类型或不同类型数据构成的数据集合。但是在 C 语言的结构体成员却不能是函数,针对这一问题,本文分析探讨了将函数指针作为结构体成员的理论和方法。通过在结构体中定义函数指针类型的成员,从而实现了在结构体中使用函数的目的。
贴子中的函数指针无法编译的例子的附件
贴子中的函数指针无法编译的例子的附件 VC6工程文件
函数指针和函数指针数组的使用
本文介绍的函数指针以及函数指针数组解决了实际程序编写过程中遇到的当在多种情况情况下调研多个函数的问题。
C++中的函数指针与函数对象的总结
以下是对C++中的函数指针与函数对象的使用进行了详细的分析介绍,需要的朋友可以参考下
一线研发之声:嵌入式C编程经验之函数指针
一线研发之声:嵌入式C编程经验之函数指针


