rahmu, the last two equations have a missing operator (number of operators = number of entries - 1) and the last equation would give division by zero since 26 + 57 = 83 and 34 - 83 = -49 which would yield:
1 0 / 13 8 - 7 * 5 65 - -
|______ division by zero (1/0)
As for the code and the answer to the first equation:
clear all
clc
fid = fopen('RPN input.txt'); % Open file where input strings of
% arithmetic equation are located
while ~feof(fid) % Read input strings until end of file
numeric = [];
C = {};
n = 1;
tline = fgetl(fid); % Read current line
while ~isempty(tline) % Store data of input line as cell of strings
k = 0;
if (k+1 < size(tline,2))
while ~isequal(tline(k+1),' ')
k = k + 1;
end
else
k = 1;
end
C{n} = tline(1:k);
n = n + 1;
if (size(tline,2) > 1)
tline = tline(k+2:end);
else
tline = [];
end
end
for i = 1:size(C,2) % Perform arithmetic operation
if ~isnan(str2double(C{i}))
numeric = horzcat(numeric,str2double(C{i}));
else
num1 = numeric(end-1); % First entry for arithmetic operation
num2 = numeric(end); % Second entry for arithmetic operation
switch C{i}
case '+'
result = num1 + num2;
case '-'
result = num1 - num2;
case '*'
result = num1 * num2;
case '/'
result = num1/num2;
otherwise
error('Invalid arithmatic operation.')
end
numeric = numeric(1:end-1);
numeric(end) = result;
end
end
disp(strcat('The result of the arithmetic operation is:',num2str(numeric)));
end
status = fclose(fid); % Close file
Answer: The result of the arithmetic operation is:3103
The second arithmetic operation would give the following result:
The result of the arithmetic operation is:279 1140