代码分析:两个字符串可以是两个整数、两个小数或者一个小数一个整数的情况。在本代码中,将小数对齐后,直接去掉小数点,对空位进行补零操作,最后按位进行运算即可,比如'0.02' - '0.112',则直接进行'0020' - '0112'的相减即可;'10'-'2',则直接进行'10' - '02'的运算;'8'-'0.02',则将'8'转成'8.0',直接用小数减小数的函数即可。
function [result] = TwoStringSubstract(num1, num2)
%% 两个字符串相减
if ischar(num1) && ischar(num2)
flag_fraction = IsFraction(num1, num2);
% 两个小数相减
if flag_fraction(1) && flag_fraction(2)
[result] = FractionSubFraction(num1, num2);
% 一个整数减一个小数
elseif ((~flag_fraction(1)) && (flag_fraction(2))) || ((flag_fraction(1)) && (~flag_fraction(2)))
% num1是整数字符串,num2是小数字符串
if (~flag_fraction(1)) && (flag_fraction(2))
[result] = IntSubFraction(num1, num2);
end
% num1是小数字符串,num2是整数字符串
if (flag_fraction(1)) && (~flag_fraction(2))
[result] = IntSubFraction(num2, num1);
end
% 两个整数相减
else
[result] = IntSubInt(num1, num2);
end
% 用作最后确保结果的正负号无误
if (str2num(num1) > str2num(num2)) && (result(1) == '-')
result = RemovePos(result, 1);
end
if (str2num(num1) < str2num(num2)) && (result(1) ~= '-')
result = ['-', result];
end
else
disp("输入的数据不是字符串")
end
end
%% 两个字符串数字相减的情况:整型-整型、小数-小数、小数-整数、整数-小数
% 通过三个函数来实现,IntSubInt()、FractionSubFraction()、IntSubFraction()
function [result] = IntSubInt(num1, num2)
diff_bit_num = size(num1, 2) - size(num2, 2);
if diff_bit_num ~= 0
if diff_bit_num > 0
[num2] = FillGap_front(num2, diff_bit_num);
else
[num1] = FillGap_front(num1, abs(diff_bit_num));
end
end
bit_num = size(num1, 2);
isless = isLess(num1, num2, bit_num);
if (isless)
[result] = sub(num2, num1, bit_num);
[result] = Insert(result, 0, '-');
else
[result] = sub(num1, num2, bit_num);
end
end
function [result] = FractionSubFraction(num1, num2)
[num1, num1_point_pos] = fraction2int(num1);
[num2, num2_point_pos] = fraction2int(num2);
diff_point_pos = num1_point_pos - num2_point_pos;
if diff_point_pos ~= 0
if diff_point_pos > 0
[num2] = FillGap_front(num2, diff_point_pos);
else
[num1] = FillGap_front(num1, abs(diff_point_pos));
end
end
diff_bit_num = size(num1, 2) - size(num2, 2);
if diff_bit_num ~= 0
if diff_bit_num > 0
[num2] = FillGap_back(num2, diff_bit_num);
else
[num1] = FillGap_back(num1, abs(diff_bit_num));
end
end
bit_num = size(num1, 2);
[isless] = isLess(num1, num2, bit_num);
if (isless)
[result] = sub(num2, num1, bit_num);
[result] = Insert(result, max(num1_point_pos, num2_point_pos) - 1, '.');
[result] = Insert(result, 0, '-');
else
[result] = sub(num1, num2, bit_num);
[result] = Insert(result, max(num1_point_pos, num2_point_pos) - 1, '.');
end
end
function [result] = IntSubFraction(num1, num2)
num1 = [num1, '.0'];
[result] = FractionSubFraction(num1, num2);
end
%% 将小数变为整型,比如'0.005'->'0005';'9.80'->'980'
function [a_, a_point_pos] = fraction2int(a)
a_point_pos = SearchPointPos(a);
a_ = RemovePos(a, a_point_pos);
end
%% 判断两个字符串是否是小数
function [result] = IsFraction(a, b)
result = zeros(1, 2);
result(1) = SearchPointPos(a);
result(2) = SearchPointPos(b);
if result(1) == -1
result(1) = 0;
end
if result(2) == -1
result(2) = 0;
end
end
%% 将位数是bit_num的两个字符串整型数据a、b,相减得到的字符串整型的结果
function [result_] = sub(a, b, bit_num)
result = []; borrow = 0;
for k = 1 : bit_num
i = bit_num - k + 1;
j = bit_num - k + 1;
x = a(i) - '0';
y = b(j) - '0';
diff_xy = mod((x - borrow - y + 10), 10);
result(end+1) = diff_xy;
% 得到下一位被借走的数borrow
if (x - borrow - y) < 0
borrow = 1;
else
borrow = 0;
end
end
% 调整result的顺序,将倒序变为顺序
[~, result_col] = size(result);
result_ = '';
for i = 1 : result_col
result_ = [result_, num2str(result(1, result_col - i + 1))];
end
end
%% 调整两个整数的位数相同,不同的在前/后面补零,达到位数相同的效果
function [a] = FillGap_front(a, bit_num)
% 在a前面补bit_num个零
for i = 1 : bit_num
a = ['0', a];
end
end
function [a] = FillGap_back(a, bit_num)
% 在a后面补bit_num个零
for i = 1 : bit_num
a = [a, '0'];
end
end
%% 判断两个字符串整型数据,按位比较后,是否a row_str
disp("输入的pos有误")
else
result = '';
for i = 1 : row_str
if pos == (i - 1)
result = [result, ch];
end
result = [result, str(i)];
end
if pos == row_str
result = [result, ch];
end
end
end
%% 找到小数点在的位置
function [result] = SearchPointPos(a)
result = -1;
for i = 1 : size(a, 2)
if (a(i) == '.')
result = i;
break;
end
end
end
%% 从字符串中剔除某一位
function [result] = RemovePos(a, pos)
if (pos <= size(a, 2)) && (pos >= 1)
result = '';
for i = 1 : pos - 1
result = [result, a(i)];
end
for i = pos + 1 : size(a, 2)
result = [result, a(i)];
end
else
disp("pos输入有误");
end
end