PEMDAS Recursive Algorithm Program - Negative Numbers Help

29 次查看(过去 30 天)
Hi, we have an assignment in my programming class that were are supposed to create a recursive algorithm that correctly processes the order of operations in the PEMDAS sequence. I have a program that runs and almost works properly except it does not correctly process negative numbers in the input or output. I am kind of confused as where to start for this without ruining the code I have so far. This is what I have written:
function PEMDAS = algebra(exp)
lefthead = 0
lefttail = 0
righthead = 0
righttail = 0
check = true
while check
for i = 1:length(exp)
if i>length(exp)
break
end
if exp(i) == '^'
lefttail = i-1
righthead = i+1
for j = lefttail :-1:1
if j == 1
lefthead = j
elseif exp(j) == '^' || exp(j) == '*' || exp(j) == '/' || exp(j) == '+' || exp(j) == '-'
lefthead = j+1
break
end
end
for j = righthead :1:length(exp)
if j == length(exp)
righttail = j
elseif exp(j) == '^' || exp(j) == '*' || exp(j) == '/' || exp(j) == '+' || exp(j) == '-'
righttail = j-1
break
end
end
subexp = exp(lefthead:righttail)
val = evaluate(subexp)
exp = [exp(1:lefthead-1), val, exp(righttail+1:end)]
end
end
for i = 1:length(exp)
if i>length(exp)
break
end
if exp(i) == '*' || exp(i) == '/'
lefttail = i-1
righthead = i+1
for j = lefttail :-1:1
if j == 1
lefthead = j
elseif exp(j) == '^' || exp(j) == '*' || exp(j) == '/' || exp(j) == '+' || exp(j) == '-'
lefthead = j+1
break
end
end
for j = righthead :1:length(exp)
if j == length(exp)
righttail = j
elseif exp(j) == '^' || exp(j) == '*' || exp(j) == '/' || exp(j) == '+' || exp(j) == '-'
righttail = j-1
break
end
end
subexp = exp(lefthead:righttail)
val = evaluate(subexp)
exp = [exp(1:lefthead-1), val, exp(righttail+1:end)]
end
end
for i =1:length(exp)
if i>length(exp)
break
end
if exp(i) == '+' || exp(i) == '-'
lefttail = i-1
righthead = i+1
for j = lefttail :-1:1
if j == 1
lefthead = j
elseif exp(j) == '^' || exp(j) == '*' || exp(j) == '/' || exp(j) == '+' || exp(j) == '-'
lefthead = j+1
break
end
end
for j = righthead :1:length(exp)
if j == length(exp)
righttail = j
elseif exp(j) == '^' || exp(j) == '*' || exp(j) == '/' || exp(j) == '+' || exp(j) == '-'
righttail = j-1
break
end
end
subexp = exp(lefthead:righttail)
val = evaluate(subexp)
exp = [exp(1:lefthead-1), val, exp(righttail+1:end)]
end
end
for i = 1:length(exp)
if exp(i) == '^' || exp(i) == '*' || exp(i) == '/' || exp(i) == '+' || exp(i) == '-'
break
elseif i==length(exp)
check = false
end
end
end
PEMDAS = exp
end
function pemdas = evaluate(subexp)
for i = 1:length(subexp)
if subexp(i) == '^' || subexp(i) == '*' || subexp(i) == '/' || subexp(i) == '+' || subexp(i) == '-'
opr = subexp(i)
subexp(i) = ' '
break
end
end
nums = split(subexp)
num1 = str2num(nums{1})
num2 = str2num(nums{2})
switch opr
case '^'
do = num1^num2
case '*'
do = num1*num2
case '/'
do = num1/num2
case '+'
do = num1+num2
case '-'
do = num1-num2
end
pemdas = num2str(do)
end
Any help would be greatly appreciated!

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Modeling Patterns for C Code Constructs 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by