怎么把MATLAB代码转换成C++代码
1个回答
展开全部
基本是按照原程序的逻辑写的,但是matlab报错,说数组index超出范围。。。。
但语法思路就是这个样子
唯一要注意的就是matlab的数组从1开始,c里面a[0]在matlab里是a(1)
祝lz好运
%main
for i=1:8
disp(['Please input the digit corresponding to ',num2str(i/4),', ',num2str((i-(i/4)*4)/2),', ',num2str(i-(i/4)*4-((i-(i/4)*4)/2)*2)]);
rule(i)=input('input: ');
end
a=zeros(1,100);
a(50)=1;
for i=1:100
disp(a(i));
end
ch=1;
for j=1:100
if (ch==1)
for i=1:100
if (i==1)
b(i)=rule(a(100)*4+a(1)*2+a(2)+1);
disp(num2str(b(i)));
elseif (i==100)
b(i)=rule(a(99)*4+a(100)*2+a(1)+1);
disp(num2str(b(i)));
else
b(i)=rule(a(i-1)*4+a(i)*2+a(i+1)+1);
disp(num2str(b(i)));
end
end
ch=~ch;
disp(' ');
else
for i=1:100
if (i==1)
a(i)=rule(b(100)*4+b(1)*2+b(2)+1);
disp(num2str(a(i)));
elseif (i==100)
a(i)=rule(b(99)*4+b(100)*2+b(1)+1);
disp(num2str(a(i)));
else
a(i)=rule(b(i-1)*4+b(i)*2+b(i+1)+1);
disp(num2str(a(i)));
end
end
ch=~ch;
disp(' ');
end
end
但语法思路就是这个样子
唯一要注意的就是matlab的数组从1开始,c里面a[0]在matlab里是a(1)
祝lz好运
%main
for i=1:8
disp(['Please input the digit corresponding to ',num2str(i/4),', ',num2str((i-(i/4)*4)/2),', ',num2str(i-(i/4)*4-((i-(i/4)*4)/2)*2)]);
rule(i)=input('input: ');
end
a=zeros(1,100);
a(50)=1;
for i=1:100
disp(a(i));
end
ch=1;
for j=1:100
if (ch==1)
for i=1:100
if (i==1)
b(i)=rule(a(100)*4+a(1)*2+a(2)+1);
disp(num2str(b(i)));
elseif (i==100)
b(i)=rule(a(99)*4+a(100)*2+a(1)+1);
disp(num2str(b(i)));
else
b(i)=rule(a(i-1)*4+a(i)*2+a(i+1)+1);
disp(num2str(b(i)));
end
end
ch=~ch;
disp(' ');
else
for i=1:100
if (i==1)
a(i)=rule(b(100)*4+b(1)*2+b(2)+1);
disp(num2str(a(i)));
elseif (i==100)
a(i)=rule(b(99)*4+b(100)*2+b(1)+1);
disp(num2str(a(i)));
else
a(i)=rule(b(i-1)*4+b(i)*2+b(i+1)+1);
disp(num2str(a(i)));
end
end
ch=~ch;
disp(' ');
end
end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询