发布网友 发布时间:2022-04-27 03:37
共1个回答
热心网友 时间:2022-06-25 14:29
这个需要使用函数指针来实现,我为了给你调试程序,写了好几分钟的代码,已经调试通过。
#include "stdafx.h"
#include <iostream.h>
int Cal1(int a,int b)
{
return a+b;
}
int Cal2(int a,int b)
{
return a - b;
}
int Cal3(int a,int b)
{
return a *b;
}
int Cal4(int a,int b)
{
return a / b;
}
int main(int argc, char* argv[])
{
int (*fun[]) (int,int) = {Cal1,Cal2,Cal3,Cal4};
char sign[] = {'+','-','*','/'};
for (int i1 = 0; i1 < 4; i1++)
{
int nValue = fun[i1](3,3);
for (int i2 = 0; i2 < 4; i2++)
{
nValue = fun[i2](nValue,3);
for (int i3 = 0; i3 < 4; i3++)
{
nValue = fun[i3](nValue,3);
for (int i4 = 0; i4 < 4; i4++)
{
nValue = fun[i4](nValue,3);
if (nValue == 6)
{
cout << "3 " << sign[i1] << " 3 " << sign[i2] << " 3 " << sign[i3] << " 3 " << sign[i4] << " 3 = 6\n";
}
}
}
}
}
char a;
cin >> a;
return 0;
}
显示结果如下: