强大的混编,这次用它来实现传说中的“河内”游戏(Hanoi),包括了子函数调用和函数递归调用,且输入参数有字符型常量。
在这里要强烈鄙视一下张德丰的《MATLAB与外部程序接口编程》,内容又落后又烂,居然还停留在6.5的版本,现在很多接口函数都已经变了不少。
m代码:
function hanoi(n,one,two,three)
if n==1
move(one,three);
else
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
end
end
function move(getone,putone)
fprintf('%c',getone);
fprintf('-->');
fprintf('%c/n',putone);
end
c代码:
#include "stdio.h"
#include "hanoi.h"
void main()
{
if (!mclInitializeApplication(NULL,0))
{
printf("error!");
exit(1);
}
if (!hanoiInitialize())
{
printf("error!");
exit(1);
}
mxArray *n,*one,*two,*three;
int N=3;
n=mxCreateDoubleScalar(N);
one=mxCreateString("A");
two=mxCreateString("B");
three=mxCreateString("C");
mlfHanoi(n,one,two,three);
mxDestroyArray(n);
mxDestroyArray(one);
mxDestroyArray(two);
mxDestroyArray(three);
hanoiTerminate();
mclTerminateApplication();
}