function B2Qw(X,choice)
len=length(X);
switch choice
case 1
p=str2double(X(1))*(-1)*power(2,len-1);
for i=2:1:len
p=p+str2double(X(i))*power(2,len-i);
end
sprintf('结果为:%d',p)
case 2
p=str2double(X(1))*(-1)*(power(2,len-1)-1);
for i=2:1:len
p=p+str2double(X(i))*power(2,len-i);
end
sprintf('结果为:%d',p)
case 3
p=0;
for i=2:1:len
p=p+str2double(X(i))*power(2,len-i);
end
p=p*power(-1,str2double(X(1)));
sprintf('结果为:%d',p)
otherwise
errordlg('ERROR');
end
end
clc;
choice=input('输入补码按1,反码按2,原码按3: ');
switch choice
case 1
X=input('请输入补码:','s');
B2Qw(X,choice);
case 2
X=input('请输入反码:','s');
B2Qw(X,choice);
case 3
X=input('请输入原码:','s');
B2Qw(X,choice);
otherwise
errordlg('ERROR');
end
matlab由补码反码原码求值
最新推荐文章于 2024-06-26 17:34:00 发布