Octave/MATLAB 罗马数字转十进制的函数
Octave/MATLAB Function to Convert Roman Numeral Into Decimal Number
我正在尝试用 Octave 编写一个函数来将罗马数字转换为十进制数字。
到目前为止我有两个主要问题:
1) 它只适用于长度不超过 6 个字母的罗马数字
2) 只有当每个字母都小于前一个时,答案才是正确的。即它不会将 (IV) 正确计算为 4,而是错误地计算为 6。
我需要以某种方式解决这两个问题。我强烈怀疑我处理这个问题的方法是错误的,并且有一种更有效的方法可以做到这一点。
无论如何,如果您对这个问题感兴趣and/or,您知道一个好的方法,非常感谢您的帮助。
function roman2num(s)
decimal = [1000, 500, 100, 50, 10, 5, 1];
roman = ["M", "D", "C", "L", "X", "V", "I"];
num = 0;
for i = 1:7
if strcmp(s(1), roman(i))
num += decimal(i);
break
end
end
if length(s) >= 2
for i = 1:7
if strcmp(s(2), roman(i))
num += decimal(i);
break
end
end
end
if length(s) >= 3
for i = 1:7
if strcmp(s(3), roman(i))
num += decimal(i);
break
end
end
end
if length(s) >= 4
for i = 1:7
if strcmp(s(4), roman(i))
num += decimal(i);
break
end
end
end
if length(s) >= 5
for i = 1:7
if strcmp(s(5), roman(i))
num += decimal(i);
break
end
end
end
if length(s) >= 6
for i = 1:7
if strcmp(s(6), roman(i))
num += decimal(i);
break
end
end
end
num
end
据我所知,您唯一需要担心的规则是确定字母是否构成 "add" 或 "subtract" 操作。对于某些字母,只有当右边的第一个不相等的字母代表更大的值时,我们才会减去它。
例如 'IIV'
两个 I
右边的第一个不相等的字母是 V
所以我们减去 2 并为 [=13= 添加 5 ] 因为它右边没有字母。
在 MATLAB 中实现此规则非常简单。
function num = roman2num(s)
decimal = [1000, 500, 100, 50, 10, 5, 1];
roman = ['M', 'D', 'C', 'L', 'X', 'V', 'I'];
tokens = arrayfun(@(x) decimal(find(roman==x,1)), char(s));
num = tokens(end);
for idx = 1:numel(tokens)-1
val = tokens(idx);
ridx = find(tokens(idx+1:end) ~= val, 1);
if ~isempty(ridx) && tokens(ridx + idx) > val
num = num - val;
else
num = num + val;
end
end
我使用 1 到 3333 之间的所有数字进行了测试。
我正在尝试用 Octave 编写一个函数来将罗马数字转换为十进制数字。
到目前为止我有两个主要问题:
1) 它只适用于长度不超过 6 个字母的罗马数字
2) 只有当每个字母都小于前一个时,答案才是正确的。即它不会将 (IV) 正确计算为 4,而是错误地计算为 6。
我需要以某种方式解决这两个问题。我强烈怀疑我处理这个问题的方法是错误的,并且有一种更有效的方法可以做到这一点。
无论如何,如果您对这个问题感兴趣and/or,您知道一个好的方法,非常感谢您的帮助。
function roman2num(s)
decimal = [1000, 500, 100, 50, 10, 5, 1];
roman = ["M", "D", "C", "L", "X", "V", "I"];
num = 0;
for i = 1:7
if strcmp(s(1), roman(i))
num += decimal(i);
break
end
end
if length(s) >= 2
for i = 1:7
if strcmp(s(2), roman(i))
num += decimal(i);
break
end
end
end
if length(s) >= 3
for i = 1:7
if strcmp(s(3), roman(i))
num += decimal(i);
break
end
end
end
if length(s) >= 4
for i = 1:7
if strcmp(s(4), roman(i))
num += decimal(i);
break
end
end
end
if length(s) >= 5
for i = 1:7
if strcmp(s(5), roman(i))
num += decimal(i);
break
end
end
end
if length(s) >= 6
for i = 1:7
if strcmp(s(6), roman(i))
num += decimal(i);
break
end
end
end
num
end
据我所知,您唯一需要担心的规则是确定字母是否构成 "add" 或 "subtract" 操作。对于某些字母,只有当右边的第一个不相等的字母代表更大的值时,我们才会减去它。
例如 'IIV'
两个 I
右边的第一个不相等的字母是 V
所以我们减去 2 并为 [=13= 添加 5 ] 因为它右边没有字母。
在 MATLAB 中实现此规则非常简单。
function num = roman2num(s)
decimal = [1000, 500, 100, 50, 10, 5, 1];
roman = ['M', 'D', 'C', 'L', 'X', 'V', 'I'];
tokens = arrayfun(@(x) decimal(find(roman==x,1)), char(s));
num = tokens(end);
for idx = 1:numel(tokens)-1
val = tokens(idx);
ridx = find(tokens(idx+1:end) ~= val, 1);
if ~isempty(ridx) && tokens(ridx + idx) > val
num = num - val;
else
num = num + val;
end
end
我使用 1 到 3333 之间的所有数字进行了测试。