函数指针的问题
要求是使用函数指针 显示两个矩阵 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++中的类。然而,标准的C语言规范并不允许直接在结构体成员中定义函数。为了解决这一限制,文章探讨了如何利用函
贴子中的函数指针无法编译的例子的附件
在本案例中,"贴子中的函数指针无法编译的例子"揭示了一个常见的编程问题,即在使用函数指针时遇到了编译错误。这个问题可能涉及到多种因素,包括类型不匹配、未定义的函数引用、不正确的函数指针声明或使用等。
函数指针和函数指针数组的使用
三、函数指针和函数指针数组的使用在实际编程中,函数指针和函数指针数组可以解决多种情况问题。
C++中的函数指针与函数对象的总结
- **类型安全**:函数对象是类的实例,可以有特定的类型,避免了类型转换的潜在问题。- **扩展性**:可以添加额外的状态或功能,如保持状态、记录日志等。
一线研发之声:嵌入式C编程经验之函数指针
在函数指针是一种非常重要的机制,在嵌入式系统中,它能够使得程序更加模块化和灵活。然而,在使用函数指针时,需要非常小心和谨慎,避免出现bug和问题。


