这段程序是很久以前写的了,用的是Turbo C++ 3.0,现在看这段程序头皮都麻。我觉得这段代码几乎可以作反面教材了,难以理解的宏,函数指针!GoTo语句,毫无意义的命名,多达七层的循环!现在我看到 Void* 和 Inti(*) 这些东西都有点佩服自己,这些都是什么玩意啊!看的时候,有的注释我想了好长时间才知道啥意思。不过它真的可以给出结果!我特意从网上下载了Trubo C++ 3.0重新编译了这段代码,能正确运行,只是中文显示乱码,于是把中文改为英文,生成了exe文件:
24.exe ,不过每计算一次就要重新执行一次程序啊!
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
Code
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
* 24.cpp Author-ZhiQiao
* 24 点游戏
* 四个 1 到 10 之间的整数, 每个数都必须使用且只能用一次.
* 加,减,乘,除可以随意使用,设法使运算结果为24.
*/
#include<iostream.h>
#include<conio.h>
![](/Images/OutliningIndicators/None.gif)
#define N 4
//LOOP 宏的定义为了书写方便
#define LOOP(i) for(i=0; i<N; i++)
//此函数将void型指针转换为四个操作符对应的函数指针.
#define F(i) ( (int(*)(int,int))opt[i] )
#define FI F(i)
#define FJ F(j)
#define FK F(k)
![](/Images/OutliningIndicators/None.gif)
//定义+,-,x,/四个操作对应的函数
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int add(int a, int b)
{ return a + b; }
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int sub(int a, int b)
{ return a - b; }
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int mul(int a, int b)
{ return a * b; }
int div(int a, int b)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ //如果a不能被b整除或b为0就返回一个比较大的整数
if(b==0 || a%b) return 2401;
return a/b;
}
![](/Images/OutliningIndicators/None.gif)
// 根据相应函数在数组中的下标判断其运算符号,用来输出结果
char whichOpt(int index)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
if(index==0) return '+';
else if(index==1) return '-';
else if(index==2) return '*';
return '/';
}
![](/Images/OutliningIndicators/None.gif)
// 求解24点的算法实现
void howObtain24(int num[], void* opt[])
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int i,j,k,a,b,c,d;
![](/Images/OutliningIndicators/InBlock.gif)
LOOP(i) LOOP(j) LOOP(k) //选择运算符
LOOP(a) LOOP(b) LOOP(c) LOOP(d) //数字不同的排列顺序
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(a==b||a==c||a==d||b==c||b==d||c==d)
continue;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( FI(FJ(FK(num[a], num[b]), num[c]), num[d]) == 24)
{
cout << "((" << num[a] << whichOpt(k) << num[b] << ')'
<< whichOpt(j) << num[c] << ')'
<< whichOpt(i) << num[d] << endl;
goto END;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( FI(FJ(num[a], num[b]), FK(num[c], num[d])) == 24 )
{
cout << '(' << num[a] << whichOpt(j) << num[b] << ')'
<< whichOpt(i) <<
'(' << num[c] << whichOpt(k) << num[d] << ')' << endl;
goto END;
}
}
cout << "这几个数字无法得到 24\n"; return;
END: return;
}
![](/Images/OutliningIndicators/None.gif)
//=======================================================================
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//四个数字
int num[N];
//四个运算符对应的函数指针
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void* opt[N] =
{ (void*)add, (void*)sub, (void*)mul, (void*)div };
cout << "请输入四个 1 到 10 之间的整数 :\n";
for(int i=0; i<N; i++) cin >> num[i];
for(int j=0; j<N; j++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(num[j]<1 || num[j]>10)
{
cout << "输入不符合要求!\n";
getch();
return 0;
}
howObtain24(num,opt);
getch();
return 0;
}