急! Matlab问题

来源:百度知道 编辑:UC知道 时间:2024/06/24 09:01:08
从键盘中输入五位数,按如下规则加密并输出。每位数字加上7,然后用和除以
10的余数取代该数字,在把第一位放到第三位,第二位放到第四位,第三位放到
第五位,第四位放到第一位,第五位放到第二位。假如输入的数字不是一个五位
数,请输出错误的信息。

能写出代码吗,谢谢!

a=input('Please input a number(10000~99999)\n','s');
try
a=str2num(a);
catch
error('ERROR! Illegal Input.');
end
if isnumeric(a)&a<1e5&a>=1e4&a==round(a)
a=mod(de2bi(a,[],10,'left-msb')+7,10);
disp('The encrypted number is:');disp(a([4 5 1 2 3]));
else
error('ERROR! Illegal Input.')
end

有问题
用和除以10的余数取代该数字,这个余数不到五位,怎么做后面的换位操作呢

function res=code_five(a)
c=num2str(a);
len=length(c);
if len~=5
error('Input a wrong number!');
else
b=zeros(1,5);
for i=1:5
b(i)=str2double(c(i));
b(i)=b(i)+7;
b(i)=mod(b(i),10);
end
res=b(4)*10000+b(5)*1000+b(1)*100+b(2)*10+b(3);
end

调用这个函数就行了。